From 39981a129fcbc2c81c6c55376831d0b503c20d59 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 21 Sep 2026 22:23:19 +0000 Subject: [PATCH] test(agent-crdt): characterize malformed follower updates (#16664) ## Summary Adds focused regression evidence for the current malformed-update boundary in `FollowerDoc`. ## Changes - **What**: Covers one valid host update and one malformed Yjs update, including the thrown decode error, absent follower event, and unchanged `updatesApplied` counter. ## Review Focus This is characterization only: production error handling, retry, logging, projection, and user-facing behavior are unchanged. ## Evidence ```text $ pnpm test:unit src/workbench/extensions/agent/crdt/followerDoc.test.ts Test Files 1 passed (1) Tests 2 passed (2) $ pnpm exec oxfmt --check src/workbench/extensions/agent/crdt/followerDoc.test.ts All matched files use the correct format. Finished in 111ms on 1 files using 8 threads. $ pnpm exec eslint src/workbench/extensions/agent/crdt/followerDoc.test.ts No output; exit 0. $ pnpm typecheck $ vue-tsc --noEmit No diagnostics; exit 0. ``` --------- Co-authored-by: Alexander Brown Co-authored-by: GitHub Action (cherry picked from commit 0206eb200518e5134dca0534538cbbad0ddf8f49) --- .../extensions/agent/crdt/followerDoc.test.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/workbench/extensions/agent/crdt/followerDoc.test.ts diff --git a/src/workbench/extensions/agent/crdt/followerDoc.test.ts b/src/workbench/extensions/agent/crdt/followerDoc.test.ts new file mode 100644 index 00000000000..62e022ba952 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/followerDoc.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it, vi } from 'vitest' +import * as Y from 'yjs' + +import { FollowerDoc } from './followerDoc' + +describe('FollowerDoc.applyRemoteUpdate', () => { + it('applies a valid host update', () => { + const host = new Y.Doc() + host.getMap('nodes').set('1', { type: 'LoadImage' }) + const update = Y.encodeStateAsUpdate(host) + const follower = new FollowerDoc() + const onUpdate = vi.fn() + follower.addEventListener('update', onUpdate) + + follower.applyRemoteUpdate(update) + + expect(follower.doc.getMap('nodes').toJSON()).toEqual({ + '1': { type: 'LoadImage' } + }) + expect(follower.updatesApplied).toBe(1) + expect(onUpdate).toHaveBeenCalledOnce() + }) + + it('throws for a truncated update without dispatching an event or incrementing updatesApplied', () => { + const follower = new FollowerDoc() + const onUpdate = vi.fn() + follower.addEventListener('update', onUpdate) + + expect(() => follower.applyRemoteUpdate(Uint8Array.of(0))).toThrow( + 'Unexpected end of array' + ) + + expect(onUpdate).not.toHaveBeenCalled() + expect(follower.updatesApplied).toBe(0) + }) +})