@1agh/maude 0.22.2 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -1
- package/cli/bin/maude.mjs +3 -0
- package/cli/commands/cache.mjs +181 -0
- package/cli/commands/cache.test.mjs +166 -0
- package/cli/commands/design-link.test.mjs +84 -2
- package/cli/commands/design.mjs +95 -4
- package/cli/commands/design.test.mjs +56 -0
- package/cli/commands/help.mjs +34 -0
- package/cli/commands/hub.mjs +255 -30
- package/cli/commands/hub.test.mjs +135 -10
- package/cli/commands/init.mjs +3 -0
- package/cli/commands/preflight.mjs +11 -0
- package/cli/commands/preflight.test.mjs +46 -0
- package/cli/commands/scenario-report.mjs +45 -0
- package/cli/lib/cache.mjs +407 -0
- package/cli/lib/cache.test.mjs +303 -0
- package/cli/lib/design-link.mjs +222 -11
- package/cli/lib/flow-design-integration.test.mjs +165 -0
- package/cli/lib/gitignore-block.mjs +90 -0
- package/cli/lib/gitignore-block.test.mjs +123 -0
- package/cli/lib/hubs-config.mjs +42 -4
- package/cli/lib/plugin-cli-reachability.test.mjs +56 -0
- package/cli/lib/preflight.mjs +41 -10
- package/package.json +8 -8
- package/plugins/design/dev-server/ai-banner.tsx +2 -2
- package/plugins/design/dev-server/annotations-context-toolbar.tsx +349 -112
- package/plugins/design/dev-server/annotations-layer.tsx +906 -137
- package/plugins/design/dev-server/api.ts +109 -4
- package/plugins/design/dev-server/bin/preflight.sh +21 -10
- package/plugins/design/dev-server/bin/prep.sh +211 -0
- package/plugins/design/dev-server/bin/scenario-report.mjs +209 -0
- package/plugins/design/dev-server/bin/screenshot.sh +18 -1
- package/plugins/design/dev-server/bin/smoke.sh +94 -11
- package/plugins/design/dev-server/build.ts +69 -3
- package/plugins/design/dev-server/canvas-comment-mount.tsx +384 -0
- package/plugins/design/dev-server/canvas-cursors.ts +125 -0
- package/plugins/design/dev-server/canvas-icons.tsx +130 -0
- package/plugins/design/dev-server/canvas-lib.tsx +47 -27
- package/plugins/design/dev-server/canvas-meta.schema.json +20 -0
- package/plugins/design/dev-server/canvas-shell.tsx +358 -245
- package/plugins/design/dev-server/client/app.jsx +191 -21
- package/plugins/design/dev-server/client/styles/3-shell.css +10 -0
- package/plugins/design/dev-server/collab/awareness-bridge.ts +77 -0
- package/plugins/design/dev-server/collab/index.ts +87 -9
- package/plugins/design/dev-server/collab/persistence.ts +34 -3
- package/plugins/design/dev-server/collab/registry.ts +131 -2
- package/plugins/design/dev-server/collab/room.ts +21 -8
- package/plugins/design/dev-server/config.schema.json +20 -0
- package/plugins/design/dev-server/context-menu.tsx +167 -23
- package/plugins/design/dev-server/context.ts +31 -0
- package/plugins/design/dev-server/contextual-toolbar.tsx +7 -7
- package/plugins/design/dev-server/dist/client.bundle.js +144 -22
- package/plugins/design/dev-server/dist/comment-mount.js +1868 -0
- package/plugins/design/dev-server/dist/styles.css +16 -0
- package/plugins/design/dev-server/dom-selection.ts +156 -0
- package/plugins/design/dev-server/equal-spacing-handles.tsx +1 -1
- package/plugins/design/dev-server/export-dialog.tsx +19 -17
- package/plugins/design/dev-server/fs-watch.ts +1 -0
- package/plugins/design/dev-server/hmr-broadcast.ts +51 -20
- package/plugins/design/dev-server/http.ts +260 -20
- package/plugins/design/dev-server/input-router.tsx +125 -64
- package/plugins/design/dev-server/participants-chrome.tsx +10 -10
- package/plugins/design/dev-server/server.ts +141 -1
- package/plugins/design/dev-server/sync/agent.ts +418 -0
- package/plugins/design/dev-server/sync/atomic-write.ts +103 -0
- package/plugins/design/dev-server/sync/codec.ts +324 -0
- package/plugins/design/dev-server/sync/connection-state.ts +203 -0
- package/plugins/design/dev-server/sync/echo-guard.ts +108 -0
- package/plugins/design/dev-server/sync/fs-mirror.ts +160 -0
- package/plugins/design/dev-server/sync/hubs-config.ts +87 -0
- package/plugins/design/dev-server/sync/index.ts +918 -0
- package/plugins/design/dev-server/sync/materialize.ts +62 -0
- package/plugins/design/dev-server/sync/migrate-seed.ts +163 -0
- package/plugins/design/dev-server/sync/origins.ts +57 -0
- package/plugins/design/dev-server/sync/projection.ts +368 -0
- package/plugins/design/dev-server/sync/status.ts +115 -0
- package/plugins/design/dev-server/sync/untrusted.ts +153 -0
- package/plugins/design/dev-server/test/_helpers.ts +6 -2
- package/plugins/design/dev-server/test/annotations-layer.test.ts +231 -0
- package/plugins/design/dev-server/test/annotations-roundtrip.test.ts +276 -0
- package/plugins/design/dev-server/test/canvas-cursors.test.ts +73 -0
- package/plugins/design/dev-server/test/canvas-origin-gate.test.ts +76 -0
- package/plugins/design/dev-server/test/collab-awareness-bridge.test.ts +223 -0
- package/plugins/design/dev-server/test/collab-reseed-guard.test.ts +78 -0
- package/plugins/design/dev-server/test/collab-room.test.ts +21 -10
- package/plugins/design/dev-server/test/comment-mount.test.ts +87 -0
- package/plugins/design/dev-server/test/csp-canvas-shell.test.ts +46 -0
- package/plugins/design/dev-server/test/fixtures/phase-20-annotations.svg +1 -0
- package/plugins/design/dev-server/test/hmr-classify.test.ts +70 -0
- package/plugins/design/dev-server/test/input-router.test.ts +21 -0
- package/plugins/design/dev-server/test/sanitize-annotation-svg.test.ts +100 -0
- package/plugins/design/dev-server/test/shared-doc-convergence.test.ts +265 -0
- package/plugins/design/dev-server/test/shared-doc-foundation.test.ts +63 -0
- package/plugins/design/dev-server/test/shared-doc-migrate.test.ts +160 -0
- package/plugins/design/dev-server/test/shared-doc-projection.test.ts +238 -0
- package/plugins/design/dev-server/test/sync-agent.test.ts +278 -0
- package/plugins/design/dev-server/test/sync-atomic-write.test.ts +63 -0
- package/plugins/design/dev-server/test/sync-codec.test.ts +165 -0
- package/plugins/design/dev-server/test/sync-connection-state.test.ts +146 -0
- package/plugins/design/dev-server/test/sync-echo-guard.test.ts +96 -0
- package/plugins/design/dev-server/test/sync-fs-mirror.test.ts +182 -0
- package/plugins/design/dev-server/test/sync-hardening.test.ts +520 -0
- package/plugins/design/dev-server/test/sync-hubs-config.test.ts +130 -0
- package/plugins/design/dev-server/test/sync-meta-codec.test.ts +123 -0
- package/plugins/design/dev-server/test/sync-runtime.test.ts +812 -0
- package/plugins/design/dev-server/test/sync-status.test.ts +80 -0
- package/plugins/design/dev-server/test/sync-untrusted.test.ts +104 -0
- package/plugins/design/dev-server/test/use-collab.test.ts +0 -0
- package/plugins/design/dev-server/test/use-tool-mode.test.tsx +38 -10
- package/plugins/design/dev-server/tool-palette.tsx +18 -16
- package/plugins/design/dev-server/undo-hud.tsx +4 -4
- package/plugins/design/dev-server/undo-stack.ts +20 -4
- package/plugins/design/dev-server/use-annotation-resize.tsx +16 -5
- package/plugins/design/dev-server/use-collab.tsx +157 -13
- package/plugins/design/dev-server/use-selection-set.tsx +12 -0
- package/plugins/design/dev-server/use-tool-mode.tsx +27 -12
- package/plugins/design/dev-server/ws.ts +40 -1
- package/plugins/design/templates/_shell.html +63 -5
- package/plugins/design/templates/canvas.tsx.template +13 -0
- package/plugins/design/templates/design-system-inspiration/SUB-AGENT-PROMPTS.md +15 -7
- package/plugins/flow/.claude-plugin/config.schema.json +5 -0
- package/plugins/flow/templates/ai-skeleton/README.md +1 -1
- package/plugins/flow/templates/ai-skeleton/gitignore +10 -0
- package/plugins/flow/templates/ai-skeleton/scenarios/README.md +3 -1
- package/plugins/flow/templates/ai-skeleton/workflows.config.json +2 -1
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// Phase 9.2 (DDR-064) Phase C — loop-free disk projection (Tasks 6 + 7).
|
|
2
|
+
//
|
|
3
|
+
// Unit tests for sync/projection.ts in isolation (no runtime / no hub). They
|
|
4
|
+
// pin the contract the shared-doc cutover relies on:
|
|
5
|
+
// - doc→file projects html/css/meta only (the collab room owns comments +
|
|
6
|
+
// annotations doc→file — no double-write);
|
|
7
|
+
// - file→doc imports all five types as a minimal DIFF (never wholesale),
|
|
8
|
+
// tagged FILE_IMPORT so it doesn't echo back to disk;
|
|
9
|
+
// - our own doc→file writes are dropped on their fs echo (hash guard);
|
|
10
|
+
// - a body file edit does NOT clobber concurrent in-doc comments (independent
|
|
11
|
+
// Y-types — the cross-type no-clobber guarantee);
|
|
12
|
+
// - an unparseable file is quarantined after 3 strikes (circuit breaker).
|
|
13
|
+
//
|
|
14
|
+
// NB: true char-level merge of CONCURRENT edits to the SAME body region is
|
|
15
|
+
// Phase 10 (structured CRDT). Here the body is opaque Y.Text + prefix/suffix
|
|
16
|
+
// diff: it preserves untouched regions, but a whole-file /design:edit that omits
|
|
17
|
+
// a concurrent browser body edit will still supersede it. Browsers edit
|
|
18
|
+
// comments/annotations (separate types), so that residual is out of Phase C
|
|
19
|
+
// scope by design.
|
|
20
|
+
|
|
21
|
+
import { describe, expect, test } from 'bun:test';
|
|
22
|
+
import * as Y from 'yjs';
|
|
23
|
+
|
|
24
|
+
import { Y_TYPES } from '../collab/persistence.ts';
|
|
25
|
+
import { MAX_HTML_BYTES, applyHtmlToDoc } from '../sync/codec.ts';
|
|
26
|
+
import { createEchoGuard, hashBytes } from '../sync/echo-guard.ts';
|
|
27
|
+
import { ORIGINS } from '../sync/origins.ts';
|
|
28
|
+
import { createDocProjection } from '../sync/projection.ts';
|
|
29
|
+
|
|
30
|
+
const enc = (s: string) => new TextEncoder().encode(s);
|
|
31
|
+
|
|
32
|
+
interface Write {
|
|
33
|
+
path: string;
|
|
34
|
+
bytes: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function makeProjection(paths: {
|
|
38
|
+
html: string;
|
|
39
|
+
comments: string;
|
|
40
|
+
annotations: string;
|
|
41
|
+
meta?: string;
|
|
42
|
+
css?: string;
|
|
43
|
+
}) {
|
|
44
|
+
const doc = new Y.Doc();
|
|
45
|
+
const writes: Write[] = [];
|
|
46
|
+
const echoGuard = createEchoGuard();
|
|
47
|
+
const projection = createDocProjection({
|
|
48
|
+
slug: 'p',
|
|
49
|
+
doc,
|
|
50
|
+
paths,
|
|
51
|
+
echoGuard,
|
|
52
|
+
flushMs: 0, // synchronous flush on the next microtask
|
|
53
|
+
writer: (path, bytes) => writes.push({ path, bytes: bytes.toString() }),
|
|
54
|
+
});
|
|
55
|
+
projection.start();
|
|
56
|
+
return { doc, writes, echoGuard, projection };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const PATHS = {
|
|
60
|
+
html: '/d/ui/screen.html',
|
|
61
|
+
comments: '/d/_comments/ui-screen.json',
|
|
62
|
+
annotations: '/d/ui-screen.annotations.svg',
|
|
63
|
+
meta: '/d/ui/screen.meta.json',
|
|
64
|
+
css: '/d/ui/screen.css',
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
68
|
+
|
|
69
|
+
describe('projection doc→file (html/css/meta only — room owns comments/annotations)', () => {
|
|
70
|
+
test('projects html / css / meta on a doc edit', async () => {
|
|
71
|
+
const { doc, writes, projection } = makeProjection(PATHS);
|
|
72
|
+
doc.getText('html').insert(0, '<button>hi</button>');
|
|
73
|
+
doc.getText('css').insert(0, '.b{color:red}');
|
|
74
|
+
// meta is stored as the canonical SHARED subset (a JSON string in Y.Text).
|
|
75
|
+
doc.getText('meta').insert(0, JSON.stringify({ title: 'Screen' }));
|
|
76
|
+
await projection.flush();
|
|
77
|
+
|
|
78
|
+
const byPath = Object.fromEntries(writes.map((w) => [w.path, w.bytes]));
|
|
79
|
+
expect(byPath[PATHS.html]).toBe('<button>hi</button>');
|
|
80
|
+
expect(byPath[PATHS.css]).toBe('.b{color:red}');
|
|
81
|
+
expect(JSON.parse(byPath[PATHS.meta])).toMatchObject({ title: 'Screen' });
|
|
82
|
+
await tick();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('does NOT write comments or annotations doc→file (the room owns those)', async () => {
|
|
86
|
+
const { doc, writes, projection } = makeProjection(PATHS);
|
|
87
|
+
doc.getArray(Y_TYPES.comments).push([{ id: 'c1', text: 'hi' }]);
|
|
88
|
+
doc.getMap(Y_TYPES.annotations).set('svg', '<svg/>');
|
|
89
|
+
await projection.flush();
|
|
90
|
+
expect(writes.find((w) => w.path === PATHS.comments)).toBeUndefined();
|
|
91
|
+
expect(writes.find((w) => w.path === PATHS.annotations)).toBeUndefined();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('never clobbers a non-empty body with an empty doc value (safe reconcile)', async () => {
|
|
95
|
+
const { writes, projection } = makeProjection(PATHS);
|
|
96
|
+
// doc html is empty → reconcile must not write an empty file over local.
|
|
97
|
+
projection.reconcile();
|
|
98
|
+
expect(writes.find((w) => w.path === PATHS.html)).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('projection file→doc (diff-import, all five types, FILE_IMPORT origin)', () => {
|
|
103
|
+
test('imports an external body edit into the doc', () => {
|
|
104
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
105
|
+
const body = '<main>edited</main>';
|
|
106
|
+
const changed = projection.applyFromFs({
|
|
107
|
+
path: PATHS.html,
|
|
108
|
+
bytes: enc(body),
|
|
109
|
+
hash: hashBytes(body),
|
|
110
|
+
});
|
|
111
|
+
expect(changed).toBe(true);
|
|
112
|
+
expect(doc.getText('html').toString()).toBe(body);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('import is tagged FILE_IMPORT (so the projector does not re-project it)', () => {
|
|
116
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
117
|
+
let lastOrigin: unknown;
|
|
118
|
+
doc.on('update', (_u: Uint8Array, origin: unknown) => {
|
|
119
|
+
lastOrigin = origin;
|
|
120
|
+
});
|
|
121
|
+
const body = '<main>x</main>';
|
|
122
|
+
projection.applyFromFs({ path: PATHS.html, bytes: enc(body), hash: hashBytes(body) });
|
|
123
|
+
expect(lastOrigin).toBe(ORIGINS.FILE_IMPORT);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('imports comments + annotations too', () => {
|
|
127
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
128
|
+
const comments = JSON.stringify([{ id: 'x', text: 'yo' }]);
|
|
129
|
+
expect(
|
|
130
|
+
projection.applyFromFs({
|
|
131
|
+
path: PATHS.comments,
|
|
132
|
+
bytes: enc(comments),
|
|
133
|
+
hash: hashBytes(comments),
|
|
134
|
+
})
|
|
135
|
+
).toBe(true);
|
|
136
|
+
expect(doc.getArray(Y_TYPES.comments).toArray()).toEqual([{ id: 'x', text: 'yo' }]);
|
|
137
|
+
|
|
138
|
+
const svg = '<svg><rect/></svg>';
|
|
139
|
+
expect(
|
|
140
|
+
projection.applyFromFs({ path: PATHS.annotations, bytes: enc(svg), hash: hashBytes(svg) })
|
|
141
|
+
).toBe(true);
|
|
142
|
+
expect(doc.getMap(Y_TYPES.annotations).get('svg')).toBe(svg);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('body import is a MINIMAL diff — preserves an untouched suffix region', () => {
|
|
146
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
147
|
+
doc.getText('html').insert(0, 'HELLO WORLD');
|
|
148
|
+
// Change only the prefix; the diff must keep " WORLD" in place (prefix/suffix
|
|
149
|
+
// elimination), not delete-all + insert-all.
|
|
150
|
+
const next = 'HOWDY WORLD';
|
|
151
|
+
projection.applyFromFs({ path: PATHS.html, bytes: enc(next), hash: hashBytes(next) });
|
|
152
|
+
expect(doc.getText('html').toString()).toBe(next);
|
|
153
|
+
// Cross-check the codec produced a minimal op (the same primitive the
|
|
154
|
+
// projector uses) — a wholesale replace would not preserve the suffix.
|
|
155
|
+
const probe = new Y.Doc();
|
|
156
|
+
probe.getText('html').insert(0, 'HELLO WORLD');
|
|
157
|
+
expect(applyHtmlToDoc(probe, next, 'x')).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('projection loop-freedom', () => {
|
|
162
|
+
test('drops the fs echo of our own doc→file write (hash guard)', async () => {
|
|
163
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
164
|
+
doc.getText('html').insert(0, '<x/>');
|
|
165
|
+
await projection.flush();
|
|
166
|
+
// The fs.watch fires for our own write — same bytes → same hash → dropped,
|
|
167
|
+
// doc unchanged, no re-import.
|
|
168
|
+
let updates = 0;
|
|
169
|
+
doc.on('update', () => {
|
|
170
|
+
updates++;
|
|
171
|
+
});
|
|
172
|
+
const changed = projection.applyFromFs({
|
|
173
|
+
path: PATHS.html,
|
|
174
|
+
bytes: enc('<x/>'),
|
|
175
|
+
hash: hashBytes('<x/>'),
|
|
176
|
+
});
|
|
177
|
+
expect(changed).toBe(false);
|
|
178
|
+
expect(updates).toBe(0);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('a body file edit does NOT clobber concurrent in-doc comments (cross-type no-clobber)', () => {
|
|
182
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
183
|
+
// Browser added a comment (lives in the comments Y.Array).
|
|
184
|
+
doc.getArray(Y_TYPES.comments).push([{ id: 'live', text: 'mine' }]);
|
|
185
|
+
// Concurrently /design:edit rewrites the whole body file.
|
|
186
|
+
const body = '<main>rewritten by design:edit</main>';
|
|
187
|
+
projection.applyFromFs({ path: PATHS.html, bytes: enc(body), hash: hashBytes(body) });
|
|
188
|
+
// Body updated; the concurrent comment SURVIVED (independent Y-type).
|
|
189
|
+
expect(doc.getText('html').toString()).toBe(body);
|
|
190
|
+
expect(doc.getArray(Y_TYPES.comments).toArray()).toEqual([{ id: 'live', text: 'mine' }]);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe('projection hub→disk hardening (security re-audit A2 + A3)', () => {
|
|
195
|
+
test('A2 — refuses to materialize an oversized (hub-pushed) body to disk', async () => {
|
|
196
|
+
const { doc, writes, projection } = makeProjection(PATHS);
|
|
197
|
+
// Simulate a hub raw-update that bypassed the codec import cap: an oversized
|
|
198
|
+
// body lands directly in the shared doc.
|
|
199
|
+
doc.getText('html').insert(0, 'x'.repeat(MAX_HTML_BYTES + 1));
|
|
200
|
+
await projection.flush();
|
|
201
|
+
// The doc→file lane must refuse it — no disk-fill DoS.
|
|
202
|
+
expect(writes.find((w) => w.path === PATHS.html)).toBeUndefined();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('A3 — a meta file with __proto__ is imported without polluting Object.prototype', () => {
|
|
206
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
207
|
+
const malicious = JSON.stringify({ __proto__: { polluted: 'yes' }, title: 'ok' });
|
|
208
|
+
projection.applyFromFs({ path: PATHS.meta, bytes: enc(malicious), hash: hashBytes(malicious) });
|
|
209
|
+
// The dangerous key was stripped at parse time — no prototype pollution and
|
|
210
|
+
// the stored shared meta carries only the safe key.
|
|
211
|
+
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
|
|
212
|
+
const stored = doc.getText('meta').toString();
|
|
213
|
+
expect(stored).not.toContain('__proto__');
|
|
214
|
+
expect(stored).not.toContain('polluted');
|
|
215
|
+
expect(stored).toContain('ok');
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('projection circuit breaker', () => {
|
|
220
|
+
test('quarantines an unparseable meta file after 3 strikes (until checksum changes)', () => {
|
|
221
|
+
const { doc, projection } = makeProjection(PATHS);
|
|
222
|
+
const broken = '{ not json';
|
|
223
|
+
const ev = { path: PATHS.meta, bytes: enc(broken), hash: hashBytes(broken) };
|
|
224
|
+
// First three attempts: parse fails → false, strikes accumulate.
|
|
225
|
+
expect(projection.applyFromFs(ev)).toBe(false);
|
|
226
|
+
expect(projection.applyFromFs(ev)).toBe(false);
|
|
227
|
+
expect(projection.applyFromFs(ev)).toBe(false);
|
|
228
|
+
// Now quarantined for that hash — still false, but short-circuited.
|
|
229
|
+
expect(projection.applyFromFs(ev)).toBe(false);
|
|
230
|
+
|
|
231
|
+
// A DIFFERENT (valid) payload clears the quarantine and applies.
|
|
232
|
+
const ok = JSON.stringify({ title: 'Fixed' });
|
|
233
|
+
expect(projection.applyFromFs({ path: PATHS.meta, bytes: enc(ok), hash: hashBytes(ok) })).toBe(
|
|
234
|
+
true
|
|
235
|
+
);
|
|
236
|
+
expect(doc.getText('meta').toString()).toContain('Fixed');
|
|
237
|
+
});
|
|
238
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Sync-agent integration tests — Phase 9 Task 4.
|
|
2
|
+
//
|
|
3
|
+
// Uses an in-memory pair of Y.Docs cross-linked via Y.applyUpdate as a stand-in
|
|
4
|
+
// for HocuspocusProvider's transport. The agent under test owns docB; docA
|
|
5
|
+
// represents "another peer talking to the hub". Updates flow:
|
|
6
|
+
//
|
|
7
|
+
// docA → encode → applyUpdate(docB) → agent observes → write to disk
|
|
8
|
+
// disk → onRead → applyFromFs → applyHtmlToDoc(docB) → encode → docA
|
|
9
|
+
//
|
|
10
|
+
// This lets us validate the whole bidi loop, echo prevention, and the
|
|
11
|
+
// 100-event stress scenario from the plan without booting a real hub.
|
|
12
|
+
|
|
13
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
|
|
17
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
18
|
+
import * as Y from 'yjs';
|
|
19
|
+
|
|
20
|
+
import { type CanvasSyncAgent, createCanvasSyncAgent } from '../sync/agent.ts';
|
|
21
|
+
import { Y_SYNC_TYPES, applyHtmlToDoc, htmlFromDoc } from '../sync/codec.ts';
|
|
22
|
+
import { createEchoGuard, hashBytes } from '../sync/echo-guard.ts';
|
|
23
|
+
import { createFsReader } from '../sync/fs-mirror.ts';
|
|
24
|
+
|
|
25
|
+
let dir: string;
|
|
26
|
+
let agent: CanvasSyncAgent;
|
|
27
|
+
let docA: Y.Doc;
|
|
28
|
+
let docB: Y.Doc;
|
|
29
|
+
|
|
30
|
+
function paths() {
|
|
31
|
+
return {
|
|
32
|
+
html: join(dir, 'screen.html'),
|
|
33
|
+
comments: join(dir, '_comments', 'screen.json'),
|
|
34
|
+
annotations: join(dir, 'screen.annotations.svg'),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
dir = mkdtempSync(join(tmpdir(), 'sync-agent-'));
|
|
40
|
+
docA = new Y.Doc();
|
|
41
|
+
docB = new Y.Doc();
|
|
42
|
+
// Mirror docA→docB and docB→docA, ignoring transient origin objects from
|
|
43
|
+
// the transport itself (we use a symbol).
|
|
44
|
+
const TRANSPORT = Symbol('transport');
|
|
45
|
+
docA.on('update', (update: Uint8Array, origin: unknown) => {
|
|
46
|
+
if (origin === TRANSPORT) return;
|
|
47
|
+
Y.applyUpdate(docB, update, TRANSPORT);
|
|
48
|
+
});
|
|
49
|
+
docB.on('update', (update: Uint8Array, origin: unknown) => {
|
|
50
|
+
if (origin === TRANSPORT) return;
|
|
51
|
+
Y.applyUpdate(docA, update, TRANSPORT);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
agent?.stop();
|
|
57
|
+
rmSync(dir, { recursive: true, force: true });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function makeAgent(extra: { adopt?: boolean; flushMs?: number } = {}): CanvasSyncAgent {
|
|
61
|
+
const a = createCanvasSyncAgent({
|
|
62
|
+
slug: 'screen',
|
|
63
|
+
doc: docB,
|
|
64
|
+
paths: paths(),
|
|
65
|
+
echoGuard: createEchoGuard(),
|
|
66
|
+
flushMs: 0, // synchronous flush; the agent uses queueMicrotask
|
|
67
|
+
...extra,
|
|
68
|
+
});
|
|
69
|
+
a.start();
|
|
70
|
+
return a;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe('CanvasSyncAgent — hub → disk (Flow B)', () => {
|
|
74
|
+
test('writes HTML to disk when other peer mutates the doc', async () => {
|
|
75
|
+
agent = makeAgent();
|
|
76
|
+
// Other peer types into docA — propagates to docB via the in-memory
|
|
77
|
+
// transport, which our agent observes.
|
|
78
|
+
applyHtmlToDoc(docA, '<button>v1</button>');
|
|
79
|
+
await agent.flush();
|
|
80
|
+
|
|
81
|
+
expect(readFileSync(paths().html, 'utf8')).toBe('<button>v1</button>');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('debounces multiple rapid peer edits into one disk write', async () => {
|
|
85
|
+
agent = makeAgent({ flushMs: 30 });
|
|
86
|
+
for (let i = 0; i < 10; i++) {
|
|
87
|
+
applyHtmlToDoc(docA, `<button>v${i}</button>`);
|
|
88
|
+
}
|
|
89
|
+
await new Promise((res) => setTimeout(res, 80));
|
|
90
|
+
|
|
91
|
+
expect(readFileSync(paths().html, 'utf8')).toBe('<button>v9</button>');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('writes comments JSON when Y.Array changes', async () => {
|
|
95
|
+
agent = makeAgent();
|
|
96
|
+
docA.transact(() => {
|
|
97
|
+
docA.getArray('comments').push([{ id: 'c1', body: 'hello' }]);
|
|
98
|
+
});
|
|
99
|
+
await agent.flush();
|
|
100
|
+
|
|
101
|
+
const written = JSON.parse(readFileSync(paths().comments, 'utf8'));
|
|
102
|
+
expect(written).toEqual([{ id: 'c1', body: 'hello' }]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('writes annotations SVG when Y.Map.svg changes', async () => {
|
|
106
|
+
agent = makeAgent();
|
|
107
|
+
docA.transact(() => {
|
|
108
|
+
docA.getMap('annotations').set('svg', '<svg>annot</svg>');
|
|
109
|
+
});
|
|
110
|
+
await agent.flush();
|
|
111
|
+
|
|
112
|
+
expect(readFileSync(paths().annotations, 'utf8')).toBe('<svg>annot</svg>');
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('CanvasSyncAgent — disk → hub (Flow A)', () => {
|
|
117
|
+
test('applies disk HTML to doc when applyFromFs is called', () => {
|
|
118
|
+
agent = makeAgent();
|
|
119
|
+
const bytes = new TextEncoder().encode('<div>local-edit</div>');
|
|
120
|
+
const changed = agent.applyFromFs({
|
|
121
|
+
path: paths().html,
|
|
122
|
+
bytes,
|
|
123
|
+
hash: hashBytes(bytes),
|
|
124
|
+
});
|
|
125
|
+
expect(changed).toBe(true);
|
|
126
|
+
expect(htmlFromDoc(docA)).toBe('<div>local-edit</div>');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('parses + applies comments JSON from disk', () => {
|
|
130
|
+
agent = makeAgent();
|
|
131
|
+
const snap = [{ id: 'c2' }];
|
|
132
|
+
const str = `${JSON.stringify(snap, null, 2)}\n`;
|
|
133
|
+
const bytes = new TextEncoder().encode(str);
|
|
134
|
+
agent.applyFromFs({ path: paths().comments, bytes, hash: hashBytes(bytes) });
|
|
135
|
+
expect(docA.getArray('comments').toArray()).toEqual(snap);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('applies annotations SVG from disk', () => {
|
|
139
|
+
agent = makeAgent();
|
|
140
|
+
const bytes = new TextEncoder().encode('<svg>x</svg>');
|
|
141
|
+
agent.applyFromFs({ path: paths().annotations, bytes, hash: hashBytes(bytes) });
|
|
142
|
+
expect(docA.getMap<string>('annotations').get('svg')).toBe('<svg>x</svg>');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('drops the fs-watch echo of its own atomic write', async () => {
|
|
146
|
+
agent = makeAgent();
|
|
147
|
+
// Peer edits → agent writes to disk → echo guard recorded.
|
|
148
|
+
applyHtmlToDoc(docA, '<button>hi</button>');
|
|
149
|
+
await agent.flush();
|
|
150
|
+
|
|
151
|
+
// Simulate the fs.watch firing for the path we just wrote.
|
|
152
|
+
const bytes = readFileSync(paths().html);
|
|
153
|
+
const u8 = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
154
|
+
const changed = agent.applyFromFs({
|
|
155
|
+
path: paths().html,
|
|
156
|
+
bytes: u8,
|
|
157
|
+
hash: hashBytes(u8),
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
expect(changed).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('CanvasSyncAgent — cold start reconciliation', () => {
|
|
165
|
+
test('hub-wins (default): overwrites disk when doc differs', async () => {
|
|
166
|
+
// Local disk has stale content.
|
|
167
|
+
writeFileSync(paths().html, '<old>');
|
|
168
|
+
// Hub state arrives via docA (propagated to docB before reconcile runs).
|
|
169
|
+
applyHtmlToDoc(docA, '<new>');
|
|
170
|
+
|
|
171
|
+
agent = makeAgent();
|
|
172
|
+
await agent.reconcile();
|
|
173
|
+
|
|
174
|
+
expect(readFileSync(paths().html, 'utf8')).toBe('<new>');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('adopt mode: pushes local disk state up to the doc', async () => {
|
|
178
|
+
writeFileSync(paths().html, '<button>local</button>');
|
|
179
|
+
// Hub has stale content.
|
|
180
|
+
applyHtmlToDoc(docA, '<old-hub>');
|
|
181
|
+
|
|
182
|
+
agent = makeAgent({ adopt: true });
|
|
183
|
+
await agent.reconcile();
|
|
184
|
+
|
|
185
|
+
expect(htmlFromDoc(docA)).toBe('<button>local</button>');
|
|
186
|
+
expect(htmlFromDoc(docB)).toBe('<button>local</button>');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('adopt mode is one-shot — second reconcile is hub-wins', async () => {
|
|
190
|
+
writeFileSync(paths().html, '<button>local-v1</button>');
|
|
191
|
+
agent = makeAgent({ adopt: true });
|
|
192
|
+
await agent.reconcile();
|
|
193
|
+
|
|
194
|
+
// Hub changes the value.
|
|
195
|
+
applyHtmlToDoc(docA, '<button>hub-v2</button>');
|
|
196
|
+
|
|
197
|
+
// Disk reverts to a stale local-only state.
|
|
198
|
+
writeFileSync(paths().html, '<button>local-v3</button>');
|
|
199
|
+
await agent.reconcile();
|
|
200
|
+
|
|
201
|
+
// Hub state won this time.
|
|
202
|
+
expect(htmlFromDoc(docB)).toBe('<button>hub-v2</button>');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('identical disk + doc states: no disk write', async () => {
|
|
206
|
+
writeFileSync(paths().html, '<button>same</button>');
|
|
207
|
+
applyHtmlToDoc(docA, '<button>same</button>');
|
|
208
|
+
|
|
209
|
+
agent = makeAgent();
|
|
210
|
+
let writes = 0;
|
|
211
|
+
const customAgent = createCanvasSyncAgent({
|
|
212
|
+
slug: 'screen',
|
|
213
|
+
doc: docB,
|
|
214
|
+
paths: paths(),
|
|
215
|
+
echoGuard: createEchoGuard(),
|
|
216
|
+
flushMs: 0,
|
|
217
|
+
writer: () => {
|
|
218
|
+
writes++;
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
customAgent.start();
|
|
222
|
+
await customAgent.reconcile();
|
|
223
|
+
customAgent.stop();
|
|
224
|
+
|
|
225
|
+
expect(writes).toBe(0);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
describe('CanvasSyncAgent — stress: 100 rapid disk writes converge with no echo loop', () => {
|
|
230
|
+
test('100-event scenario from plan validate', async () => {
|
|
231
|
+
// Wire up fs-mirror against the temp dir so this looks like the production
|
|
232
|
+
// flow: file writes → fs.watch → fs-mirror debounce → agent.applyFromFs.
|
|
233
|
+
agent = makeAgent({ flushMs: 20 });
|
|
234
|
+
|
|
235
|
+
const reader = createFsReader({
|
|
236
|
+
rootDir: dir,
|
|
237
|
+
quietMs: 15,
|
|
238
|
+
accept: (p) => p.endsWith('.html'),
|
|
239
|
+
onRead: async (evt) => {
|
|
240
|
+
agent.applyFromFs({
|
|
241
|
+
path: join(dir, evt.path),
|
|
242
|
+
bytes: evt.bytes,
|
|
243
|
+
hash: evt.hash,
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// Track how many times the doc transitioned — should be bounded, no echo
|
|
249
|
+
// loop multiplying it.
|
|
250
|
+
let docUpdates = 0;
|
|
251
|
+
docB.on('update', () => {
|
|
252
|
+
docUpdates++;
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
for (let i = 0; i < 100; i++) {
|
|
256
|
+
writeFileSync(paths().html, `<button>${i}</button>`);
|
|
257
|
+
reader.notify('screen.html');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Let everything settle: reader quiet (15ms), agent flush (20ms) + slack.
|
|
261
|
+
await new Promise((res) => setTimeout(res, 200));
|
|
262
|
+
await reader.flush();
|
|
263
|
+
await agent.flush();
|
|
264
|
+
reader.stop();
|
|
265
|
+
|
|
266
|
+
// Doc + disk + peer all converge on the last write.
|
|
267
|
+
const last = '<button>99</button>';
|
|
268
|
+
expect(htmlFromDoc(docB)).toBe(last);
|
|
269
|
+
expect(htmlFromDoc(docA)).toBe(last);
|
|
270
|
+
expect(readFileSync(paths().html, 'utf8')).toBe(last);
|
|
271
|
+
|
|
272
|
+
// Update count must be bounded — strictly < 100 transitions (debounce did
|
|
273
|
+
// its job) AND < 200 (no echo loop running 2x amplification).
|
|
274
|
+
expect(docUpdates).toBeLessThan(200);
|
|
275
|
+
// Also: it should be > 0 — we DID see real syncs.
|
|
276
|
+
expect(docUpdates).toBeGreaterThan(0);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Atomic-write unit tests — Phase 9 Task 4.
|
|
2
|
+
|
|
3
|
+
import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
8
|
+
|
|
9
|
+
import { atomicWrite } from '../sync/atomic-write.ts';
|
|
10
|
+
|
|
11
|
+
let dir: string;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
dir = mkdtempSync(join(tmpdir(), 'atomic-write-'));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
rmSync(dir, { recursive: true, force: true });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('atomicWrite', () => {
|
|
22
|
+
test('writes UTF-8 string to a new path', () => {
|
|
23
|
+
const p = join(dir, 'a.html');
|
|
24
|
+
atomicWrite(p, '<button>hi</button>');
|
|
25
|
+
expect(readFileSync(p, 'utf8')).toBe('<button>hi</button>');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('writes Uint8Array bytes verbatim', () => {
|
|
29
|
+
const p = join(dir, 'b.html');
|
|
30
|
+
const bytes = new TextEncoder().encode('<div>x</div>');
|
|
31
|
+
atomicWrite(p, bytes);
|
|
32
|
+
expect(readFileSync(p, 'utf8')).toBe('<div>x</div>');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('overwrites existing files atomically (no partial-write window)', () => {
|
|
36
|
+
const p = join(dir, 'c.html');
|
|
37
|
+
writeFileSync(p, 'old');
|
|
38
|
+
atomicWrite(p, 'new');
|
|
39
|
+
expect(readFileSync(p, 'utf8')).toBe('new');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('cleans up the .tmp sidecar after a successful write', () => {
|
|
43
|
+
const p = join(dir, 'd.html');
|
|
44
|
+
atomicWrite(p, 'final');
|
|
45
|
+
const leftovers = readdirSync(dir).filter((f) => f.startsWith('d.html.tmp.'));
|
|
46
|
+
expect(leftovers).toEqual([]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('rapid sequential writes converge on the last value', () => {
|
|
50
|
+
const p = join(dir, 'e.html');
|
|
51
|
+
for (let i = 0; i < 50; i++) {
|
|
52
|
+
atomicWrite(p, `v${i}`);
|
|
53
|
+
}
|
|
54
|
+
expect(readFileSync(p, 'utf8')).toBe('v49');
|
|
55
|
+
const leftovers = readdirSync(dir).filter((f) => f.startsWith('e.html.tmp.'));
|
|
56
|
+
expect(leftovers).toEqual([]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('returns the path it wrote', () => {
|
|
60
|
+
const p = join(dir, 'f.html');
|
|
61
|
+
expect(atomicWrite(p, 'x')).toBe(p);
|
|
62
|
+
});
|
|
63
|
+
});
|