Repository navigation
fix(watch): walk all seller pages so removal detection stays truthful #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9a5cc3e
146a984
91e54f8
fa953bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -516,6 +516,221 @@ func TestUserBySlugURLAndHash(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // user items pagination | ||
|
|
||
| func TestUserItemsReturnsPageWithNextCursor(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| h.fake.UserItemsPageSize = 2 | ||
| for i := 0; i < 5; i++ { | ||
| h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(i), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| } | ||
| r := h.must("", "user", "items", fakewallapop.OtherHash) | ||
| var page struct { | ||
| Items []struct{ Hash string } | ||
| NextPage string `json:"next_page"` | ||
| } | ||
| decode(t, r.stdout, &page) | ||
| if len(page.Items) != 2 || page.NextPage == "" { | ||
| t.Fatalf("got %d items, next %q", len(page.Items), page.NextPage) | ||
| } | ||
| reqs := h.fake.RequestsTo("/api/v3/users/" + fakewallapop.OtherHash + "/items") | ||
| if len(reqs) != 1 || reqs[0].Query.Get("since") != "" { | ||
| t.Fatalf("first page request: %d, query %v", len(reqs), reqs[0].Query) | ||
| } | ||
| next := page.NextPage | ||
|
|
||
| r = h.must("", "user", "items", fakewallapop.OtherHash, "--next", next) | ||
| decode(t, r.stdout, &page) | ||
| if len(page.Items) != 2 { | ||
| t.Fatalf("second page got %d items", len(page.Items)) | ||
| } | ||
| reqs = h.fake.RequestsTo("/api/v3/users/" + fakewallapop.OtherHash + "/items") | ||
| if len(reqs) != 2 || reqs[1].Query.Get("since") != next { | ||
| t.Fatalf("second page did not pass since=%q: %v", next, reqs[len(reqs)-1].Query) | ||
| } | ||
| if page.NextPage == "" { | ||
| t.Fatal("second page lost the next cursor") | ||
| } | ||
|
|
||
| r = h.must("", "user", "items", fakewallapop.OtherHash, "--next", page.NextPage) | ||
| var last map[string]any | ||
| decode(t, r.stdout, &last) | ||
| if got, ok := last["next_page"]; !ok || got != "" { | ||
| t.Fatalf("last page must carry an empty next_page key, got %q present=%v", got, ok) | ||
| } | ||
| if n := len(last["items"].([]any)); n != 1 { | ||
| t.Fatalf("last page: %d items", n) | ||
| } | ||
| } | ||
|
|
||
| func TestUserItemsAllWalksEveryPage(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| h.fake.UserItemsPageSize = 2 | ||
| for i := 0; i < 5; i++ { | ||
| h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(i), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| } | ||
| r := h.must("", "user", "items", fakewallapop.OtherHash, "--all") | ||
| var page struct { | ||
| Items []struct{ Hash string } | ||
| NextPage string `json:"next_page"` | ||
| } | ||
| decode(t, r.stdout, &page) | ||
| if len(page.Items) != 5 || page.NextPage != "" { | ||
| t.Fatalf("got %d items, next %q", len(page.Items), page.NextPage) | ||
| } | ||
| // Count alone would let a duplicate hide an omission: assert the whole | ||
| // ordered listing. | ||
| want := []string{"hash00000000", "hash00000001", "hash00000002", "hash00000003", "hash00000004"} | ||
| for i, it := range page.Items { | ||
| if it.Hash != want[i] { | ||
| t.Fatalf("item %d = %s, want %s", i, it.Hash, want[i]) | ||
| } | ||
| } | ||
| if n := len(h.fake.RequestsTo("/api/v3/users/" + fakewallapop.OtherHash + "/items")); n != 3 { | ||
| t.Fatalf("expected 3 sequential page requests, got %d", n) | ||
| } | ||
| if strings.Contains(r.stderr, "warning") { | ||
| t.Fatalf("unexpected warning: %s", r.stderr) | ||
| } | ||
| } | ||
|
|
||
| func TestUserItemsAllStopsAtBoundWithWarning(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| h.fake.UserItemsPageSize = 1 | ||
| for i := 0; i < 60; i++ { | ||
| h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(i), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| } | ||
| r := h.must("", "user", "items", fakewallapop.OtherHash, "--all") | ||
| var page struct { | ||
| Items []struct{ Hash string } | ||
| NextPage string `json:"next_page"` | ||
| } | ||
| decode(t, r.stdout, &page) | ||
| if len(page.Items) != 50 || page.NextPage != "50" { | ||
| t.Fatalf("bound walk: %d items, next %q", len(page.Items), page.NextPage) | ||
| } | ||
| if !strings.Contains(r.stderr, "stopped after 50 pages") { | ||
| t.Fatalf("missing bound warning: %s", r.stderr) | ||
| } | ||
| if n := len(h.fake.RequestsTo("/api/v3/users/" + fakewallapop.OtherHash + "/items")); n != 50 { | ||
| t.Fatalf("expected bounded 50 requests, got %d", n) | ||
| } | ||
| resume := h.must("", "user", "items", fakewallapop.OtherHash, "--next", page.NextPage) | ||
| var resumed struct { | ||
| Items []struct{ Hash string } `json:"items"` | ||
| } | ||
| decode(t, resume.stdout, &resumed) | ||
| if len(resumed.Items) != 1 || resumed.Items[0].Hash != "hash00000050" { | ||
| t.Fatalf("resume from %q returned %d items starting at %q", page.NextPage, len(resumed.Items), resumed.Items[0].Hash) | ||
| } | ||
| } | ||
|
|
||
| func TestUserItemsAllRejectsRepeatedCursor(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| h.fake.UserItemsPageSize = 1 | ||
| h.fake.UserItemsStickyCursor = "1" | ||
| for i := 0; i < 3; i++ { | ||
| h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(i), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| } | ||
| r := h.run("", "user", "items", fakewallapop.OtherHash, "--all") | ||
| if r.code == 0 || !strings.Contains(r.stderr, "cursor did not advance") { | ||
| t.Fatalf("expected stalled cursor error, exit %d stderr %q", r.code, r.stderr) | ||
| } | ||
| var partial struct { | ||
| Items []struct{ Hash string } `json:"items"` | ||
| Next string `json:"next_page"` | ||
| } | ||
| decode(t, r.stdout, &partial) | ||
| if len(partial.Items) != 2 || partial.Next != "1" { | ||
| t.Fatalf("stalled walk lost partial results/cursor: %+v", partial) | ||
| } | ||
| } | ||
|
|
||
| func TestUserItemsAllPreservesPagesWhenLaterRequestFails(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| h.fake.UserItemsPageSize = 2 | ||
| h.fake.UserItemsFailSince = "2" | ||
| for i := 0; i < 5; i++ { | ||
| h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(i), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| } | ||
| r := h.run("", "user", "items", fakewallapop.OtherHash, "--all") | ||
| if r.code == 0 || !strings.Contains(r.stderr, "500") { | ||
| t.Fatalf("expected later-page error, exit %d stderr %q", r.code, r.stderr) | ||
| } | ||
| var partial struct { | ||
| Items []struct{ Hash string } `json:"items"` | ||
| Next string `json:"next_page"` | ||
| } | ||
| decode(t, r.stdout, &partial) | ||
| if len(partial.Items) != 2 || partial.Next != "2" { | ||
| t.Fatalf("failed walk lost partial results/cursor: %+v", partial) | ||
| } | ||
| } | ||
|
|
||
| // watch seller pagination (UserItemsAll) | ||
|
|
||
| func TestSellerWatchWalksAllPages(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| h.fake.UserItemsPageSize = 2 | ||
| var items []*fakewallapop.Item | ||
| for i := 0; i < 3; i++ { | ||
| items = append(items, h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(i), Title: "Bike", Seller: fakewallapop.OtherHash})) | ||
| } | ||
| h.must("", "watch", "add", "seller", "other-seller-68037934", "--name", "fran") | ||
| // The third item lives on page 2; removing a page-1 item must not | ||
| // ghost-remove it, and the real removal is reported exactly once. | ||
| items[0].Removed = true | ||
| r := h.must("", "watch", "check", "fran", "--format", "jsonl") | ||
| if strings.Count(r.stdout, `"item.removed"`) != 1 || !strings.Contains(r.stdout, "hash00000000") { | ||
| t.Fatalf("events:\n%s", r.stdout) | ||
| } | ||
| // The page-2 item is tracked: its removal is seen on the next check. | ||
| items[2].Removed = true | ||
| r = h.must("", "watch", "check", "fran", "--format", "jsonl") | ||
| if strings.Count(r.stdout, `"item.removed"`) != 1 || !strings.Contains(r.stdout, "hash00000002") { | ||
| t.Fatalf("page-2 item not tracked:\n%s", r.stdout) | ||
| } | ||
| } | ||
|
|
||
| func TestSellerWatchDoesNotReportRemovedWhenWalkTruncates(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| h.fake.UserItemsPageSize = 1 | ||
| for i := 0; i < 55; i++ { | ||
| h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(i), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| } | ||
| h.must("", "watch", "add", "seller", "other-seller-68037934", "--name", "fran") | ||
| r := h.must("", "watch", "check", "fran") | ||
| if strings.Contains(r.stdout, `"item.removed"`) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Truncated watch regression passes When the removal sweep is enabled and the first 50 items remain unchanged between the baseline and check, Arrange for a previously seen item to be absent from the fetched 50 pages during the check, then assert that no removal is emitted while the walk is truncated. Prompt for AI agentsIn `internal/cli/cli_test.go` at line 650:
**Truncated watch regression passes**
When the removal sweep is enabled and the first 50 items remain unchanged between the baseline and check, `TestSellerWatchDoesNotReportRemovedWhenWalkTruncates` passes with the removal sweep enabled: its add and check use the same first 50 items, so none of the hashes in `seen` are missing. The assertion never exercises a previously seen item beyond the fetched pages, allowing false-removal reports on truncated walks to return.
Arrange for a previously seen item to be absent from the fetched 50 pages during the check, then assert that no removal is emitted while the walk is truncated. |
||
| t.Fatalf("truncated walk must not report removals:\n%s", r.stdout) | ||
| } | ||
| // Both the baseline check (at add) and this check walked the bound. | ||
| if n := len(h.fake.RequestsTo("/api/v3/users/" + fakewallapop.OtherHash + "/items")); n != 100 { | ||
| t.Fatalf("expected bounded 50 page requests per check, got %d", n) | ||
| } | ||
| } | ||
|
|
||
| func TestSellerWatchDoesNotReportRemovedWhenCursorStalls(t *testing.T) { | ||
| h := newHarness(t) | ||
| h.login() | ||
| first := h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(0), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| h.must("", "watch", "add", "seller", "other-seller-68037934", "--name", "fran") | ||
| h.fake.AddItem(fakewallapop.Item{Hash: "hash" + padHash(1), Title: "Bike", Seller: fakewallapop.OtherHash}) | ||
| first.Removed = true | ||
| h.fake.UserItemsPageSize = 1 | ||
| h.fake.UserItemsStickyCursor = "1" | ||
| r := h.run("", "watch", "check", "fran", "--format", "jsonl") | ||
| if r.code == 0 || strings.Contains(r.stdout, `"item.removed"`) { | ||
| t.Fatalf("stalled walk must fail without false removals, exit %d stdout:\n%s\nstderr:\n%s", r.code, r.stdout, r.stderr) | ||
| } | ||
| } | ||
|
|
||
| // chat | ||
|
|
||
| func TestChatSendPublishesWebShapedPayload(t *testing.T) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Page-two watch regression passes
When the watch implementation reads only page 1 and the first of the three fixture items is removed,
TestSellerWatchWalksAllPagespasses with a first-page-only watch: its baseline records only items 0 and 1; after item 0 is removed, items 1 and 2 both fit on page 1, so item 2 appears as aseller.new_itemand the assertion atinternal/cli/cli_test.go:630ignores it. The next check then reports item 2 removed, satisfying the second assertion too.Keep enough items active after removing the first one that a page-one-only check still misses a previously seen item, and assert that no
seller.new_itemevent is emitted for the page-two item.Prompt for AI agents