Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanv committed Aug 20, 2024
1 parent 4d0cea2 commit 426ddd4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
12 changes: 11 additions & 1 deletion backend/lynx/lynx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ import (

var parseUrlHandlerFunc = handleParseURL

// Interfaces for dependency injection for summarization tests
type Summarizer interface {
MaybeSummarizeLink(app core.App, linkID string)
}
var CurrentSummarizer Summarizer = &DefaultSummarizer{}
type DefaultSummarizer struct{}
func (s *DefaultSummarizer) MaybeSummarizeLink(app core.App, linkID string) {
summarizer.MaybeSummarizeLink(app, linkID)
}

func InitializePocketbase(app core.App) {

apiKeyAuth := ApiKeyAuthMiddleware(app)
Expand Down Expand Up @@ -79,7 +89,7 @@ func InitializePocketbase(app core.App) {

app.OnModelAfterCreate("links").Add(func(e *core.ModelEvent) error {
routine.FireAndForget(func() {
summarizer.MaybeSummarizeLink(app, e.Model.GetId())
CurrentSummarizer.MaybeSummarizeLink(app, e.Model.GetId())
})
return nil
})
Expand Down
90 changes: 90 additions & 0 deletions backend/lynx/lynx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,93 @@ func generateRecordToken(collectionNameOrId string, email string) string {

return token
}

func TestSummarizationHook(t *testing.T) {
setupTestApp := func(t *testing.T) *tests.TestApp {
testApp, err := tests.NewTestApp(testDataDir)
if err != nil {
t.Fatal(err)
}

InitializePocketbase(testApp)

return testApp
}

summarizeCalled := false
originalSummarizer := CurrentSummarizer

CurrentSummarizer = &MockSummarizer{
MaybeSummarizeLinkFunc: func(app core.App, linkID string) {
summarizeCalled = true
},
}

t.Cleanup(func() {
CurrentSummarizer = originalSummarizer
})

scenarios := []tests.ApiScenario{
{
Name: "Create link and trigger summarization hook",
Method: http.MethodPost,
Url: "/api/collections/links/records",
Body: strings.NewReader(generateValidLinkJSON(t)),
RequestHeaders: map[string]string{
"Content-Type": "application/json",
"Authorization": generateRecordToken("users", "test@example.com"),
},
ExpectedStatus: 200,
ExpectedEvents: map[string]int{
"OnModelBeforeCreate": 1,
"OnModelAfterCreate": 1,
"OnRecordBeforeCreateRequest": 1,
"OnRecordAfterCreateRequest": 1,
},
ExpectedContent: []string{"example.com"},
TestAppFactory: setupTestApp,
AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) {
if !summarizeCalled {
t.Fatal("MaybeSummarizeLink was not called after link creation")
}
},
},
}

for _, scenario := range scenarios {
scenario.Test(t)
}
}

type MockSummarizer struct {
MaybeSummarizeLinkFunc func(app core.App, linkID string)
}

func (m *MockSummarizer) MaybeSummarizeLink(app core.App, linkID string) {
if m.MaybeSummarizeLinkFunc != nil {
m.MaybeSummarizeLinkFunc(app, linkID)
}
}


func generateValidLinkJSON(t *testing.T) string {
link := map[string]interface{}{
"title": "Test Link",
"original_url": "https://example.com",
"cleaned_url": "https://example.com",
"hostname": "example.com",
"user": "h4oofx0tx2eupnq", // test@example.com
"added_to_library": time.Now().Format(time.RFC3339),
"excerpt": "This is a test excerpt",
"raw_text_content": "This is the raw text content of the test link",
"read_time_seconds": 60,
"read_time_display": "1 min",
}

jsonData, err := json.Marshal(link)
if err != nil {
t.Fatalf("Failed to marshal link data: %v", err)
}

return string(jsonData)
}
8 changes: 6 additions & 2 deletions backend/test_pb_data/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Test data admin login
- `test@example.com`
- `testpassword`

# Test data users:

- `test@example.com`: Basic user with no related models.
- `test@example.com` (`h4oofx0tx2eupnq`): Basic user with no related models.
- Has a vaild API key: `this_is_a_test_api_key` (ID `qvwy0nqws813o4s`)
- `test2@example.com`: Basic user with a single link, ID `8n3iq8dt6vwi4ph`
- `test2@example.com` (`u3ozd82edmlybb1`): Basic user with a single link, ID `8n3iq8dt6vwi4ph`
- Has an expired API key: `this_key_is_expired` (ID `rmj5sowq4rtbz43`)

0 comments on commit 426ddd4

Please sign in to comment.