@hydranium/conformance 1.0.0-next.24 → 1.0.0-next.242

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/lib/data/index.js CHANGED
@@ -9,26 +9,100 @@
9
9
  /**
10
10
  * The `@hydranium/conformance/data` slice — protocol conformance for the
11
11
  * data-server head. The driver port IS the protocol-native
12
- * {@link DataServerProtocol} proxy (plus the captured `onDocumentUpdated`
13
- * events), so no upstream wire-lib dep enters the kit and `DataServerHarness`
14
- * satisfies the port structurally with no adapter.
12
+ * {@link DataServerProtocol} proxy (plus the captured client notifications),
13
+ * so no upstream wire-lib dep enters the kit and `DataServerHarness` satisfies
14
+ * the port structurally with no adapter.
15
15
  */
16
16
  import assert from 'node:assert/strict';
17
- import { TransferDocument } from '@hydranium/protocol';
17
+ import { asModelVersion, FRAMEWORK_CLIENT_IDS, isConflictError, isDocumentNotOpenError, isDuplicateClientIdError, isReservedClientIdError, isSessionClosedError, ReferenceSource, SyntheticStep, TransferDocument, UNKNOWN_CLIENT_ID } from '@hydranium/protocol';
18
18
  import { waitFor } from '@hydranium/protocol/testing';
19
19
  import { resolveDeferred, resolveModel } from '../model.js';
20
- /** Client id the kit seeds/edits documents under (the originating author). */
21
- const AUTHOR = 'conformance-author';
22
- /** Client id the kit subscribes under — distinct from {@link AUTHOR} so the echo is recognisable. */
20
+ /** Client id the kit subscribes under; a watch needs no session. */
23
21
  const SUBSCRIBER = 'conformance-subscriber';
24
22
  /**
25
- * Client id for a write made BEFORE any subscription exists. Distinct from
26
- * {@link AUTHOR} so an event caused by it is recognisable: a head that fans
27
- * notifications out regardless of its subscription table is otherwise
28
- * indistinguishable from one that honours the table, since both deliver
29
- * something for the post-subscribe write.
23
+ * A fresh session id per check. A fixed one would be refused as a duplicate
24
+ * by a head whose `connect` reuses one server across checks, whenever an
25
+ * earlier check failed before ending its session.
30
26
  */
31
- const SEEDER = 'conformance-seeder';
27
+ function sessionId(label = 'conformance-session') {
28
+ return `${label}#${globalThis.crypto.randomUUID()}`;
29
+ }
30
+ /** Register a session under a fresh id labelled `label`, and answer the id. */
31
+ async function startSession(driver, label) {
32
+ const clientId = sessionId(label);
33
+ await driver.proxy.createSession({ clientId, label });
34
+ return clientId;
35
+ }
36
+ /**
37
+ * Create `model` for the session `clientId`, then open it, since a fixture may
38
+ * name a document that exists nowhere but in the kit. A failed create is
39
+ * dropped when the open succeeds, so a head whose create is broken fails only
40
+ * the create check; an open that fails reports both failures.
41
+ */
42
+ async function openOrCreate(driver, clientId, model) {
43
+ const created = await rejectionOf(driver.proxy.createModelDocument({ uri: model.uri, clientId, text: model.text }));
44
+ const opened = await rejectionOf(driver.proxy.openModelDocument({ uri: model.uri, clientId }));
45
+ if (opened !== undefined) {
46
+ throw new Error(`Could not seed ${model.uri}: ${String(opened)}; creating it failed with ${String(created)}`, { cause: opened });
47
+ }
48
+ }
49
+ /**
50
+ * Write `model` to its URI under a new session labelled `label`, and answer the
51
+ * session's id. The session keeps the document open until the driver is
52
+ * disposed: closing it would revert the document to its file, which holds
53
+ * another text or none.
54
+ */
55
+ async function seed(driver, model, label = 'conformance-seeder') {
56
+ const clientId = await startSession(driver, label);
57
+ await openOrCreate(driver, clientId, model);
58
+ await driver.proxy.updateModelDocument({ uri: model.uri, clientId, model: model.text, baseVersion: 'any' });
59
+ return clientId;
60
+ }
61
+ /** A new session labelled `label` with `uri` open, for a write after the seed; answers its id. */
62
+ async function openAs(driver, uri, label = 'conformance-author') {
63
+ const clientId = await startSession(driver, label);
64
+ await driver.proxy.openModelDocument({ uri, clientId });
65
+ return clientId;
66
+ }
67
+ /**
68
+ * Register `clientId` on `driver`, retrying while the server still refuses it
69
+ * as a duplicate, for up to `boundMs`; the last refusal is thrown.
70
+ */
71
+ async function registerWithin(driver, clientId, boundMs) {
72
+ const deadline = Date.now() + boundMs;
73
+ for (;;) {
74
+ const refusal = await rejectionOf(driver.proxy.createSession({ clientId }));
75
+ if (refusal === undefined) {
76
+ return;
77
+ }
78
+ if (!isDuplicateClientIdError(refusal) || Date.now() >= deadline) {
79
+ throw refusal;
80
+ }
81
+ await new Promise(resolveDelay => setTimeout(resolveDelay, 20));
82
+ }
83
+ }
84
+ /** A URI beside `uri` in its folder, its file name prefixed with `prefix`, so the language and project stay the fixture's. */
85
+ function siblingOf(uri, prefix) {
86
+ const slash = uri.lastIndexOf('/') + 1;
87
+ return `${uri.slice(0, slash)}${prefix}${uri.slice(slash)}`;
88
+ }
89
+ /**
90
+ * Read `uri` until `settled` holds of the answer, for up to two seconds; fail
91
+ * with `message` after that. For an effect a head may finish after its request
92
+ * answered, such as the revert of a last close. A refused read reaches
93
+ * `settled` as `undefined`.
94
+ */
95
+ async function readUntil(driver, uri, settled, message) {
96
+ const deadline = Date.now() + 2_000;
97
+ while (!settled(await driver.proxy.getModelDocument({ uri }).catch(() => undefined))) {
98
+ assert.ok(Date.now() < deadline, message);
99
+ await new Promise(resolveDelay => setTimeout(resolveDelay, 20));
100
+ }
101
+ }
102
+ /** Settle `promise` into its rejection, or `undefined` when it resolved. */
103
+ async function rejectionOf(promise) {
104
+ return promise.then(() => undefined, (error) => error);
105
+ }
32
106
  /**
33
107
  * Build the data-server check battery: server-level checks once, then the
34
108
  * grammar-bearing checks per language. Each check connects a fresh driver
@@ -36,11 +110,11 @@ const SEEDER = 'conformance-seeder';
36
110
  * unit tests; adopters call `runDataConformance`.
37
111
  *
38
112
  * `LanguageFixture.edit` is read HERE and nowhere else in the kit, and is
39
- * optional: its two checks report skipped when it is absent. See
113
+ * optional: every check that needs one reports skipped when it is absent. See
40
114
  * {@link LanguageFixture} for which slice reads which field.
41
115
  */
