Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

test(e2e): harden the mock matcher's cursor, keys and stream completion

Sergey Kozyrenkoe4b8001499dd
diff --git a/frontend/e2e/mocks/cassette.ts b/frontend/e2e/mocks/cassette.ts
index 6ac79dfe..94577f60 100644
--- a/frontend/e2e/mocks/cassette.ts
+++ b/frontend/e2e/mocks/cassette.ts
@@ -39,9 +39,10 @@ export interface GraphQLPayload {
export interface RestCassetteEntry extends WorldFlagged {
body?: unknown;
/**
- * Deep-subset match against the request's JSON body. Without it a path hit
- * answers success no matter what the app sent — pin it on every mutation
- * entry whose payload matters (login credentials, create/update bodies).
+ * Subset match against the request's JSON body: nested plain objects are matched
+ * recursively, but an array value is compared by full equality, not element-wise.
+ * Without it a path hit answers success no matter what the app sent — pin it on every
+ * mutation entry whose payload matters (login credentials, create/update bodies).
*/
bodySubset?: Record<string, unknown>;
/**
diff --git a/frontend/e2e/mocks/world.ts b/frontend/e2e/mocks/world.ts
index f6185676..0a742fab 100644
--- a/frontend/e2e/mocks/world.ts
+++ b/frontend/e2e/mocks/world.ts
@@ -15,8 +15,17 @@ const stableStringify = (value: unknown): string =>
: node,
) ?? 'undefined';
-const isPlainObject = (value: unknown): value is Record<string, unknown> =>
- typeof value === 'object' && value !== null && !Array.isArray(value);
+const isPlainObject = (value: unknown): value is Record<string, unknown> => {
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
+ return false;
+ }
+
+ // Only true plain objects recurse in isSubsetMatch. A Date/Map/Set/class instance has no own
+ // enumerable entries, so `every` over [] would be vacuously true and the pin would match any object.
+ const proto = Object.getPrototypeOf(value) as unknown;
+
+ return proto === Object.prototype || proto === null;
+};
const isSubsetMatch = (expected?: Record<string, unknown>, actual?: Record<string, unknown>): boolean =>
!expected ||
@@ -55,7 +64,7 @@ export class MockWorld {
*/
readonly unmatchedSubscriptions: string[] = [];
- private readonly consumed = new Map<string, { index: number; signature: string }>();
+ private readonly consumed = new Map<string, Set<number>>();
private readonly flags = new Set<string>();
private readonly flagWaiters = new Map<string, Array<() => void>>();
private readonly sockets = new Set<MockSocket>();
@@ -110,8 +119,10 @@ export class MockWorld {
);
const candidates = this.eligible(matching);
+ // Include the body in the cursor key (as the GraphQL half keys on variables): two
+ // body-differentiated sequences for one path must not reset each other's cursor.
return matching && candidates?.length
- ? this.nextEntry(`rest:${method}:${normalized}`, candidates, matching)
+ ? this.nextEntry(`rest:${method}:${normalized}:${stableStringify(body ?? {})}`, candidates, matching)
: undefined;
}
@@ -123,8 +134,10 @@ export class MockWorld {
isSubsetMatch(candidate.variables, variables),
);
+ // Key on the request's variables, not the entry's: an entry written without variables would
+ // otherwise merge every flow's subscribers onto one stream, and the second would join delta-only.
return entry
- ? { entry, streamKey: `sub:${operationName}:${JSON.stringify(entry.variables ?? {})}` }
+ ? { entry, streamKey: `sub:${operationName}:${stableStringify(variables ?? {})}` }
: undefined;
}
@@ -149,6 +162,10 @@ export class MockWorld {
if (!state.isDriving) {
state.isDriving = true;
void this.driveStream(state, entry.frames, entry.complete ?? false);
+ } else if (state.cursor >= entry.frames.length && (entry.complete ?? false)) {
+ // Late subscriber to an already-exhausted stream: no frame replay (the no-replay-after-
+ // reconnect contract), but a complete:true stream must still signal completion.
+ subscriber.complete();
}
return () => state.subscribers.delete(subscriber);
@@ -186,19 +203,19 @@ export class MockWorld {
}
/**
- * Sequenced responses: entries for the same match key are consumed in
- * order; the last one repeats. The cursor is scoped to the current eligible
- * subset — when a raised flag changes which entries are visible, carrying a
- * pre-flag cursor over would skip the new subset's leading entries.
+ * Sequenced responses: entries for one match key are served in order and the last repeats.
+ * Consumption is tracked by entry identity (its index in the unfiltered `all` list), not by a
+ * cursor into the current eligible subset — so a raised flag that grows the subset serves the
+ * newly-enabled entry next, instead of restarting at 0 and replaying an already-served one.
*/
private nextEntry<T extends WorldFlagged>(key: string, candidates: T[], all: T[]): T {
- const signature = candidates.map((candidate) => all.indexOf(candidate)).join(',');
- const state = this.consumed.get(key);
- const index = state?.signature === signature ? state.index : 0;
+ const served = this.consumed.get(key) ?? new Set<number>();
+
+ this.consumed.set(key, served);
- this.consumed.set(key, { index: index + 1, signature });
+ const entry = candidates.find((candidate) => !served.has(all.indexOf(candidate))) ?? candidates.at(-1)!;
- const entry = candidates[Math.min(index, candidates.length - 1)] as T;
+ served.add(all.indexOf(entry));
if (entry.setFlag) {
this.raiseFlag(entry.setFlag);
diff --git a/frontend/e2e/mocks/world.unit.test.ts b/frontend/e2e/mocks/world.unit.test.ts
index be98b752..dba376e5 100644
--- a/frontend/e2e/mocks/world.unit.test.ts
+++ b/frontend/e2e/mocks/world.unit.test.ts
@@ -66,6 +66,35 @@ describe('MockWorld matching', () => {
world.matchGraphQL('login', {});
expect(world.matchGraphQL('info', {})?.data).toEqual({ role: 'user' });
});
+
+ it('advances to the newly-enabled entry after a second flag, without replaying the first', () => {
+ const world = new MockWorld({
+ mutations: { f1: [{ data: {}, setFlag: 'f1' }], f2: [{ data: {}, setFlag: 'f2' }] },
+ queries: {
+ stage: [
+ { data: { stage: 'base' } },
+ { data: { stage: 'after-f1' }, whenFlag: 'f1' },
+ { data: { stage: 'after-f2' }, whenFlag: 'f2' },
+ ],
+ },
+ });
+
+ expect(world.matchGraphQL('stage', {})?.data).toEqual({ stage: 'base' });
+ world.matchGraphQL('f1', {});
+ expect(world.matchGraphQL('stage', {})?.data).toEqual({ stage: 'after-f1' });
+ world.matchGraphQL('f2', {});
+ // A cursor reset to 0 on the grown eligible subset would replay 'after-f1' here.
+ expect(world.matchGraphQL('stage', {})?.data).toEqual({ stage: 'after-f2' });
+ });
+
+ it('does not treat a non-plain-object pin (a Date) as a vacuous match', () => {
+ const world = new MockWorld({
+ mutations: { save: [{ data: { ok: true }, variables: { at: new Date('2026-01-01') } }] },
+ });
+
+ // With isPlainObject true for a Date, the pin recursed into [] own-keys and matched anything.
+ expect(world.matchGraphQL('save', { at: { anything: true } })).toBeUndefined();
+ });
});
describe('MockWorld streams', () => {
@@ -86,4 +115,21 @@ describe('MockWorld streams', () => {
expect(late).toEqual([{ data: { n: 2 } }]);
});
+
+ it('completes a late subscriber that joins after a complete:true stream has drained', async () => {
+ const world = new MockWorld({});
+ const entry: SubscriptionCassetteEntry = { complete: true, frames: [{ payload: { data: { n: 1 } } }] };
+ let firstDone = false;
+
+ world.subscribeStream('k', entry, { complete: () => (firstDone = true), next: () => {} });
+ await vi.waitFor(() => expect(firstDone).toBe(true));
+
+ const late: unknown[] = [];
+ let lateDone = false;
+
+ world.subscribeStream('k', entry, { complete: () => (lateDone = true), next: (p) => late.push(p) });
+
+ expect(lateDone).toBe(true);
+ expect(late).toEqual([]);
+ });
});