@hydranium/protocol 1.0.0-next.19 → 1.0.0-next.193

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.
Files changed (205) hide show
  1. package/README.md +35 -1
  2. package/lib/abstract-logger.d.ts.map +1 -1
  3. package/lib/abstract-logger.js.map +1 -1
  4. package/lib/client/data-connection.d.ts +157 -0
  5. package/lib/client/data-connection.d.ts.map +1 -0
  6. package/lib/client/data-connection.js +236 -0
  7. package/lib/client/data-connection.js.map +1 -0
  8. package/lib/client/data-events.d.ts +13 -1
  9. package/lib/client/data-events.d.ts.map +1 -1
  10. package/lib/client/data-events.js +21 -0
  11. package/lib/client/data-events.js.map +1 -1
  12. package/lib/client/data-port.d.ts +24 -22
  13. package/lib/client/data-port.d.ts.map +1 -1
  14. package/lib/client/data-session.d.ts +471 -81
  15. package/lib/client/data-session.d.ts.map +1 -1
  16. package/lib/client/data-session.js +737 -108
  17. package/lib/client/data-session.js.map +1 -1
  18. package/lib/client/index.d.ts +14 -9
  19. package/lib/client/index.d.ts.map +1 -1
  20. package/lib/client/index.js +14 -9
  21. package/lib/client/index.js.map +1 -1
  22. package/lib/client/message-relay.d.ts +9 -3
  23. package/lib/client/message-relay.d.ts.map +1 -1
  24. package/lib/client/message-relay.js +11 -5
  25. package/lib/client/message-relay.js.map +1 -1
  26. package/lib/client/post-message-transport.d.ts +64 -3
  27. package/lib/client/post-message-transport.d.ts.map +1 -1
  28. package/lib/client/post-message-transport.js +175 -1
  29. package/lib/client/post-message-transport.js.map +1 -1
  30. package/lib/client/rpc-connection.d.ts +149 -0
  31. package/lib/client/rpc-connection.d.ts.map +1 -0
  32. package/lib/client/rpc-connection.js +202 -0
  33. package/lib/client/rpc-connection.js.map +1 -0
  34. package/lib/client-ids.d.ts +45 -0
  35. package/lib/client-ids.d.ts.map +1 -0
  36. package/lib/client-ids.js +48 -0
  37. package/lib/client-ids.js.map +1 -0
  38. package/lib/clock.d.ts +38 -0
  39. package/lib/clock.d.ts.map +1 -1
  40. package/lib/clock.js +36 -1
  41. package/lib/clock.js.map +1 -1
  42. package/lib/data/data-protocol-methods.d.ts +4 -4
  43. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  44. package/lib/data/data-protocol-methods.js +12 -1
  45. package/lib/data/data-protocol-methods.js.map +1 -1
  46. package/lib/data/data-server-protocol.d.ts +131 -41
  47. package/lib/data/data-server-protocol.d.ts.map +1 -1
  48. package/lib/data/events.d.ts +117 -21
  49. package/lib/data/events.d.ts.map +1 -1
  50. package/lib/data/requests.d.ts +69 -11
  51. package/lib/data/requests.d.ts.map +1 -1
  52. package/lib/debouncer.d.ts.map +1 -1
  53. package/lib/debouncer.js.map +1 -1
  54. package/lib/errors.d.ts +181 -29
  55. package/lib/errors.d.ts.map +1 -1
  56. package/lib/errors.js +261 -29
  57. package/lib/errors.js.map +1 -1
  58. package/lib/glsp-save-model-actions.d.ts +50 -0
  59. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  60. package/lib/glsp-save-model-actions.js +28 -0
  61. package/lib/glsp-save-model-actions.js.map +1 -0
  62. package/lib/index.d.ts +4 -0
  63. package/lib/index.d.ts.map +1 -1
  64. package/lib/index.js +7 -0
  65. package/lib/index.js.map +1 -1
  66. package/lib/latency-collector.d.ts +8 -4
  67. package/lib/latency-collector.d.ts.map +1 -1
  68. package/lib/latency-collector.js.map +1 -1
  69. package/lib/logger.d.ts +22 -1
  70. package/lib/logger.d.ts.map +1 -1
  71. package/lib/logger.js +31 -3
  72. package/lib/logger.js.map +1 -1
  73. package/lib/messages/index.d.ts +29 -0
  74. package/lib/messages/index.d.ts.map +1 -0
  75. package/lib/messages/index.js +59 -0
  76. package/lib/messages/index.js.map +1 -0
  77. package/lib/messages/primitives.d.ts +188 -0
  78. package/lib/messages/primitives.d.ts.map +1 -0
  79. package/lib/messages/primitives.js +161 -0
  80. package/lib/messages/primitives.js.map +1 -0
  81. package/lib/model-server.d.ts +60 -13
  82. package/lib/model-server.d.ts.map +1 -1
  83. package/lib/model-server.js +4 -2
  84. package/lib/model-server.js.map +1 -1
  85. package/lib/model-service/base-version.d.ts +64 -0
  86. package/lib/model-service/base-version.d.ts.map +1 -0
  87. package/lib/model-service/base-version.js +43 -0
  88. package/lib/model-service/base-version.js.map +1 -0
  89. package/lib/model-service/index.d.ts +1 -1
  90. package/lib/model-service/index.d.ts.map +1 -1
  91. package/lib/model-service/index.js +4 -5
  92. package/lib/model-service/index.js.map +1 -1
  93. package/lib/model-service/reference-candidate.d.ts +5 -3
  94. package/lib/model-service/reference-candidate.d.ts.map +1 -1
  95. package/lib/noop-logger.d.ts.map +1 -1
  96. package/lib/noop-logger.js.map +1 -1
  97. package/lib/observable-value.js.map +1 -1
  98. package/lib/patch-merge.d.ts +35 -32
  99. package/lib/patch-merge.d.ts.map +1 -1
  100. package/lib/patch-merge.js +67 -23
  101. package/lib/patch-merge.js.map +1 -1
  102. package/lib/profile-session.d.ts +8 -4
  103. package/lib/profile-session.d.ts.map +1 -1
  104. package/lib/profile-session.js.map +1 -1
  105. package/lib/reconcile-write.d.ts +65 -0
  106. package/lib/reconcile-write.d.ts.map +1 -0
  107. package/lib/reconcile-write.js +67 -0
  108. package/lib/reconcile-write.js.map +1 -0
  109. package/lib/rpc/bind-rpc-methods.d.ts +29 -3
  110. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  111. package/lib/rpc/bind-rpc-methods.js +22 -3
  112. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  113. package/lib/rpc/create-rpc-proxy.d.ts +10 -0
  114. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  115. package/lib/rpc/create-rpc-proxy.js +12 -2
  116. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  117. package/lib/rpc/wire-prefix.js.map +1 -1
  118. package/lib/testing/catalogue-audit.d.ts +80 -0
  119. package/lib/testing/catalogue-audit.d.ts.map +1 -0
  120. package/lib/testing/catalogue-audit.js +94 -0
  121. package/lib/testing/catalogue-audit.js.map +1 -0
  122. package/lib/testing/data-doubles.d.ts +13 -12
  123. package/lib/testing/data-doubles.d.ts.map +1 -1
  124. package/lib/testing/data-doubles.js +19 -5
  125. package/lib/testing/data-doubles.js.map +1 -1
  126. package/lib/testing/fake-clock.d.ts +9 -1
  127. package/lib/testing/fake-clock.d.ts.map +1 -1
  128. package/lib/testing/fake-clock.js +54 -45
  129. package/lib/testing/fake-clock.js.map +1 -1
  130. package/lib/testing/index.d.ts +1 -0
  131. package/lib/testing/index.d.ts.map +1 -1
  132. package/lib/testing/index.js +5 -2
  133. package/lib/testing/index.js.map +1 -1
  134. package/lib/testing/node/duplex-connection.js.map +1 -1
  135. package/lib/testing/node/duplex-stream.js.map +1 -1
  136. package/lib/testing/node/index.d.ts +1 -0
  137. package/lib/testing/node/index.d.ts.map +1 -1
  138. package/lib/testing/node/index.js +2 -2
  139. package/lib/testing/node/index.js.map +1 -1
  140. package/lib/testing/node/message-port-pair.d.ts +25 -0
  141. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  142. package/lib/testing/node/message-port-pair.js +26 -0
  143. package/lib/testing/node/message-port-pair.js.map +1 -0
  144. package/lib/testing/wait-for.js.map +1 -1
  145. package/lib/tracer.d.ts.map +1 -1
  146. package/lib/tracer.js.map +1 -1
  147. package/lib/transfer-diagnostic.d.ts +33 -0
  148. package/lib/transfer-diagnostic.d.ts.map +1 -1
  149. package/lib/transfer-diagnostic.js +23 -0
  150. package/lib/transfer-diagnostic.js.map +1 -1
  151. package/lib/transfer-document.d.ts +70 -32
  152. package/lib/transfer-document.d.ts.map +1 -1
  153. package/lib/transfer-document.js +17 -9
  154. package/lib/transfer-document.js.map +1 -1
  155. package/lib/uri.d.ts.map +1 -1
  156. package/lib/uri.js.map +1 -1
  157. package/lib/util.d.ts +8 -0
  158. package/lib/util.d.ts.map +1 -1
  159. package/lib/util.js +32 -0
  160. package/lib/util.js.map +1 -1
  161. package/package.json +27 -18
  162. package/src/client/data-connection.ts +314 -0
  163. package/src/client/data-events.ts +33 -1
  164. package/src/client/data-port.ts +25 -23
  165. package/src/client/data-session.ts +945 -126
  166. package/src/client/index.ts +14 -9
  167. package/src/client/message-relay.ts +29 -7
  168. package/src/client/post-message-transport.ts +219 -4
  169. package/src/client/rpc-connection.ts +268 -0
  170. package/src/client-ids.ts +49 -0
  171. package/src/clock.ts +56 -0
  172. package/src/data/data-protocol-methods.ts +13 -4
  173. package/src/data/data-server-protocol.ts +156 -41
  174. package/src/data/events.ts +123 -21
  175. package/src/data/requests.ts +74 -11
  176. package/src/errors.ts +311 -36
  177. package/src/glsp-save-model-actions.ts +59 -0
  178. package/src/index.ts +7 -0
  179. package/src/latency-collector.ts +8 -3
  180. package/src/logger.ts +28 -2
  181. package/src/messages/index.ts +36 -0
  182. package/src/messages/primitives.ts +271 -0
  183. package/src/model-server.ts +63 -18
  184. package/src/model-service/base-version.ts +72 -0
  185. package/src/model-service/index.ts +4 -5
  186. package/src/model-service/reference-candidate.ts +5 -3
  187. package/src/patch-merge.ts +97 -42
  188. package/src/profile-session.ts +9 -4
  189. package/src/reconcile-write.ts +124 -0
  190. package/src/rpc/bind-rpc-methods.ts +49 -4
  191. package/src/rpc/create-rpc-proxy.ts +20 -2
  192. package/src/testing/catalogue-audit.ts +111 -0
  193. package/src/testing/data-doubles.ts +42 -17
  194. package/src/testing/fake-clock.ts +62 -47
  195. package/src/testing/index.ts +5 -2
  196. package/src/testing/node/index.ts +2 -2
  197. package/src/testing/node/message-port-pair.ts +40 -0
  198. package/src/transfer-diagnostic.ts +40 -0
  199. package/src/transfer-document.ts +87 -34
  200. package/src/util.ts +33 -0
  201. package/lib/model-service/args.d.ts +0 -64
  202. package/lib/model-service/args.d.ts.map +0 -1
  203. package/lib/model-service/args.js +0 -11
  204. package/lib/model-service/args.js.map +0 -1
  205. package/src/model-service/args.ts +0 -67
