Skip to content

Commit 6d6eed5

Browse files
author
npub1cc3ha7z055mu0rwwu7806t2wt8mj3pvu0uv5mfp2c50dahaqhczshdalg6
committed
test(desktop): mock-mode get_channel_window emits bounds + honors cursor
The get_channel_window mock branch returned rows only, but every caller runs parseChannelWindowResponse, which requires exactly one kind-39006 bounds event and throws otherwise. So ordinary mock-mode channel opens broke even though the relay parity gate (parity-ancestor-island, relay mode) stayed GREEN. Fix (additive): - Mock branch now emits exactly one correctly-keyed 39006 bounds event. The d tag matches expectedBoundsKey (<channel>:head at the frontier, else <channel>:<created_at>:<event_id> of the request cursor); has_more/next_cursor agree, computed from a limit+1-style probe (candidates past the cursor > cap). - Mock branch now honors the composite (until, before_id) cursor like the relay keyset (created_at DESC, id ASC), so page 2 advances instead of re-returning page 1. Adds channel-window-mock-paging.spec.ts: seeds 75 top-level rows into the empty random channel, walks page-1 (head) -> page-2 (signed cursor), asserts each page parses through the real parseChannelWindowResponse and the union has no duplication and no loss. Registered in the smoke project. Mock-mode mirrors contract v2 by hand alongside the Tauri command — a future contract change must touch both. Co-authored-by: npub1cc3ha7z055mu0rwwu7806t2wt8mj3pvu0uv5mfp2c50dahaqhczshdalg6 <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Tyler Longwell <[email protected]>
1 parent 574c31f commit 6d6eed5

3 files changed

Lines changed: 158 additions & 7 deletions