42
116
  export function buildDataChecks(options) {
43
- const { connect, expectsProjects } = options;
117
+ const { connect, attach, expectsProjects } = options;
44
118
  const checks = [];
45
119
  // Split from `waitForReady` so a failure names which of the two broke.
46
120
  //
@@ -98,6 +172,43 @@ export function buildDataChecks(options) {
98
172
  }
99
173
  : undefined
100
174
  });
175
+ checks.push({
176
+ title: 'createSession refuses an id already live, and frees it once the session ends',
177
+ body: async () => {
178
+ const driver = await connect();
179
+ try {
180
+ const clientId = await startSession(driver, 'conformance-session');
181
+ const duplicate = await rejectionOf(driver.proxy.createSession({ clientId }));
182
+ assert.ok(isDuplicateClientIdError(duplicate), `a second createSession under a live id was not refused: ${String(duplicate)}`);
183
+ await driver.proxy.closeSession({ clientId });
184
+ // The other half: a head that refused every id would pass the
185
+ // assertion above on its own.
186
+ await driver.proxy.createSession({ clientId });
187
+ await driver.proxy.closeSession({ clientId });
188
+ }
189
+ finally {
190
+ driver.dispose();
191
+ }
192
+ }
193
+ });
194
+ checks.push({
195
+ title: 'createSession refuses every id the framework reserves, not as a duplicate',
196
+ body: async () => {
197
+ const driver = await connect();
198
+ try {
199
+ // Every id: a head that refuses only some passes a single case. A
200
+ // reserved id never frees up, so a caller retrying on a duplicate
201
+ // has to be able to tell this refusal apart.
202
+ for (const clientId of FRAMEWORK_CLIENT_IDS) {
203
+ const refusal = await rejectionOf(driver.proxy.createSession({ clientId }));
204
+ assert.ok(isReservedClientIdError(refusal), `createSession under the reserved id ${clientId} was not refused as reserved: ${String(refusal)}`);
205
+ }
206
+ }
207
+ finally {
208
+ driver.dispose();
209
+ }
210
+ }
211
+ });
101
212
  checks.push({
102
213
  title: 'waitForReady resolves rather than hanging or rejecting',
103
214
  body: async () => {
@@ -114,7 +225,7 @@ export function buildDataChecks(options) {
114
225
  }
115
226
  });
116
227
  for (const language of options.languages) {
117
- const { valid, invalid, edit } = language;
228
+ const { valid, invalid, edit, dependent } = language;
118
229
  const tag = `[${valid.languageId}]`;
119
230
  checks.push({
120
231
  title: `getModelDocument(valid) returns a coherent envelope ${tag}`,
@@ -124,7 +235,7 @@ export function buildDataChecks(options) {
124
235
  // Resolved AFTER `connect`, which is the whole point of allowing a
125
236
  // thunk: the fixture may name a workspace `connect` just created.
126
237
  const model = resolveModel(valid);
127
- await driver.proxy.updateModelDocument({ uri: model.uri, clientId: AUTHOR, model: model.text });
238
+ await seed(driver, model);
128
239
  // `includeDiagnostics` for the same reason the invalid check
129
240
  // passes it: a synchronous read settles at the integrity-settled
130
241
  // phase, so an empty array without it can mean "validation has
@@ -135,12 +246,14 @@ export function buildDataChecks(options) {
135
246
  // a shaped-but-contentless envelope for a document it did parse
136
247
  // would pass. `$type` is the one field every TransferElement
137
248
  // carries, so it is assertable with no fixture knowledge.
138
- const { root } = TransferDocument.assertLoaded(document);
249
+ const { model: snapshot } = TransferDocument.assertLoaded(document);
250
+ const { root } = snapshot;
139
251
  assert.ok(typeof root.$type === 'string' && root.$type.length > 0, `getModelDocument(valid) returned a root with no $type: ${JSON.stringify(root)}`);
140
252
  // `version` is the conflict token every later update gates on, so
141
253
  // an envelope that omits it is unusable however good the root is.
142
- assert.ok(Number.isInteger(document.version), `getModelDocument(valid) returned a non-integer version: ${String(document.version)}`);
143
- assert.deepStrictEqual(document.diagnostics, []);
254
+ assert.ok(Number.isInteger(snapshot.version), `getModelDocument(valid) returned a non-integer version: ${String(snapshot.version)}`);
255
+ assert.strictEqual(typeof snapshot.hash, 'string', 'getModelDocument(valid) returned a model with no hash');
256
+ assert.deepStrictEqual(snapshot.diagnostics, []);
144
257
  }
145
258
  finally {
146
259
  driver.dispose();
@@ -153,21 +266,387 @@ export function buildDataChecks(options) {
153
266
  const driver = await connect();
154
267
  try {
155
268
  const model = resolveModel(invalid);
156
- await driver.proxy.updateModelDocument({ uri: model.uri, clientId: AUTHOR, model: model.text });
269
+ await seed(driver, model);
157
270
  // Diagnostics are a validation-phase product; a synchronous read settles at the
158
271
  // integrity-settled phase by default, so request validation explicitly here.
159
272
  // Safe despite `includeDiagnostics` waiting rather than forcing a build: the
160
273
  // update above has already driven this document through validation.
161
274
  const document = await driver.proxy.getModelDocument({ uri: model.uri, includeDiagnostics: true });
162
- assert.ok(document.diagnostics.length >= 1, 'getModelDocument(invalid) reported no diagnostics');
275
+ assert.ok((document.model?.diagnostics?.length ?? 0) >= 1, 'getModelDocument(invalid) reported no diagnostics');
163
276
  }
164
277
  finally {
165
278
  driver.dispose();
166
279
  }
167
280
  }
168
281
  });
169
- // Opt-in: both remaining checks need a second, observably different model
170
- // text, which only `edit` supplies.
282
+ checks.push({
283
+ title: `a diagnostic carrying a framework message code also carries its params ${tag}`,
284
+ body: async () => {
285
+ const driver = await connect();
286
+ try {
287
+ const model = resolveModel(invalid);
288
+ await seed(driver, model);
289
+ const document = await driver.proxy.getModelDocument({ uri: model.uri, includeDiagnostics: true });
290
+ // Conditional rather than fixture-driven, and deliberately so: what
291
+ // an `invalid` fixture provokes is the adopter's choice, and a
292
+ // syntactic error legitimately carries no identity at all. The
293
+ // invariant is that the identity travels WHOLE or not — a `code`
294
+ // without `params` is the half-state that renders a translated
295
+ // template with its placeholders left standing, and it is
296
+ // reachable only by overriding `toTransferDiagnostic`.
297
+ const halfIdentities = (document.model?.diagnostics ?? []).filter(diagnostic => typeof diagnostic.code === 'string' && diagnostic.code.startsWith('hydranium/') && diagnostic.params === undefined);
298
+ assert.deepStrictEqual(halfIdentities.map(diagnostic => diagnostic.code), [], 'diagnostics carry a framework message code with no params, so a translating surface cannot render them');
299
+ }
300
+ finally {
301
+ driver.dispose();
302
+ }
303
+ }
304
+ });
305
+ checks.push({
306
+ title: `a session writes only a document it has open ${tag}`,
307
+ body: async () => {
308
+ const driver = await connect();
309
+ try {
310
+ const model = resolveModel(valid);
311
+ await seed(driver, model);
312
+ const clientId = await startSession(driver, 'conformance-session');
313
+ const write = { uri: model.uri, clientId, model: model.text, baseVersion: 'any' };
314
+ const unopened = await rejectionOf(driver.proxy.updateModelDocument(write));
315
+ assert.ok(isDocumentNotOpenError(unopened), `a session wrote a document it never opened: ${String(unopened)}`);
316
+ await driver.proxy.openModelDocument({ uri: model.uri, clientId });
317
+ await driver.proxy.updateModelDocument(write);
318
+ await driver.proxy.closeModelDocument({ uri: model.uri, clientId });
319
+ const closed = await rejectionOf(driver.proxy.updateModelDocument(write));
320
+ assert.ok(isDocumentNotOpenError(closed), `a session wrote a document after closing it: ${String(closed)}`);
321
+ await driver.proxy.closeSession({ clientId });
322
+ }
323
+ finally {
324
+ driver.dispose();
325
+ }
326
+ }
327
+ });
328
+ checks.push({
329
+ title: `a session saves only a document it has open ${tag}`,
330
+ body: async () => {
331
+ const driver = await connect();
332
+ try {
333
+ const model = resolveModel(valid);
334
+ await seed(driver, model);
335
+ const clientId = await startSession(driver, 'conformance-session');
336
+ const save = { uri: model.uri, clientId, model: model.text, baseVersion: 'any' };
337
+ const unopened = await rejectionOf(driver.proxy.saveModelDocument(save));
338
+ assert.ok(isDocumentNotOpenError(unopened), `a session saved a document it never opened: ${String(unopened)}`);
339
+ // The other half: a head refusing every session save would pass
340
+ // the assertion above on its own.
341
+ await driver.proxy.openModelDocument({ uri: model.uri, clientId });
342
+ await driver.proxy.saveModelDocument(save);
343
+ await driver.proxy.closeSession({ clientId });
344
+ }
345
+ finally {
346
+ driver.dispose();
347
+ }
348
+ }
349
+ });
350
+ checks.push({
351
+ title: `a document request under an id no session was registered for fails ${tag}`,
352
+ body: async () => {
353
+ const driver = await connect();
354
+ try {
355
+ const model = resolveModel(valid);
356
+ await seed(driver, model);
357
+ const clientId = sessionId('conformance-unregistered');
358
+ // Opened or written under an id that is no session, a document
359
+ // would stay open for a client whose end nothing ever reports.
360
+ const open = await rejectionOf(driver.proxy.openModelDocument({ uri: model.uri, clientId }));
361
+ assert.ok(isSessionClosedError(open), `a document opened under an unregistered id: ${String(open)}`);
362
+ const write = await rejectionOf(driver.proxy.updateModelDocument({ uri: model.uri, clientId, model: model.text, baseVersion: 'any' }));
363
+ assert.ok(isSessionClosedError(write), `a document was written under an unregistered id: ${String(write)}`);
364
+ }
365
+ finally {
366
+ driver.dispose();
367
+ }
368
+ }
369
+ });
370
+ checks.push({
371
+ title: `ending a connection ends its sessions ${tag}`,
372
+ skipReason: attach ? undefined : 'options supply no `attach` (a second connection to the same server outlives the ended one)',
373
+ body: attach
374
+ ? async () => {
375
+ const driver = await connect();
376
+ let driverOpen = true;
377
+ try {
378
+ const model = resolveModel(valid);
379
+ await seed(driver, model);
380
+ const clientId = await startSession(driver, 'conformance-session');
381
+ await driver.proxy.openModelDocument({ uri: model.uri, clientId });
382
+ const sibling = await attach(driver);
383
+ try {
384
+ driverOpen = false;
385
+ driver.dispose();
386
+ // Refused as a duplicate while the ended connection's
387
+ // session lives on, which over a socket it does until the
388
+ // server has seen the close; so retried, within a bound.
389
+ await registerWithin(sibling, clientId, 2_000);
390
+ const write = await rejectionOf(sibling.proxy.updateModelDocument({ uri: model.uri, clientId, model: model.text, baseVersion: 'any' }));
391
+ assert.ok(isDocumentNotOpenError(write), `a document stayed open after its connection ended: ${String(write)}`);
392
+ await sibling.proxy.closeSession({ clientId });
393
+ }
394
+ finally {
395
+ sibling.dispose();
396
+ }
397
+ }
398
+ finally {
399
+ if (driverOpen) {
400
+ driver.dispose();
401
+ }
402
+ }
403
+ }
404
+ : undefined
405
+ });
406
+ checks.push({
407
+ title: `closeSession closes every document the session had open ${tag}`,
408
+ body: async () => {
409
+ const driver = await connect();
410
+ try {
411
+ const model = resolveModel(valid);
412
+ await seed(driver, model);
413
+ const clientId = await startSession(driver, 'conformance-session');
414
+ await driver.proxy.openModelDocument({ uri: model.uri, clientId });
415
+ await driver.proxy.closeSession({ clientId });
416
+ // Observed through a new session under the same id: one that
417
+ // inherited the ended session's open would write without opening.
418
+ await driver.proxy.createSession({ clientId });
419
+ const write = await rejectionOf(driver.proxy.updateModelDocument({ uri: model.uri, clientId, model: model.text, baseVersion: 'any' }));
420
+ assert.ok(isDocumentNotOpenError(write), `a document stayed open after its session ended: ${String(write)}`);
421
+ await driver.proxy.closeSession({ clientId });
422
+ }
423
+ finally {
424
+ driver.dispose();
425
+ }
426
+ }
427
+ });
428
+ checks.push({
429
+ title: `createModelDocument creates a document open for the session, and refuses one that exists ${tag}`,
430
+ body: async () => {
431
+ const driver = await connect();
432
+ try {
433
+ const model = resolveModel(valid);
434
+ await seed(driver, model);
435
+ const clientId = await startSession(driver, 'conformance-session');
436
+ const createdUri = siblingOf(model.uri, 'conformance-created-');
437
+ const created = await driver.proxy.createModelDocument({ uri: createdUri, clientId, text: model.text });
438
+ const { model: createdModel } = TransferDocument.assertLoaded(created);
439
+ // Written without an open: only the create can have opened it.
440
+ await driver.proxy.updateModelDocument({ uri: createdUri, clientId, model: model.text, baseVersion: createdModel.version });
441
+ const existing = await rejectionOf(driver.proxy.createModelDocument({ uri: model.uri, clientId, text: model.text }));
442
+ assert.ok(existing !== undefined, `createModelDocument accepted ${model.uri}, which already exists`);
443
+ await driver.proxy.closeSession({ clientId });
444
+ }
445
+ finally {
446
+ driver.dispose();
447
+ }
448
+ }
449
+ });
450
+ checks.push({
451
+ title: `a write of the invalid model answers with its diagnostics ${tag}`,
452
+ body: async () => {
453
+ const driver = await connect();
454
+ try {
455
+ const model = resolveModel(invalid);
456
+ const clientId = await startSession(driver, 'conformance-session');
457
+ await openOrCreate(driver, clientId, model);
458
+ // Another text first, so that the invalid one is a change: the
459
+ // answer to a write that changes nothing can carry the
460
+ // diagnostics of an earlier validation. This does not prove the
461
+ // answer waited for this write's validation, only that it
462
+ // carries diagnostics at all.
463
+ const firstText = resolveDeferred(valid.text);
464
+ await driver.proxy.updateModelDocument({ uri: model.uri, clientId, model: firstText, baseVersion: 'any' });
465
+ // The answer alone, with no read after it: a caller that shows
466
+ // the problems of what it wrote should not need a second
467
+ // request, and a read could find a later write's state.
468
+ const answer = await driver.proxy.updateModelDocument({ uri: model.uri, clientId, model: model.text, baseVersion: 'any' });
469
+ assert.ok((answer.model?.diagnostics?.length ?? 0) >= 1, 'writing the invalid model answered with no diagnostics');
470
+ await driver.proxy.closeSession({ clientId });
471
+ }
472
+ finally {
473
+ driver.dispose();
474
+ }
475
+ }
476
+ });
477
+ checks.push({
478
+ title: `a created document never saved leaves with its last close ${tag}`,
479
+ body: async () => {
480
+ const driver = await connect();
481
+ try {
482
+ const model = resolveModel(valid);
483
+ const uri = siblingOf(model.uri, 'conformance-unsaved-');
484
+ const clientId = await startSession(driver, 'conformance-session');
485
+ await driver.proxy.createModelDocument({ uri, clientId, text: model.text });
486
+ // Served before the close, so an envelope without a model after
487
+ // it is the close's doing and not the head's answer for every
488
+ // created document.
489
+ TransferDocument.assertLoaded(await driver.proxy.getModelDocument({ uri }));
490
+ await driver.proxy.closeSession({ clientId });
491
+ // It has no file to go back to, so no text is left to serve, and
492
+ // the protocol answers a URI with no document with an envelope
493
+ // that has no model, not with a refusal.
494
+ await readUntil(driver, uri, document => document !== undefined && !TransferDocument.isLoaded(document), `${uri}, created and never saved, was not answered with an envelope without a model after its last close`);
495
+ }
496
+ finally {
497
+ driver.dispose();
498
+ }
499
+ }
500
+ });
501
+ checks.push({
502
+ title: `a document is dirty while its text differs from its file, and clean once saved ${tag}`,
503
+ body: async () => {
504
+ const driver = await connect();
505
+ try {
506
+ const model = resolveModel(valid);
507
+ // Only `dirty` is asserted, so any second text serves.
508
+ const unsaved = resolveDeferred(invalid.text);
509
+ const uri = siblingOf(model.uri, `conformance-saved-${globalThis.crypto.randomUUID()}-`);
510
+ const clientId = await startSession(driver, 'conformance-session');
511
+ await driver.proxy.createModelDocument({ uri, clientId, text: model.text });
512
+ const saved = await driver.proxy.saveModelDocument({ uri, clientId, model: model.text, baseVersion: 'any' });
513
+ assert.strictEqual(saved.text?.dirty, false, `the save of ${uri} answered dirty: ${String(saved.text?.dirty)}, not false`);
514
+ assert.strictEqual(saved.persisted?.version, saved.text?.version, `the save of ${uri} answered it wrote v${String(saved.persisted?.version)}, not the v${String(saved.text?.version)} it holds`);
515
+ const edited = await driver.proxy.updateModelDocument({ uri, clientId, model: unsaved, baseVersion: 'any' });
516
+ assert.strictEqual(edited.text?.dirty, true, `an unsaved write of ${uri} answered dirty: ${String(edited.text?.dirty)}, not true`);
517
+ const resaved = await driver.proxy.saveModelDocument({ uri, clientId, model: unsaved, baseVersion: 'any' });
518
+ assert.strictEqual(resaved.text?.dirty, false, `the second save of ${uri} answered dirty: ${String(resaved.text?.dirty)}, not false`);
519
+ await driver.proxy.closeSession({ clientId });
520
+ }
521
+ finally {
522
+ driver.dispose();
523
+ }
524
+ }
525
+ });
526
+ checks.push({
527
+ title: `persisting writes the text a document holds as it is, and refuses a stale base version ${tag}`,
528
+ body: async () => {
529
+ const driver = await connect();
530
+ try {
531
+ const model = resolveModel(valid);
532
+ // Trailing blank lines no serialiser writes, so a head that
533
+ // persists a re-serialised model rather than the text shows.
534
+ const held = `${model.text}\n\n`;
535
+ const uri = siblingOf(model.uri, `conformance-persisted-${globalThis.crypto.randomUUID()}-`);
536
+ const writer = await startSession(driver, 'conformance-writer');
537
+ const created = await driver.proxy.createModelDocument({ uri, clientId: writer, text: model.text });
538
+ const written = await driver.proxy.updateModelDocument({ uri, clientId: writer, model: held, baseVersion: 'any' });
539
+ const persister = await openAs(driver, uri, 'conformance-persister');
540
+ const stale = await rejectionOf(driver.proxy.persistModelDocument({
541
+ uri,
542
+ clientId: persister,
543
+ baseVersion: TransferDocument.assertLoaded(created).model.version
544
+ }));
545
+ assert.ok(isConflictError(stale), `persisting ${uri} on a stale base version did not conflict: ${String(stale)}`);
546
+ const base = TransferDocument.assertLoaded(written).model.version;
547
+ const persisted = await driver.proxy.persistModelDocument({ uri, clientId: persister, baseVersion: base });
548
+ assert.strictEqual(persisted.persisted?.version, base, `the persist of ${uri} answered it wrote v${String(persisted.persisted?.version)}, not the v${base} it was based on`);
549
+ assert.strictEqual(persisted.text?.dirty, false, `the persist of ${uri} answered dirty: ${String(persisted.text?.dirty)}`);
550
+ assert.strictEqual(persisted.text?.hash, written.text?.hash, `persisting ${uri} changed the text it holds`);
551
+ // Read after the last close, which serves the file: read before
552
+ // it, the held text answers and a file written wrong passes.
553
+ await driver.proxy.closeSession({ clientId: writer });
554
+ await driver.proxy.closeSession({ clientId: persister });
555
+ const reader = await openAs(driver, uri, 'conformance-reader');
556
+ const reread = await driver.proxy.getModelDocument({ uri });
557
+ assert.strictEqual(reread.text?.hash, written.text?.hash, `the file persisted for ${uri} is not the text it held`);
558
+ await driver.proxy.closeSession({ clientId: reader });
559
+ }
560
+ finally {
561
+ driver.dispose();
562
+ }
563
+ }
564
+ });
565
+ checks.push({
566
+ title: `a document's text hash is equal for equal text and differs for different text ${tag}`,
567
+ body: async () => {
568
+ const driver = await connect();
569
+ try {
570
+ const model = resolveModel(valid);
571
+ // Only the text is compared, so any second text serves.
572
+ const other = resolveDeferred(invalid.text);
573
+ const uri = siblingOf(model.uri, `conformance-hashed-${globalThis.crypto.randomUUID()}-`);
574
+ const clientId = await startSession(driver, 'conformance-session');
575
+ const created = await driver.proxy.createModelDocument({ uri, clientId, text: model.text });
576
+ const edited = await driver.proxy.updateModelDocument({ uri, clientId, model: other, baseVersion: 'any' });
577
+ // Back to the first text at a later version: the hash follows
578
+ // the text, which is what a client compares across a revert.
579
+ const restored = await driver.proxy.updateModelDocument({ uri, clientId, model: model.text, baseVersion: 'any' });
580
+ assert.strictEqual(typeof created.text?.hash, 'string', `the create of ${uri} answered with no text hash`);
581
+ assert.notStrictEqual(edited.text?.hash, created.text?.hash, `${uri} hashed a different text alike: ${String(edited.text?.hash)}`);
582
+ assert.strictEqual(restored.text?.hash, created.text?.hash, `${uri} hashed its first text again as ${String(restored.text?.hash)}`);
583
+ const read = await driver.proxy.getModelDocument({ uri });
584
+ assert.strictEqual(read.text?.hash, restored.text?.hash, `a read of ${uri} hashed its text as ${String(read.text?.hash)}`);
585
+ const absent = await driver.proxy.getModelDocument({
586
+ uri: siblingOf(model.uri, `conformance-absent-${globalThis.crypto.randomUUID()}-`)
587
+ });
588
+ assert.strictEqual(absent.text, undefined, `an envelope with no document carried text ${JSON.stringify(absent.text)}`);
589
+ await driver.proxy.closeSession({ clientId });
590
+ }
591
+ finally {
592
+ driver.dispose();
593
+ }
594
+ }
595
+ });
596
+ // The create-dialog query, and the reason it is its own check: it is the
597
+ // only request a head receives whose URI names no file. Everything else
598
+ // in this battery addresses a document, so a head that routes purely by
599
+ // URI extension passes all of them and still cannot open a create dialog.
600
+ //
601
+ // Doubly opt-in — the fixture must name a query AND the driver must serve
602
+ // the reference surface — because either half missing means the head never
603
+ // claimed this behaviour. The reasons are reported separately so a skip
604
+ // says which half is absent.
605
+ const referenceQuery = language.referenceQuery;
606
+ const referenceSkipReason = !referenceQuery
607
+ ? 'fixture supplies no `referenceQuery` (omit it if this language has no create-element dialog)'
608
+ : undefined;
609
+ checks.push({
610
+ title: `findReferenceCandidates answers for a synthetic source at a folder URI ${tag}`,
611
+ skipReason: referenceSkipReason,
612
+ body: referenceQuery
613
+ ? async () => {
614
+ const driver = await connect();
615
+ try {
616
+ if (!driver.references) {
617
+ // Checked here rather than in `skipReason`: the driver only
618
+ // exists once `connect` has run, and skip reasons are
619
+ // computed while the battery is being planned.
620
+ return;
621
+ }
622
+ const model = resolveModel(valid);
623
+ await seed(driver, model);
624
+ // Default to the folder holding the valid model: a sibling of
625
+ // it is where a create flow would put the new file.
626
+ const folderUri = referenceQuery.folderUri
627
+ ? resolveDeferred(referenceQuery.folderUri)
628
+ : model.uri.slice(0, model.uri.lastIndexOf('/'));
629
+ assert.ok(folderUri.length > 0 && folderUri !== model.uri, `could not derive a folder URI from ${model.uri}; supply referenceQuery.folderUri`);
630
+ const candidates = await driver.references.findReferenceCandidates({
631
+ source: ReferenceSource.synthetic(folderUri, referenceQuery.type),
632
+ syntheticPath: referenceQuery.path?.map(([containerProperty, type]) => SyntheticStep.of(containerProperty, type)),
633
+ property: referenceQuery.property
634
+ });
635
+ // The expected label, not merely a non-empty array: the defect
636
+ // this guards against answers `[]`, and an empty result is
637
+ // also what a head with a genuinely empty index answers, so
638
+ // only a named candidate tells the two apart.
639
+ const labels = candidates.map(candidate => candidate.label);
640
+ assert.ok(labels.includes(referenceQuery.expectCandidate), `findReferenceCandidates at the folder ${folderUri} did not offer ${JSON.stringify(referenceQuery.expectCandidate)}; got [${labels.join(', ')}]`);
641
+ }
642
+ finally {
643
+ driver.dispose();
644
+ }
645
+ }
646
+ : undefined
647
+ });
648
+ // Opt-in: a check that needs a second, observably different model text
649
+ // takes it from `edit`, and skips without one.
171
650
  const editSkipReason = 'fixture supplies no `edit` (data-slice only; omit it if this language is not driven through the data head)';
172
651
  checks.push({
173
652
  title: `updateModelDocument applies an edit a follow-up get reflects ${tag}`,
@@ -177,10 +656,237 @@ export function buildDataChecks(options) {
177
656
  const driver = await connect();
178
657
  try {
179
658
  const model = resolveModel(valid);
180
- await driver.proxy.updateModelDocument({ uri: model.uri, clientId: AUTHOR, model: model.text });
181
- await driver.proxy.updateModelDocument({ uri: model.uri, clientId: AUTHOR, model: resolveDeferred(edit.to) });
659
+ const seeder = await seed(driver, model);
660
+ await driver.proxy.updateModelDocument({
661
+ uri: model.uri,
662
+ clientId: seeder,
663
+ model: resolveDeferred(edit.to),
664
+ baseVersion: 'any'
665
+ });
182
666
  const document = await driver.proxy.getModelDocument({ uri: model.uri });
183
- assert.ok(edit.expect(document.root), 'edit.expect(root) was false — the edit was not reflected by a follow-up get');
667
+ assert.ok(edit.expect(document.model?.root), 'edit.expect(root) was false — the edit was not reflected by a follow-up get');
668
+ }
669
+ finally {
670
+ driver.dispose();
671
+ }
672
+ }
673
+ : undefined
674
+ });
675
+ checks.push({
676
+ title: `updateModelDocument arms the conflict gate on a base version ${tag}`,
677
+ skipReason: edit ? undefined : editSkipReason,
678
+ body: edit
679
+ ? async () => {
680
+ const driver = await connect();
681
+ try {
682
+ const model = resolveModel(valid);
683
+ const seeder = await seed(driver, model);
684
+ const author = await openAs(driver, model.uri);
685
+ // The version the gate is meant to accept, read BEFORE the
686
+ // foreign edit that supersedes it.
687
+ const stale = await driver.proxy.getModelDocument({ uri: model.uri });
688
+ await driver.proxy.updateModelDocument({
689
+ uri: model.uri,
690
+ clientId: seeder,
691
+ model: resolveDeferred(edit.to),
692
+ baseVersion: 'any'
693
+ });
694
+ let rejection;
695
+ await driver.proxy
696
+ .updateModelDocument({
697
+ uri: model.uri,
698
+ clientId: author,
699
+ model: model.text,
700
+ baseVersion: TransferDocument.assertLoaded(stale).model.version
701
+ })
702
+ .catch((error) => {
703
+ rejection = error;
704
+ });
705
+ assert.ok(isConflictError(rejection), `a write based on the superseded v${String(stale.model?.version)} was not refused: ${String(rejection)}`);
706
+ // The other half, and it is not optional: a head that refused
707
+ // EVERY write would satisfy the assertion above on its own, so
708
+ // the gate has to be shown accepting a current snapshot too.
709
+ const fresh = await driver.proxy.getModelDocument({ uri: model.uri });
710
+ await driver.proxy.updateModelDocument({
711
+ uri: model.uri,
712
+ clientId: author,
713
+ model: resolveDeferred(edit.to),
714
+ baseVersion: TransferDocument.assertLoaded(fresh).model.version
715
+ });
716
+ }
717
+ finally {
718
+ driver.dispose();
719
+ }
720
+ }
721
+ : undefined
722
+ });
723
+ checks.push({
724
+ title: `the last close drops a document's unsaved text and keeps what its save wrote ${tag}`,
725
+ skipReason: edit ? undefined : editSkipReason,
726
+ body: edit
727
+ ? async () => {
728
+ const driver = await connect();
729
+ try {
730
+ const model = resolveModel(valid);
731
+ const edited = resolveDeferred(edit.to);
732
+ const uri = siblingOf(model.uri, `conformance-reverted-${globalThis.crypto.randomUUID()}-`);
733
+ const writer = await startSession(driver, 'conformance-writer');
734
+ // Created with the edit, so that going back to the created
735
+ // text instead of the saved one shows.
736
+ await driver.proxy.createModelDocument({ uri, clientId: writer, text: edited });
737
+ const saved = await driver.proxy.saveModelDocument({ uri, clientId: writer, model: model.text, baseVersion: 'any' });
738
+ assert.ok(!edit.expect(saved.model?.root), 'edit.expect holds of the valid text, so this check cannot tell the edit apart');
739
+ const reader = await openAs(driver, uri, 'conformance-reader');
740
+ await driver.proxy.updateModelDocument({ uri, clientId: writer, model: edited, baseVersion: 'any' });
741
+ // Not the last close: the reader still shows the writer's
742
+ // text. Read at once, so a head that reverts after answering
743
+ // could still pass this half.
744
+ await driver.proxy.closeSession({ clientId: writer });
745
+ const held = await driver.proxy.getModelDocument({ uri });
746
+ assert.ok(edit.expect(held.model?.root), `closing one of two sessions dropped the unsaved text of ${uri}`);
747
+ await driver.proxy.closeSession({ clientId: reader });
748
+ await readUntil(driver, uri, document => document?.model !== undefined && !edit.expect(document.model.root), `${uri} did not go back to the text its save wrote after its last close`);
749
+ }
750
+ finally {
751
+ driver.dispose();
752
+ }
753
+ }
754
+ : undefined
755
+ });
756
+ // Opt-in twice over: the cascade needs an edit to provoke it AND a second
757
+ // document that references the first to be provoked.
758
+ const cascadeSkipReason = 'fixture supplies no `dependent` (a document referencing `valid`), so no cascade can be provoked over the protocol';
759
+ checks.push({
760
+ title: `updateModelDocuments writes a set all or none ${tag}`,
761
+ skipReason: edit && dependent
762
+ ? undefined
763
+ : dependent
764
+ ? editSkipReason
765
+ : 'fixture supplies no `dependent`, the second document a set needs besides `valid`',
766
+ body: edit && dependent
767
+ ? async () => {
768
+ const driver = await connect();
769
+ try {
770
+ const model = resolveModel(valid);
771
+ const other = resolveModel(dependent);
772
+ await seed(driver, model);
773
+ await seed(driver, other);
774
+ const clientId = await startSession(driver, 'conformance-session');
775
+ const { model: first } = TransferDocument.assertLoaded(await driver.proxy.openModelDocument({ uri: model.uri, clientId }));
776
+ const { model: second } = TransferDocument.assertLoaded(await driver.proxy.openModelDocument({ uri: other.uri, clientId }));
777
+ const edited = resolveDeferred(edit.to);
778
+ // The stale member comes LAST, so a head that checks and
779
+ // applies one document at a time has applied the first.
780
+ const stale = await rejectionOf(driver.proxy.updateModelDocuments({
781
+ clientId,
782
+ updates: [
783
+ { uri: model.uri, model: edited, baseVersion: first.version },
784
+ { uri: other.uri, model: other.text, baseVersion: asModelVersion(second.version + 1) }
785
+ ]
786
+ }));
787
+ assert.ok(isConflictError(stale), `a set with a stale member was not refused: ${String(stale)}`);
788
+ const untouched = await driver.proxy.getModelDocument({ uri: model.uri });
789
+ assert.strictEqual(untouched.model?.version, first.version, 'a refused set applied its first document');
790
+ await driver.proxy.updateModelDocuments({
791
+ clientId,
792
+ updates: [
793
+ { uri: model.uri, model: edited, baseVersion: first.version },
794
+ { uri: other.uri, model: other.text, baseVersion: second.version }
795
+ ]
796
+ });
797
+ const applied = await driver.proxy.getModelDocument({ uri: model.uri });
798
+ assert.ok(edit.expect(applied.model?.root), 'edit.expect(root) was false — a current set was not applied');
799
+ await driver.proxy.closeSession({ clientId });
800
+ }
801
+ finally {
802
+ driver.dispose();
803
+ }
804
+ }
805
+ : undefined
806
+ });
807
+ checks.push({
808
+ title: `editing a document reports its unwatched dependent as built ${tag}`,
809
+ skipReason: edit && dependent ? undefined : dependent ? editSkipReason : cascadeSkipReason,
810
+ body: edit && dependent
811
+ ? async () => {
812
+ const driver = await connect();
813
+ try {
814
+ const model = resolveModel(valid);
815
+ const other = resolveModel(dependent);
816
+ await seed(driver, model);
817
+ await seed(driver, other);
818
+ // Watch ONLY the referenced document. The dependent is left
819
+ // unwatched on purpose: that is the state in which no other
820
+ // channel can report it, and the state a workspace view is in
821
+ // for every document it displays without opening.
822
+ await driver.proxy.watchModelDocument({ uri: model.uri, clientId: SUBSCRIBER });
823
+ const author = await openAs(driver, model.uri);
824
+ const before = driver.builds.length;
825
+ await driver.proxy.updateModelDocument({
826
+ uri: model.uri,
827
+ clientId: author,
828
+ model: resolveDeferred(edit.to),
829
+ baseVersion: 'any'
830
+ });
831
+ await waitFor(() => driver.builds.slice(before).some(event => event.uris.includes(other.uri)), {
832
+ message: `no onDocumentsBuilt event named ${other.uri} after editing the document it references`
833
+ });
834
+ const reported = driver.builds.slice(before).flatMap(event => [...event.uris]);
835
+ // The watched document is excluded: its watcher already heard
836
+ // about it on the update channel, and repeating it here would
837
+ // be the bandwidth the per-URI gate exists to avoid.
838
+ assert.ok(!reported.includes(model.uri), `onDocumentsBuilt named the WATCHED ${model.uri}; it is reported on the update channel instead`);
839
+ }
840
+ finally {
841
+ driver.dispose();
842
+ }
843
+ }
844
+ : undefined
845
+ });
846
+ const breakingEdit = language.breakingEdit;
847
+ checks.push({
848
+ title: `an edit that changes a watched dependent credits the dependent's event to no client ${tag}`,
849
+ skipReason: !dependent
850
+ ? cascadeSkipReason
851
+ : breakingEdit === undefined
852
+ ? 'fixture supplies no `breakingEdit` (a text for `valid` that changes what `dependent` shows)'
853
+ : undefined,
854
+ body: dependent && breakingEdit !== undefined
855
+ ? async () => {
856
+ const driver = await connect();
857
+ try {
858
+ const model = resolveModel(valid);
859
+ const other = resolveModel(dependent);
860
+ const eventsFor = (uri, from = 0) => driver.events.slice(from).filter(event => event.document.uri === uri);
861
+ await seed(driver, model);
862
+ // The dependent's seeder keeps it open: an open document is
863
+ // the one a head could wrongly credit to its opener.
864
+ await seed(driver, other);
865
+ // Watched after the seeds: a seed that changes nothing, over
866
+ // a fixture file that holds its text already, sends a watcher
867
+ // nothing to wait for.
868
+ await driver.proxy.watchModelDocument({ uri: model.uri, clientId: SUBSCRIBER });
869
+ await driver.proxy.watchModelDocument({ uri: other.uri, clientId: SUBSCRIBER });
870
+ const author = await openAs(driver, model.uri);
871
+ const before = driver.events.length;
872
+ const answer = await driver.proxy.updateModelDocument({
873
+ uri: model.uri,
874
+ clientId: author,
875
+ model: resolveDeferred(breakingEdit),
876
+ baseVersion: 'any'
877
+ });
878
+ const written = () => eventsFor(model.uri, before).find(event => event.document.model?.version === answer.model?.version);
879
+ await waitFor(() => written() !== undefined && eventsFor(other.uri, before).length > 0, {
880
+ message: `no onDocumentUpdated event for both ${model.uri} and its dependent ${other.uri} after the breaking edit`
881
+ });
882
+ assert.deepStrictEqual({ reason: written()?.reason, sourceClientId: written()?.sourceClientId }, { reason: 'changed', sourceClientId: author }, 'the written document’s event is not the author’s change');
883
+ // A recipient drops an event naming its own id as its echo, so
884
+ // a dependent's event credited to the author, or to the client
885
+ // that has the dependent open, is lost to exactly the client
886
+ // that would show it.
887
+ for (const event of eventsFor(other.uri, before)) {
888
+ assert.deepStrictEqual({ reason: event.reason, sourceClientId: event.sourceClientId }, { reason: 'rebuilt', sourceClientId: UNKNOWN_CLIENT_ID }, 'the dependent’s event is not a rebuild that names no client');
889
+ }
184
890
  }
185
891
  finally {
186
892
  driver.dispose();
@@ -196,20 +902,35 @@ export function buildDataChecks(options) {
196
902
  const driver = await connect();
197
903
  try {
198
904
  const model = resolveModel(valid);
199
- await driver.proxy.updateModelDocument({ uri: model.uri, clientId: SEEDER, model: model.text });
905
+ // The seeder's write comes BEFORE any subscription, under an id
906
+ // distinct from the author's so an event it caused is
907
+ // recognisable: a head that fans notifications out regardless
908
+ // of its subscription table is otherwise indistinguishable from
909
+ // one that honours the table, since both deliver something for
910
+ // the post-subscribe write.
911
+ const seeder = await seed(driver, model);
912
+ const author = await openAs(driver, model.uri);
200
913
  await driver.proxy.watchModelDocument({ uri: model.uri, clientId: SUBSCRIBER });
201
- await driver.proxy.updateModelDocument({ uri: model.uri, clientId: AUTHOR, model: resolveDeferred(edit.to) });
202
- await waitFor(() => driver.events.some(event => event.sourceClientId === AUTHOR), {
914
+ await driver.proxy.updateModelDocument({
915
+ uri: model.uri,
916
+ clientId: author,
917
+ model: resolveDeferred(edit.to),
918
+ baseVersion: 'any'
919
+ });
920
+ await waitFor(() => driver.events.some(event => event.sourceClientId === author), {
203
921
  message: `no onDocumentUpdated event for ${model.uri} after the post-subscription update`
204
922
  });
205
923
  const last = driver.events[driver.events.length - 1];
206
924
  assert.strictEqual(last.document.uri, model.uri);
207
- assert.strictEqual(last.sourceClientId, AUTHOR);
925
+ assert.strictEqual(last.sourceClientId, author);
926
+ // The first delivery of the author's version is a change;
927
+ // reported as rebuilt, it says the event echoes no write.
928
+ assert.strictEqual(last.reason, 'changed');
208
929
  // Nothing from before the subscription. Waiting for the
209
930
  // post-subscribe event first is what makes this provable: the
210
931
  // two notifications share one ordered connection, so a seeding
211
932
  // event that was ever going to arrive has arrived by now.
212
- assert.ok(!driver.events.some(event => event.sourceClientId === SEEDER), 'an onDocumentUpdated event arrived for the update made BEFORE watchModelDocument');
933
+ assert.ok(!driver.events.some(event => event.sourceClientId === seeder), 'an onDocumentUpdated event arrived for the update made BEFORE watchModelDocument');
213
934
  }
214
935
  finally {
215
936
  driver.dispose();