@@ -34,7 +34,15 @@
34
34
  import { Emitter, type MessageConnection } from 'vscode-jsonrpc';
35
35
  import type { DataPort } from '../client/data-port';
36
36
  import type { DataClientProtocol } from '../data/data-server-protocol';
37
- import type { ProjectsChangedEvent, TransferDocumentSavedEvent, TransferDocumentUpdatedEvent } from '../data/events';
37
+ import type {
38
+ ProjectsChangedEvent,
39
+ TransferDocumentDeletedEvent,
40
+ TransferDocumentDirtyChangedEvent,
41
+ TransferDocumentSavedEvent,
42
+ TransferDocumentsBuiltEvent,
43
+ TransferDocumentUpdatedEvent
44
+ } from '../data/events';
45
+ import type { ResolvedMessage } from '../messages/primitives';
38
46
  import type { Project } from '../project';
39
47
  import type { TransferDiagnostic } from '../transfer-diagnostic';
40
48
  import type { TransferElement } from '../transfer-element';
@@ -51,13 +59,6 @@ export interface FakeDataPortOptions {
51
59
  * which the consumer surfaces through {@link FakeDataPort.reported}.
52
60
  */
53
61
  connect(): MessageConnection | Promise<MessageConnection>;
54
- /**
55
- * Stable client identity. Defaults to `'fake-data-port'`, which avoids the
56
- * three sentinels the framework reserves (`'language-client'`, `'unknown'`,
57
- * `'revert-on-close'`); override it when a test needs two distinguishable
58
- * clients on one server.
59
- */
60
- clientId?: string;
61
62
  }