File tree

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default defineConfig({
5959
"**/virtualization.spec.ts",
6060
"**/scroll-history.spec.ts",
6161
"**/channel-dense-second-reach.spec.ts",
62+
"**/channel-window-mock-paging.spec.ts",
6263
"**/overscroll-boundary.spec.ts",
6364
"**/cold-switch-longtask.perf.ts",
6465
"**/timeline-no-shift.spec.ts",

‎desktop/src/testing/e2eBridge.ts‎

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@/shared/api/customEmoji";
1616
import {
1717
KIND_AGENT_OBSERVER_FRAME,
18+
KIND_CHANNEL_WINDOW_BOUNDS,
1819
KIND_DM_VISIBILITY,
1920
KIND_EVENT_REMINDER,
2021
KIND_HUDDLE_STARTED,
@@ -3710,7 +3711,36 @@ async function handleGetChannelMessagesBefore(
37103711
}
37113712

37123713
/**
3713-
* Mirror of the desktop `get_channel_window` command (channel_window.rs): fetch
3714+
* Build the single kind-39006 bounds event a channel window response must carry.
3715+
* The `d` tag key must match `expectedBoundsKey` in channelWindowResponse.ts:
3716+
* `<channel>:head` at the frontier, else `<channel>:<created_at>:<event_id>` of
3717+
* the request cursor (lower-cased). `has_more`/`next_cursor` must agree — the
3718+
* parser rejects a bounds event where they disagree.
3719+
*/
3720+
function buildMockChannelWindowBounds(
3721+
args: {
3722+
channelId: string;
3723+
cursor?: { created_at: number; event_id: string } | null;
3724+
},
3725+
hasMore: boolean,
3726+
nextCursor: { created_at: number; id: string } | null,
3727+
): RelayEvent {
3728+
const suffix = args.cursor
3729+
? `${args.cursor.created_at}:${args.cursor.event_id.toLowerCase()}`
3730+
: "head";
3731+
const boundsKey = `${args.channelId.toLowerCase()}:${suffix}`;
3732+
return {
3733+
id: `mock-window-bounds-${boundsKey}`,
3734+
pubkey: DEFAULT_MOCK_IDENTITY.pubkey,
3735+
created_at: Math.floor(Date.now() / 1000),
3736+
kind: KIND_CHANNEL_WINDOW_BOUNDS,
3737+
tags: [["d", boundsKey]],
3738+
content: JSON.stringify({ has_more: hasMore, next_cursor: nextCursor }),
3739+
sig: "mocksig".repeat(20).slice(0, 128),
3740+
};
3741+
}
3742+
3743+
/**
37143744
* one server-assembled channel window over the `/query` bridge. Emits the flat
37153745
* event array the relay assembles — top-level rows (newest first), then the aux
37163746
* closure, then relay-signed `39005` summaries and exactly one `39006` bounds
@@ -3733,10 +3763,11 @@ async function handleGetChannelWindow(
37333763
const identity = getIdentity(config);
37343764

37353765
if (!identity) {
3736-
// Mock store: top-level timeline rows in relay order. The mock store holds
3737-
// no relay-synthesized 39005/39006, so this is rows-only (parity suites run
3738-
// the relay path below, which returns the full assembled window).
3739-
return getMockMessageStore(args.channelId)
3766+
// Mock store: server-assembled channel window over the mock event store,
3767+
// mirroring the relay path's shape so callers (parseChannelWindowResponse)
3768+
// parse both modes identically. Top-level timeline rows in relay order,
3769+
// then exactly one kind-39006 bounds event.
3770+
const candidates = getMockMessageStore(args.channelId)
37403771
.filter(
37413772
(event) =>
37423773
TIMELINE_KINDS.has(event.kind) &&
@@ -3745,8 +3776,31 @@ async function handleGetChannelWindow(
37453776
.sort(
37463777
(left, right) =>
37473778
right.created_at - left.created_at || left.id.localeCompare(right.id),
3748-
)
3749-
.slice(0, cap);
3779+
);
3780+
// Honor the composite (until, before_id) cursor exactly like the relay's
3781+
// keyset: keep only rows strictly older than the cursor under the
3782+
// (created_at DESC, id ASC) order — older created_at, or the same second
3783+
// with a strictly greater id.
3784+
const cursor = args.cursor;
3785+
const afterCursor = cursor
3786+
? candidates.filter(
3787+
(event) =>
3788+
event.created_at < cursor.created_at ||
3789+
(event.created_at === cursor.created_at &&
3790+
event.id > cursor.event_id),
3791+
)
3792+
: candidates;
3793+
const rows = afterCursor.slice(0, cap);
3794+
// Exhaustion probe mirrors the relay's limit+1: more rows past the cursor
3795+
// than the page cap means another page exists. next_cursor is the last
3796+
// retained row.
3797+
const hasMore = afterCursor.length > cap;
3798+
const lastRow = rows[rows.length - 1];
3799+
const nextCursor =
3800+
hasMore && lastRow
3801+
? { created_at: lastRow.created_at, id: lastRow.id }
3802+
: null;
3803+
return [...rows, buildMockChannelWindowBounds(args, hasMore, nextCursor)];
37503804
}
37513805

37523806
// Relay mode: mirror build_channel_window_filter exactly — top-level dispatch
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
import { parseChannelWindowResponse } from "@/features/messages/lib/channelWindowResponse";
4+
import type { RelayEvent } from "@/shared/api/types";
5+
6+
import { installMockBridge } from "../helpers/bridge";
7+
8+
// Focused mock-mode regression for the get_channel_window bridge handler.
9+
//
10+
// The relay-mode parity gate (parity-ancestor-island) never exercises the mock
11+
// branch (identity present). A rows-only mock branch broke every ordinary
12+
// mock-mode channel open, because parseChannelWindowResponse requires exactly
13+
// one kind-39006 bounds event and throws otherwise. This proves the mock branch
14+
// now returns a parseable page in both positions of a two-page walk, and that
15+
// the page-1/page-2 union of rows has no duplication and no loss.
16+
//
17+
// Uses the empty `random` channel (no pre-seeds) so union math is exact.
18+
const RANDOM_CHANNEL_ID = "9dae0116-799b-5071-a0a8-fdd30a91a35d";
19+
const PAGE_CAP = 50; // getChannelWindowEvents default limitRows
20+
const SEED_COUNT = 75; // > one page, so page-1 is full and page-2 holds the tail
21+
22+
type WindowInvoke = (
23+
command: string,
24+
payload?: Record<string, unknown>,
25+
) => Promise<RelayEvent[]>;
26+
27+
test("mock-mode channel window pages parse with no dup or loss across page-1/page-2", async ({
28+
page,
29+
}) => {
30+
await installMockBridge(page);
31+
await page.goto("/");
32+
await page.waitForFunction(
33+
() =>
34+
typeof window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__ === "function" &&
35+
typeof window.__BUZZ_E2E_INVOKE_MOCK_COMMAND__ === "function",
36+
);
37+
38+
// Seed SEED_COUNT top-level messages, strictly increasing created_at so their
39+
// relay order (created_at DESC, id ASC) is deterministic.
40+
await page.evaluate(
41+
({ seedCount }) => {
42+
const base = 1_700_000_000;
43+
for (let index = 0; index < seedCount; index += 1) {
44+
window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__?.({
45+
channelName: "random",
46+
content: `paging ${index}`,
47+
createdAt: base + index,
48+
});
49+
}
50+
},
51+
{ seedCount: SEED_COUNT },
52+
);
53+
54+
const invokeWindow = (payload: Record<string, unknown>) =>
55+
page.evaluate(
56+
([channelId, cursor, limitRows]) =>
57+
(window.__BUZZ_E2E_INVOKE_MOCK_COMMAND__ as unknown as WindowInvoke)(
58+
"get_channel_window",
59+
{ channelId, cursor, limitRows },
60+
),
61+
[payload.channelId, payload.cursor, payload.limitRows] as const,
62+
);
63+
64+
// Page 1: head (no cursor).
65+
const rawPage1 = await invokeWindow({
66+
channelId: RANDOM_CHANNEL_ID,
67+
cursor: null,
68+
limitRows: PAGE_CAP,
69+
});
70+
const page1 = parseChannelWindowResponse(rawPage1, RANDOM_CHANNEL_ID, null);
71+
expect(page1.rows.length).toBe(PAGE_CAP);
72+
expect(page1.hasMore).toBe(true);
73+
expect(page1.nextCursor).not.toBeNull();
74+
75+
// Page 2: feed page-1's signed cursor back verbatim as the request cursor.
76+
const cursor = page1.nextCursor;
77+
if (!cursor) throw new Error("page-1 must expose a next cursor");
78+
const rawPage2 = await invokeWindow({
79+
channelId: RANDOM_CHANNEL_ID,
80+
cursor: { created_at: cursor.createdAt, event_id: cursor.eventId },
81+
limitRows: PAGE_CAP,
82+
});
83+
const page2 = parseChannelWindowResponse(rawPage2, RANDOM_CHANNEL_ID, cursor);
84+
expect(page2.rows.length).toBe(SEED_COUNT - PAGE_CAP);
85+
expect(page2.hasMore).toBe(false);
86+
expect(page2.nextCursor).toBeNull();
87+
88+
// Union: no duplication, no loss — exactly the SEED_COUNT distinct rows.
89+
const ids = [
90+
...page1.rows.map((row) => row.event.id),
91+
...page2.rows.map((row) => row.event.id),
92+
];
93+
const unique = new Set(ids);
94+
expect(unique.size).toBe(SEED_COUNT); // no loss
95+
expect(ids.length).toBe(unique.size); // no duplication
96+
});

0 commit comments

Comments
 (0)