62
63
 
63
64
  /** A {@link DataPort} that records what passed through it. */
@@ -73,10 +74,11 @@ export interface FakeDataPort extends DataPort {
73
74
  /**
74
75
  * Every {@link DataPort.reportError} call, in order. Read from outside: this
75
76
  * is the only place a transport failure surfaces, so a test for the failure
76
- * path asserts on the `context` string here rather than on a rejection that
77
- * the consumer may legitimately swallow.
77
+ * path asserts here rather than on a rejection the consumer may legitimately
78
+ * swallow. Prefer asserting on `message.code`, which is stable, over
79
+ * `message.text`, which is the English default and may be reworded.
78
80
  */
79
- readonly reported: readonly { readonly error: unknown; readonly context: string }[];
81
+ readonly reported: readonly { readonly error: unknown; readonly message: ResolvedMessage }[];
80
82
  /**
81
83
  * Fire {@link DataPort.onDispose} — the host tearing the transport down, a
82
84
  * language-server restart being the case that forces the event to exist.
@@ -96,10 +98,9 @@ export interface FakeDataPort extends DataPort {
96
98
  */
97
99
  export function makeFakeDataPort(options: FakeDataPortOptions): FakeDataPort {
98
100
  const connections: MessageConnection[] = [];
99
- const reported: { error: unknown; context: string }[] = [];
101
+ const reported: { error: unknown; message: ResolvedMessage }[] = [];
100
102
  const disposeEmitter = new Emitter<void>();
101
103
  return {
102
- clientId: options.clientId ?? 'fake-data-port',
103
104
  connections,
104
105
  reported,
105
106
  onDispose: disposeEmitter.event,
@@ -108,8 +109,8 @@ export function makeFakeDataPort(options: FakeDataPortOptions): FakeDataPort {
108
109
  connections.push(connection);
109
110
  return connection;
110
111
  },
111
- reportError(error: unknown, context: string): void {
112
- reported.push({ error, context });
112
+ reportError(error: unknown, message: ResolvedMessage): void {
113
+ reported.push({ error, message });
113
114
  },
114
115
  fireDispose(): void {
115
116
  disposeEmitter.fire(undefined);
@@ -132,6 +133,12 @@ export interface CapturingDataClient<
132
133
  readonly updates: TransferDocumentUpdatedEvent<TTransfer, TDiagnostic>[];
133
134
  /** Every `onDocumentSaved` event, in arrival order. */
134
135
  readonly saves: TransferDocumentSavedEvent<TTransfer, TDiagnostic>[];
136
+ /** Every `onDocumentDirtyChanged` event, in arrival order. */
137
+ readonly dirtyChanges: TransferDocumentDirtyChangedEvent[];
138
+ /** Every `onDocumentDeleted` event, in arrival order. */
139
+ readonly deletions: TransferDocumentDeletedEvent[];
140
+ /** Every `onDocumentsBuilt` event, in arrival order. */
141
+ readonly builds: TransferDocumentsBuiltEvent[];
135
142
  /** Every `onProjectsChanged` event, in arrival order. */
136
143
  readonly projectsChanges: ProjectsChangedEvent<TProject>[];
137
144
  }
@@ -139,7 +146,7 @@ export interface CapturingDataClient<
139
146
  /**
140
147
  * A {@link DataClientProtocol} that records every notification it receives.
141
148
  *
142
- * Recording ALL THREE channels even when a suite reads one is deliberate: an
149
+ * Recording EVERY channel even when a suite reads one is deliberate: an
143
150
  * event delivered on the wrong channel is a real defect of the data head, and a
144
151
  * double that drops the other two turns it into silence on the one being
145
152
  * watched.
@@ -156,6 +163,9 @@ export function makeCapturingDataClient<
156
163
  >(overrides: Partial<DataClientProtocol<TTransfer, TDiagnostic, TProject>> = {}): CapturingDataClient<TTransfer, TDiagnostic, TProject> {
157
164
  const updates: TransferDocumentUpdatedEvent<TTransfer, TDiagnostic>[] = [];
158
165
  const saves: TransferDocumentSavedEvent<TTransfer, TDiagnostic>[] = [];
166
+ const dirtyChanges: TransferDocumentDirtyChangedEvent[] = [];
167
+ const deletions: TransferDocumentDeletedEvent[] = [];
168
+ const builds: TransferDocumentsBuiltEvent[] = [];
159
169
  const projectsChanges: ProjectsChangedEvent<TProject>[] = [];
160
170
  const client: DataClientProtocol<TTransfer, TDiagnostic, TProject> = {
161
171
  onDocumentUpdated:
@@ -168,11 +178,26 @@ export function makeCapturingDataClient<
168
178
  (event => {
169
179
  saves.push(event);
170
180
  }),
181
+ onDocumentDirtyChanged:
182
+ overrides.onDocumentDirtyChanged ??
183
+ (event => {
184
+ dirtyChanges.push(event);
185
+ }),
186
+ onDocumentDeleted:
187
+ overrides.onDocumentDeleted ??
188
+ (event => {
189
+ deletions.push(event);
190
+ }),
191
+ onDocumentsBuilt:
192
+ overrides.onDocumentsBuilt ??
193
+ (event => {
194
+ builds.push(event);
195
+ }),
171
196
  onProjectsChanged:
172
197
  overrides.onProjectsChanged ??
173
198
  (event => {
174
199
  projectsChanges.push(event);
175
200
  })
176
201
  };
177
- return { client, updates, saves, projectsChanges };
202
+ return { client, updates, saves, dirtyChanges, deletions, builds, projectsChanges };
178
203
  }
@@ -7,7 +7,7 @@
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
9
 
10
- import { type Clock, type Stopwatch, type Timed } from '../clock';
10
+ import { type Clock, type Stopwatch, SystemClock } from '../clock';
11
11
  import { Disposable } from '../util';
12
12
 
13
13
  /** A {@link Clock} whose time only moves when the test calls {@link FakeClock.advance}. */
@@ -17,9 +17,13 @@ export interface FakeClock extends Clock {
17
17
  * whose deadline falls within the window — in chronological order, each at
18
18
  * its own deadline (so `now()` inside a callback reads the fire instant, not
19
19
  * the advance target). Timers scheduled by a callback fire too if they fall
20
- * within the same window.
20
+ * within the same window. A {@link Clock.raceTimer} over a promise that
21
+ * settles through a chain still pending loses to the timer, so let that
22
+ * chain settle before advancing.
21
23
  */
22
24
  advance(ms: number): void;
25
+ /** How many {@link Clock.setTimer} timers have neither fired nor been disposed. */
26
+ pendingTimers(): number;
23
27
  }
24
28
 
25
29
  interface FakeTimer {
@@ -39,68 +43,79 @@ interface FakeTimer {
39
43
  *
40
44
  * Server-free and DI-free — lives in `@hydranium/protocol/testing` so every
41
45
  * head can bind it without pulling in a server package.
46
+ *
47
+ * The clock is a `SystemClock` with its time replaced, so its methods live on
48
+ * its prototype and a spread of it carries none of them. Replace one by
49
+ * assigning it onto the clock, or layer over the clock with `Object.create`.
42
50
  */
43
51
  export function makeFakeClock(options: { now?: number } = {}): FakeClock {
44
- let current = options.now ?? 0;
45
- const timers: FakeTimer[] = [];
52
+ return new VirtualClock(options.now ?? 0);
53
+ }
54
+
55
+ /**
56
+ * A `SystemClock` whose time, stopwatches and timers read one virtual axis, so
57
+ * the `measure` and `raceTimer` it inherits run on that axis too.
58
+ */
59
+ class VirtualClock extends SystemClock implements FakeClock {
60
+ protected readonly timers: FakeTimer[] = [];
46
61
 
47
- const makeStopwatch = (): Stopwatch => {
48
- const start = current;
49
- let lastLap = current;
62
+ constructor(protected current: number) {
63
+ super();
64
+ }
65
+
66
+ override now(): number {
67
+ return this.current;
68
+ }
69
+
70
+ override setTimer(callback: () => void, ms: number): Disposable {
71
+ const timer: FakeTimer = { at: this.current + ms, callback, disposed: false, fired: false };
72
+ this.timers.push(timer);
73
+ return Disposable.create(() => {
74
+ timer.disposed = true;
75
+ });
76
+ }
77
+
78
+ override stopwatch(): Stopwatch {
79
+ const read = (): number => this.current;
80
+ const start = read();
81
+ let lastLap = start;
50
82
  let stopped: number | undefined;
51
83
  return {
52
84
  get elapsedMs(): number {
53
- return (stopped ?? current) - start;
85
+ return (stopped ?? read()) - start;
54
86
  },
55
87
  lap(): number {
56
- const at = stopped ?? current;
88
+ const at = stopped ?? read();
57
89
  const split = at - lastLap;
58
90
  lastLap = at;
59
91
  return split;
60
92
  },
61
93
  stop(): number {
62
- if (stopped === undefined) {
63
- stopped = current;
64
- }
94
+ stopped ??= read();
65
95
  return stopped - start;
66
96
  }
67
97
  };
68
- };
98
+ }
69
99
 
70
- return {
71
- now: () => current,
72
- setTimer(callback: () => void, ms: number): Disposable {
73
- const timer: FakeTimer = { at: current + ms, callback, disposed: false, fired: false };
74
- timers.push(timer);
75
- return Disposable.create(() => {
76
- timer.disposed = true;
77
- });
78
- },
79
- stopwatch: makeStopwatch,
80
- measure: (<T>(callback: () => T | Promise<T>): Timed<T> | Promise<Timed<T>> => {
81
- const stopwatch = makeStopwatch();
82
- const result = callback();
83
- if (result instanceof Promise) {
84
- return (result as Promise<T>).then(value => ({ result: value, elapsedMs: stopwatch.elapsedMs }));
85
- }
86
- return { result: result as T, elapsedMs: stopwatch.elapsedMs };
87
- }) as Clock['measure'],
88
- advance(ms: number): void {
89
- const target = current + ms;
90
- // Fire due timers one at a time, re-scanning after each so a callback
91
- // that schedules a nearer timer still fires within this window.
92
- for (;;) {
93
- const next = timers
94
- .filter(timer => !timer.disposed && !timer.fired && timer.at <= target)
95
- .sort((left, right) => left.at - right.at)[0];
96
- if (!next) {
97
- break;
98
- }
99
- current = next.at;
100
- next.fired = true;
101
- next.callback();
100
+ pendingTimers(): number {
101
+ return this.timers.filter(timer => !timer.disposed && !timer.fired).length;
102
+ }
103
+
104
+ advance(ms: number): void {
105
+ const target = this.current + ms;
106
+ // Fire due timers one at a time, re-scanning after each so a callback
107
+ // that schedules a nearer timer still fires within this window.
108
+ for (;;) {
109
+ const next = this.timers
110
+ .filter(timer => !timer.disposed && !timer.fired && timer.at <= target)
111
+ .sort((left, right) => left.at - right.at)[0];
112
+ if (!next) {
113
+ break;
102
114
  }
103
- current = target;
115
+ this.current = next.at;
116
+ next.fired = true;
117
+ next.callback();
104
118
  }
105
- };
119
+ this.current = target;
120
+ }
106
121
  }
@@ -10,16 +10,19 @@
10
10
  // Subpath barrel for `@hydranium/protocol/testing` — the BROWSER-NEUTRAL shared
11
11
  // primitives: `makeFakeClock` (deterministic `Clock` double on one virtual time
12
12
  // axis), `waitFor` / `tick`, the client-side data-head doubles
13
- // (`makeFakeDataPort` / `makeCapturingDataClient`), and the `Harness` marker
13
+ // (`makeFakeDataPort` / `makeCapturingDataClient`), the translation-catalogue
14
+ // audit (`flattenCatalogue` / `findUndeclaredCodes` / `findSharedCodes`), and
15
+ // the `Harness` marker
14
16
  // interface every framework harness extends. Kept out of the main barrel so production bundles don't pull
15
17
  // the test scaffolding in by default; adopters opt in by importing from
16
18
  // `@hydranium/protocol/testing`.
17
19
  //
18
- // Neutrality is gated (`scripts/check-neutral-bundles.mjs`), which is why the
20
+ // Neutrality is gated (`scripts/check-neutral-bundles.mts`), which is why the
19
21
  // duplex transports are NOT here: a `PassThrough` pair is in their exported
20
22
  // type, so they cannot be made portable and live at `./testing/node` instead.
21
23
  // The same rule the package surface uses — the portable name is the short one.
22
24
 
25
+ export * from './catalogue-audit';
23
26
  export * from './data-doubles';
24
27
  export * from './fake-clock';
25
28
  export * from './harness';
@@ -8,8 +8,7 @@
8
8
  ********************************************************************************/
9
9
 
10
10
  // Subpath barrel for `@hydranium/protocol/testing/node` — the test transports
11
- // that need a Node runtime: a crossed `PassThrough` pair and the in-process
12
- // `MessageConnection` bridge over it.
11
+ // that need a Node runtime.
13
12
  //
14
13
  // Separate from `./testing` because a `PassThrough` is in `DuplexStreamPair`'s
15
14
  // exported type and `StreamMessageReader` comes from `vscode-jsonrpc/node`, so
@@ -20,3 +19,4 @@
20
19
 
21
20
  export * from './duplex-connection';
22
21
  export * from './duplex-stream';
22
+ export * from './message-port-pair';
@@ -0,0 +1,40 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ import { MessageChannel } from 'node:worker_threads';
11
+ import type { TransferredMessagePort } from '../../client/post-message-transport';
12
+
13
+ /**
14
+ * The two ends of a `worker_threads` `MessageChannel`, typed as the transferred
15
+ * browser ports `createMessagePortTransport` takes. Node's port clones and keeps
16
+ * order as a browser's does, which is what lets a headless test stand in for a
17
+ * worker.
18
+ *
19
+ * `dispose` closes both ports. Node reports that as a close and a browser port
20
+ * reports nothing, so a test that asserts on a close does so before teardown.
21
+ */
22
+ export interface MessagePortPair {
23
+ readonly port1: TransferredMessagePort;
24
+ readonly port2: TransferredMessagePort;
25
+ dispose(): void;
26
+ }
27
+
28
+ export function makeMessagePortPair(): MessagePortPair {
29
+ const channel = new MessageChannel();
30
+ return {
31
+ // Node's port satisfies the structural type at runtime; the cast records
32
+ // one platform's port standing in for the other's.
33
+ port1: channel.port1 as unknown as TransferredMessagePort,
34
+ port2: channel.port2 as unknown as TransferredMessagePort,
35
+ dispose(): void {
36
+ channel.port1.close();
37
+ channel.port2.close();
38
+ }
39
+ };
40
+ }
@@ -7,6 +7,8 @@
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
9
 
10
+ import { type MessageParams, type ResolvedMessage } from './messages/primitives';
11
+
10
12
  /**
11
13
  * Generic, transport-friendly diagnostic shape used by the model-server protocol.
12
14
  *
@@ -56,6 +58,21 @@ export interface TransferDiagnostic {
56
58
  * is not usable as a rule identity on its own.
57
59
  */
58
60
  code?: number | string;
61
+ /**
62
+ * Substitutions for the placeholders in the message this {@link code} names,
63
+ * present only for a diagnostic raised from a framework message declaration.
64
+ *
65
+ * Without them a translated template renders with its `{name}` tokens left
66
+ * standing, because substitution leaves an unmatched token in place rather
67
+ * than raising — so a surface that translates from {@link code} alone is
68
+ * correct for a parameterless sentence and visibly wrong for a parameterised
69
+ * one. This is the field that makes the second case work; the LSP carrier has
70
+ * always moved the params, and no carrier below it did.
71
+ *
72
+ * Prefer {@link TransferDiagnostic.resolved} over reading this: it decides
73
+ * whether an identity is present at all, which a lone `params` cannot.
74
+ */
75
+ params?: MessageParams;
59
76
  }
60
77
 
61
78
  export namespace TransferDiagnostic {
@@ -74,6 +91,29 @@ export namespace TransferDiagnostic {
74
91
  return diagnostic.type === 'parsing-error';
75
92
  }
76
93
 
94
+ /**
95
+ * The diagnostic as a renderable message, for a surface that translates.
96
+ * `undefined` when it carries no framework identity — a syntactic error, an
97
+ * adopter's own check, a linker failure — which is the case a caller must
98
+ * distinguish rather than render.
99
+ *
100
+ * Hand the result to `renderFrameworkMessage` with whatever catalogue the
101
+ * host has loaded. The `text` is the server's English, so a code the
102
+ * catalogue does not carry still yields a complete sentence.
103
+ *
104
+ * `code` alone does not establish an identity: it also holds Langium's
105
+ * internal code and an adopter's own, and either would be looked up against a
106
+ * catalogue that cannot have it. Requiring `params` is what discriminates,
107
+ * and it is why a parameterless framework message still populates the field
108
+ * with an empty object rather than omitting it.
109
+ */
110
+ export function resolved(diagnostic: TransferDiagnostic): ResolvedMessage | undefined {
111
+ if (typeof diagnostic.code !== 'string' || diagnostic.params === undefined) {
112
+ return undefined;
113
+ }
114
+ return { code: diagnostic.code, params: diagnostic.params, text: diagnostic.message };
115
+ }
116
+
77
117
  export function getPath(diagnostic: TransferDiagnostic): string {
78
118
  return diagnostic.property ? `${diagnostic.element}${ELEMENT_PROPERTY_SEPARATOR}${diagnostic.property}` : diagnostic.element;
79
119
  }
@@ -9,51 +9,93 @@
9
9
 
10
10
  import type { TransferDiagnostic } from './transfer-diagnostic';
11
11
  import type { TransferElement } from './transfer-element';
12
+ import { asModelVersion, type ModelVersion, type TextVersion } from './model-service/base-version';
13
+
14
+ /** The model a data server built for a document, encoded for the wire. */
15
+ export interface TransferModelSnapshot<TRoot extends TransferElement, TDiagnostic = TransferDiagnostic> {
16
+ root: TRoot;
17
+ /** Absent until the document is validated; `[]` once it is and nothing was found. */
18
+ diagnostics?: TDiagnostic[];
19
+ /**
20
+ * The version of the text `root` was parsed from. A caller that mutates the
21
+ * model sends this back as the `baseVersion` of its write, and the server
22
+ * rejects the write with `ConflictError` once its text has moved past it.
23
+ */
24
+ version: ModelVersion;
25
+ /**
26
+ * The data server's fingerprint of `root` and `diagnostics`, or under its
27
+ * `'text-diagnostics'` strategy of the text `root` was parsed from and
28
+ * `diagnostics`.
29
+ * Independent of `version`; an equal hash also means equally validated.
30
+ */
31
+ hash: string;
32
+ }
33
+
34
+ /** The text a data server held for a document when it sent it. */
35
+ export interface TextState {
36
+ /**
37
+ * The server's version of that text. A `TransferModelSnapshot.version` below it
38
+ * means the model was parsed from older text, and an update at this version
39
+ * or a later one follows.
40
+ */
41
+ version: TextVersion;
42
+ /**
43
+ * What `textHash` makes of the text alone: equal texts hash equal whatever
44
+ * their version or dirty state, across a revert or a server restart that
45
+ * numbers versions afresh.
46
+ */
47
+ hash: string;
48
+ /**
49
+ * Whether the text differs from the file, as the server last knew the file.
50
+ * A later change arrives as `onDocumentDirtyChanged` to a client watching
51
+ * the document.
52
+ */
53
+ dirty: boolean;
54
+ }
12
55
 
13
56
  /**
14
57
  * Wire envelope exchanged between the model-server's data-server head and
15
- * its clients. Carries a transfer-shaped root — never an AST root — plus
16
- * the document's diagnostics in the adopter's wire-diagnostic shape.
58
+ * its clients: the server's model of a document and the text it holds for it,
59
+ * each absent where the server has none.
17
60
  *
18
61
  * The constraint `TTransfer extends TransferElement` pins the type system
19
62
  * to the wire data shape (no `$container` cycles, references as strings;
20
- * see {@link TransferElement}). The paired server-internal envelope is
21
- * `AstDocument<TAst extends AstNode, TDiagnostic>` in
22
- * `@hydranium/core` — same structural shape, different generic
23
- * constraint, used at the AST layer before the `TransferEncoder`
24
- * translation produces a {@link TransferDocument}.
63
+ * see {@link TransferElement}).
25
64
  */
26
65
  export interface TransferDocument<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic> {
27
66
  uri: string;
28
67
  /**
29
- * The document version this snapshot was taken at — sourced from the
30
- * server's text-document version counter. Callers that mutate the
31
- * document subsequently pass this value back as `TransferUpdateArgs.baseVersion`
32
- * (or `TransferSaveArgs.baseVersion`) so the server can detect stale-based
33
- * updates and reject them with `ConflictError`.
34
- *
35
- * See `@hydranium/protocol#errors` for the conflict-detection contract.
36
- */
37
- version: number;
38
- /**
39
- * Absent when the document does not exist — the server answers an unknown URI
40
- * with a shaped envelope rather than an error, so absence is an ordinary
41
- * branch and not a catch. Declared required it reads as a guarantee, and
42
- * every `root.x` compiles into a throw.
68
+ * Absent when the document does not exist: the server answers an unknown
69
+ * URI with a shaped envelope rather than an error, so absence is an
70
+ * ordinary branch and not a catch.
43
71
  */
44
- root?: TTransfer;
45
- diagnostics: TDiagnostic[];
72
+ model?: TransferModelSnapshot<TTransfer, TDiagnostic>;
73
+ /** Absent when the server holds no text for the document. */
74
+ text?: TextState;
46
75
  }
47
76
 
48
77
  /**
49
- * A {@link TransferDocument} the server actually had, so `root` is present.
78
+ * A {@link TransferDocument} the server actually had, so `model` is present.
50
79
  * Narrowed to by {@link TransferDocument.isLoaded} / {@link TransferDocument.assertLoaded}.
51
80
  */
52
81
  export type LoadedTransferDocument<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic> = TransferDocument<
53
82
  TTransfer,
54
83
  TDiagnostic
55
84
  > & {
56
- root: TTransfer;
85
+ model: TransferModelSnapshot<TTransfer, TDiagnostic>;
86
+ };
87
+
88
+ /**
89
+ * What a save or a persist answers: the document, and in `persisted` the
90
+ * version of the text it wrote to the file. That can differ from
91
+ * `model.version` either way, since writes land between the build and the
92
+ * take, so a client marking a version saved takes it from here.
93
+ */
94
+ export type TransferSavedDocument<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic> = TransferDocument<
95
+ TTransfer,
96
+ TDiagnostic
97
+ > & {
98
+ persisted: { version: TextVersion };
57
99
  };
58
100
 
59
101
  export namespace TransferDocument {
@@ -64,13 +106,13 @@ export namespace TransferDocument {
64
106
  export function isLoaded<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
65
107
  document: TransferDocument<TTransfer, TDiagnostic>
66
108
  ): document is LoadedTransferDocument<TTransfer, TDiagnostic> {
67
- return document.root !== undefined;
109
+ return document.model !== undefined;
68
110
  }
69
111
 
70
112
  /**
71
- * The same document with `root` no longer optional, or a throw. For call
113
+ * The same document with `model` no longer optional, or a throw. For call
72
114
  * sites where absence is a bug rather than a branch; the whole envelope is
73
- * returned because a caller needing `root` usually needs `uri` or `version`
115
+ * returned because a caller needing `model` usually needs `uri` or `text`
74
116
  * with it.
75
117
  */
76
118
  export function assertLoaded<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
@@ -82,18 +124,29 @@ export namespace TransferDocument {
82
124
  return document;
83
125
  }
84
126
 
127
+ /** The envelope for a document the server does not have. */
128
+ export function absent<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
129
+ uri: string
130
+ ): TransferDocument<TTransfer, TDiagnostic> {
131
+ return { uri };
132
+ }
133
+
85
134
  /**
86
- * Construct a {@link TransferDocument} envelope. `diagnostics` defaults
87
- * to `[]` so test fixtures, fake protocol implementations, and the
88
- * "no diagnostics yet" code paths don't need to repeat the empty array
89
- * at every call site.
135
+ * Construct a {@link TransferDocument} envelope with a model, its version
136
+ * marked as coming from a read. `diagnostics` absent means not validated.
90
137
  */
91
138
  export function create<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
92
139
  uri: string,
93
140
  version: number,
94
141
  root: TTransfer,
95
- diagnostics: TDiagnostic[] = []
142
+ hash: string,
143
+ diagnostics?: TDiagnostic[],
144
+ text?: TextState
96
145
  ): TransferDocument<TTransfer, TDiagnostic> {
97
- return { uri, version, root, diagnostics };
146
+ return {
147
+ uri,
148
+ model: { root, version: asModelVersion(version), hash, ...(diagnostics === undefined ? {} : { diagnostics }) },
149
+ ...(text === undefined ? {} : { text })
150
+ };
98
151
  }
99
152
  }
package/src/util.ts CHANGED
@@ -248,3 +248,36 @@ export function getAt<T = unknown>(obj: unknown, path: string[]): T | undefined
248
248
  }
249
249
  return cur as T;
250
250
  }
251
+
252
+ /**
253
+ * A stable, non-cryptographic digest of `text`, equal on every host for equal
254
+ * text: the `TextState.hash` a data server stamps, so a client can
255
+ * hash text of its own to compare. An array is read as its elements joined, so
256
+ * a large text is hashed in parts without concatenating it. It tells whether a
257
+ * text changed, not who could forge one.
258
+ */
259
+ export function textHash(text: string | readonly string[]): string {
260
+ const { high, low } = cyrb53(typeof text === 'string' ? [text] : text);
261
+ return high.toString(16).padStart(8, '0') + low.toString(16).padStart(8, '0');
262
+ }
263
+
264
+ /**
265
+ * The two 32-bit words cyrb53 ends on, each unsigned, for `parts` read as one
266
+ * string. It needs no `node:*` module, so every host runs it.
267
+ */
268
+ function cyrb53(parts: readonly string[]): { readonly high: number; readonly low: number } {
269
+ let h1 = 0xdeadbeef;
270
+ let h2 = 0x41c6ce57;
271
+ for (const part of parts) {
272
+ for (let i = 0; i < part.length; i++) {
273
+ const code = part.charCodeAt(i);
274
+ h1 = Math.imul(h1 ^ code, 2654435761);
275
+ h2 = Math.imul(h2 ^ code, 1597334677);
276
+ }
277
+ }
278
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
279
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
280
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
281
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
282
+ return { high: h2 >>> 0, low: h1 >>> 0 };
283
+ }