castle-web-cli 0.4.230 → 0.4.232

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 (92) hide show
  1. package/dist/castle-host/host.js +81 -0
  2. package/dist/devSaveStorage.d.ts +36 -0
  3. package/dist/devSaveStorage.js +469 -0
  4. package/dist/devSessionChild.js +22 -12
  5. package/dist/devSessionServer.js +8 -2
  6. package/dist/devSessionStorage.d.ts +9 -0
  7. package/dist/devSessionStorage.js +66 -1
  8. package/dist/init.js +1 -1
  9. package/dist/serve.js +5 -1
  10. package/dist/serverBundle.js +5 -3
  11. package/kits/base/castle.json +1 -1
  12. package/kits/base/sdk/README.md +148 -1
  13. package/kits/base/sdk/castle.d.ts +4 -0
  14. package/kits/base/sdk/castle.js +2 -0
  15. package/kits/base/sdk/commands.d.ts +15 -0
  16. package/kits/base/sdk/multiplayer.js +52 -4
  17. package/kits/base/sdk/save.d.ts +197 -0
  18. package/kits/base/sdk/save.js +525 -0
  19. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  20. package/kits/base/sdk/saveCoordinator.js +252 -0
  21. package/kits/base/sdk/server/storage.d.ts +3 -2
  22. package/kits/base/sdk/server/storage.js +25 -5
  23. package/kits/base/sdk/server/wrapper.d.ts +4 -0
  24. package/kits/base/sdk/server/wrapper.js +2 -0
  25. package/kits/base/sdk/store.d.ts +3 -2
  26. package/kits/base/sdk/store.js +5 -0
  27. package/kits/hub-game/CLAUDE.md +29 -4
  28. package/kits/hub-game/code/client/avatar.js +39 -0
  29. package/kits/hub-game/code/client/chat.js +12 -0
  30. package/kits/hub-game/code/client/chrome.js +237 -13
  31. package/kits/hub-game/code/client/grab.js +166 -7
  32. package/kits/hub-game/code/client/hats.js +26 -0
  33. package/kits/hub-game/code/client/hookLayer.js +36 -3
  34. package/kits/hub-game/code/client/hudClear.js +126 -0
  35. package/kits/hub-game/code/client/state.js +35 -1
  36. package/kits/hub-game/code/client/talk.js +10 -36
  37. package/kits/hub-game/code/client/tiers.js +505 -0
  38. package/kits/hub-game/code/client/together.js +144 -4
  39. package/kits/hub-game/code/client/wear.js +96 -14
  40. package/kits/hub-game/code/server/actors.js +5 -0
  41. package/kits/hub-game/code/server/grab.js +299 -13
  42. package/kits/hub-game/code/server/hub.js +162 -7
  43. package/kits/hub-game/code/server/manifest.js +80 -8
  44. package/kits/hub-game/code/server/npcs.js +21 -33
  45. package/kits/hub-game/code/server/party.js +75 -6
  46. package/kits/hub-game/code/server/say.js +8 -2
  47. package/kits/hub-game/code/server/wear.js +116 -11
  48. package/kits/hub-game/code/systems/hub.js +386 -21
  49. package/kits/hub-game/docs/contract.md +285 -19
  50. package/kits/hub-game/package.json +1 -1
  51. package/kits/hub-game/scripts/test-chat.mjs +32 -0
  52. package/kits/hub-game/scripts/test-grab-client.mjs +511 -0
  53. package/kits/hub-game/scripts/test-grab.mjs +857 -3
  54. package/kits/hub-game/scripts/test-hats.mjs +229 -0
  55. package/kits/hub-game/scripts/test-hub-trip.mjs +721 -0
  56. package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
  57. package/kits/hub-game/scripts/test-manifest.mjs +122 -1
  58. package/kits/hub-game/scripts/test-npcs.mjs +34 -24
  59. package/kits/hub-game/scripts/test-party.mjs +108 -4
  60. package/kits/hub-game/scripts/test-say.mjs +59 -2
  61. package/kits/hub-game/scripts/test-tiers.mjs +854 -0
  62. package/kits/hub-game/scripts/test-together.mjs +33 -1
  63. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  64. package/kits/physics-2d/CLAUDE.md +2 -0
  65. package/kits/physics-2d/castle.json +2 -2
  66. package/kits/physics-2d/engine/scene.js +127 -0
  67. package/kits/physics-2d/package.json +1 -1
  68. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +298 -0
  69. package/kits/physics-3d/CLAUDE.md +531 -0
  70. package/kits/physics-3d/behaviors/Player.jsx +437 -3
  71. package/kits/physics-3d/castle.json +3 -3
  72. package/kits/physics-3d/docs/pxmodel-format.md +4 -0
  73. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -1
  74. package/kits/physics-3d/engine3d/actornode.js +253 -0
  75. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  76. package/kits/physics-3d/engine3d/optimize.js +49 -0
  77. package/kits/physics-3d/engine3d/orbitinput.js +277 -0
  78. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  79. package/kits/physics-3d/engine3d/world3d.js +525 -11
  80. package/kits/physics-3d/package.json +1 -1
  81. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  82. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  83. package/kits/physics-3d/scripts/test-camera-follow.mjs +543 -0
  84. package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
  85. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  86. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  87. package/kits/physics-3d/scripts/test-static-actors.mjs +667 -0
  88. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  89. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  90. package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
  91. package/kits/physics-3d/systems/physics3d.js +273 -9
  92. package/package.json +1 -1
@@ -0,0 +1,676 @@
1
+ // Tests over a clear HUD (contract v1.26, a place's `hud: { clear: true }`):
2
+ // the sizes and geometry (`code/client/hudClear.js`), and the chrome that
3
+ // draws by them (`code/client/chrome.js`) over a small stand-in for the DOM.
4
+ // A place that does not ask is written to exactly as it was: the same styles,
5
+ // positions and order of writes, whether the chrome was never told, told no,
6
+ // or told no after a place that asked. `npm run check` runs it.
7
+
8
+ import assert from 'node:assert/strict';
9
+ import {
10
+ BIG_MIN_PX,
11
+ BIG_PX,
12
+ BIG_WITH_TEXT_PX,
13
+ BIG_WITH_TEXT_SHORT_PX,
14
+ bigEms,
15
+ boxAt,
16
+ fitPx,
17
+ HUD_GAP,
18
+ hudLayout,
19
+ pushBelow,
20
+ SHORT_HUD_SHARE,
21
+ } from '../code/client/hudClear.js';
22
+
23
+ let n = 0;
24
+ function test(name, fn) {
25
+ fn();
26
+ n += 1;
27
+ }
28
+
29
+ // --- the sizes ---------------------------------------------------------
30
+
31
+ const PHONE = { viewW: 360, viewH: 740 };
32
+ const SIDEWAYS = { viewW: 740, viewH: 360 };
33
+
34
+ test('a big line alone keeps 56 px, on any view', () => {
35
+ for (const view of [PHONE, SIDEWAYS, { viewW: 1280, viewH: 800 }]) {
36
+ for (const text of [undefined, '', null]) {
37
+ const layout = hudLayout({ big: 'FINISH', text, ...view });
38
+ assert.equal(layout.bigPx, BIG_PX);
39
+ assert.equal(layout.bigOneLine, false);
40
+ assert.equal(layout.bigMaxWidth, null);
41
+ }
42
+ }
43
+ assert.equal(BIG_PX, 56);
44
+ });
45
+
46
+ test('a big line over a text line is clearly smaller: 32 px, 28 on a short view', () => {
47
+ const tall = hudLayout({ big: '3', text: 'get ready', ...PHONE });
48
+ assert.equal(tall.bigPx, BIG_WITH_TEXT_PX);
49
+ assert.ok(tall.bigPx >= 30 && tall.bigPx <= 34);
50
+ assert.equal(tall.bigOneLine, true);
51
+ assert.equal(tall.textLines, null);
52
+ assert.equal(tall.maxHeight, null);
53
+ const short = hudLayout({ big: '3', text: 'get ready', ...SIDEWAYS });
54
+ assert.equal(short.bigPx, BIG_WITH_TEXT_SHORT_PX);
55
+ // The words every playtest game puts there fit at the full smaller size.
56
+ for (const big of ['12.3', '1:23.4', 'YOU WIN', 'YOU LOSE', 'WINNER', 'FINISH', 'GOAL!', 'TIME', '3 / 5', 'OUT']) {
57
+ assert.equal(hudLayout({ big, text: 'x', ...PHONE }).bigPx, BIG_WITH_TEXT_PX, big);
58
+ }
59
+ });
60
+
61
+ test('a long big line shrinks to one line at 360 px, never below the least, and is never wrapped', () => {
62
+ // The contract cuts `big` at 12 characters.
63
+ for (const big of ['LOCKED OUT W', 'WWWWWWWWWWWW', 'MMMMMMMMMMMM', '000000000000', 'SHOW OVER NOW', 'iiiiiiiiiiii']) {
64
+ for (const reserve of [0, 98]) {
65
+ const layout = hudLayout({ big, text: 'find the lantern', ...PHONE, reserve });
66
+ assert.equal(layout.bigOneLine, true);
67
+ assert.ok(layout.bigPx >= BIG_MIN_PX && layout.bigPx <= BIG_WITH_TEXT_PX, `${big}: ${layout.bigPx}`);
68
+ // Never wider than the HUD's own widest (70vw), never narrower than the
69
+ // box hangs (half the view).
70
+ assert.ok(layout.bigMaxWidth <= 252 && layout.bigMaxWidth >= 180);
71
+ // It fits by the estimate, or is at the least and cut with an ellipsis.
72
+ assert.ok(bigEms(big) * layout.bigPx <= layout.bigMaxWidth || layout.bigPx === BIG_MIN_PX, big);
73
+ }
74
+ }
75
+ const wide = hudLayout({ big: 'LOCKED OUT W', text: 'x', ...PHONE });
76
+ assert.ok(wide.bigPx < BIG_WITH_TEXT_PX, 'twelve capitals are shrunk');
77
+ // Beside a Back button the row is narrower, so the same words are smaller.
78
+ assert.ok(hudLayout({ big: 'LOCKED OUT W', text: 'x', ...PHONE, reserve: 98 }).bigPx <= wide.bigPx);
79
+ assert.equal(hudLayout({ big: 'LOCKED OUT W', text: 'x', ...PHONE, reserve: 98 }).bigMaxWidth, 180);
80
+ // On a wide view nothing of twelve characters needs shrinking.
81
+ assert.equal(hudLayout({ big: 'WWWWWWWWWWWW', text: 'x', viewW: 1280, viewH: 800 }).bigPx, BIG_WITH_TEXT_PX);
82
+ });
83
+
84
+ test('the drawn line is measured after: one still too wide shrinks to fit, to the least', () => {
85
+ assert.equal(fitPx(32, 252, 252), 32);
86
+ assert.equal(fitPx(32, 252, 200), 32);
87
+ assert.equal(fitPx(32, 252, 336), 24);
88
+ assert.equal(fitPx(32, 252, 5000), BIG_MIN_PX);
89
+ assert.equal(fitPx(32, 0, 100), 32, 'nothing measured: left alone');
90
+ assert.equal(fitPx(32, 252, undefined), 32);
91
+ });
92
+
93
+ test('a short view: the text keeps to two lines and the box to a share of the height', () => {
94
+ for (const spec of [{ big: '3', text: 'a long line' }, { big: '', text: 'a long line' }, { big: 'FINISH', text: '' }]) {
95
+ const layout = hudLayout({ ...spec, ...SIDEWAYS });
96
+ assert.equal(layout.textLines, 2);
97
+ assert.equal(layout.maxHeight, Math.round(360 * SHORT_HUD_SHARE));
98
+ assert.ok(layout.maxHeight <= 360 * 0.45);
99
+ }
100
+ // Title 17 px, the smaller big line, two lines of text: inside the share.
101
+ const layout = hudLayout({ big: '3', text: 'x', ...SIDEWAYS });
102
+ assert.ok(13 * 1.3 + layout.bigPx * 1.05 + 2 * 14 * 1.3 <= layout.maxHeight);
103
+ // A portrait view is never short, however small: the web player's card on a 360 px phone.
104
+ const card = hudLayout({ big: '3', text: 'x', viewW: 352, viewH: 475 });
105
+ assert.equal(card.textLines, null);
106
+ assert.equal(card.maxHeight, null);
107
+ assert.equal(card.bigPx, hudLayout({ big: '3', text: 'x', ...PHONE }).bigPx);
108
+ // A tall view has neither limit.
109
+ assert.equal(hudLayout({ big: '3', text: 'x', ...PHONE }).textLines, null);
110
+ assert.equal(hudLayout({ big: '3', text: 'x', ...PHONE }).maxHeight, null);
111
+ });
112
+
113
+ // --- the geometry --------------------------------------------------------
114
+
115
+ const HUD = { left: 90, right: 270, top: 12, bottom: 100 };
116
+
117
+ test('boxAt: a label hangs over its point, an edge chip is centred on it', () => {
118
+ assert.deepEqual(boxAt(180, 90, 100, 24), { left: 130, right: 230, top: 66, bottom: 90 });
119
+ assert.deepEqual(boxAt(180, 90, 100, 24, 'center'), { left: 130, right: 230, top: 78, bottom: 102 });
120
+ });
121
+
122
+ test('pushBelow: what lies over the HUD goes just under it; the rest stays', () => {
123
+ // Over it: moved so its top is the gap under the HUD's bottom.
124
+ const over = boxAt(180, 90, 100, 24);
125
+ assert.equal(pushBelow(over, HUD), HUD.bottom + HUD_GAP - over.top);
126
+ assert.equal(over.top + pushBelow(over, HUD), 106);
127
+ // Only its bottom edge under the HUD's: still moved whole.
128
+ assert.equal(boxAt(180, 110, 100, 24).top + pushBelow(boxAt(180, 110, 100, 24), HUD), 106);
129
+ // In the gap: moved the little that clears it.
130
+ assert.equal(pushBelow(boxAt(180, 127, 100, 24), HUD), 3);
131
+ // Already under, beside it, or above it: not moved.
132
+ assert.equal(pushBelow(boxAt(180, 130, 100, 24), HUD), 0);
133
+ assert.equal(pushBelow(boxAt(180, 400, 100, 24), HUD), 0);
134
+ assert.equal(pushBelow(boxAt(40, 90, 100, 24), HUD), 0, 'its right edge touches the HUD');
135
+ assert.equal(pushBelow(boxAt(320, 90, 100, 24), HUD), 0);
136
+ assert.equal(pushBelow(boxAt(180, 12, 100, 24), HUD), 0, 'above the HUD');
137
+ // A corner over it is over it.
138
+ assert.ok(pushBelow(boxAt(60, 90, 100, 24), HUD) > 0);
139
+ // No HUD drawn: nothing moves.
140
+ assert.equal(pushBelow(over, null), 0);
141
+ // Never up: a push is zero or down.
142
+ for (let y = -50; y < 400; y += 7) {
143
+ assert.ok(pushBelow(boxAt(180, y, 100, 24), HUD) >= 0);
144
+ }
145
+ });
146
+
147
+ // --- the chrome, over a stand-in DOM -------------------------------------
148
+
149
+ // Every write (a style, the text, a child added or removed) is logged in
150
+ // order, so two runs can be held to the same writes, not only the same end.
151
+ function fakeDom(view) {
152
+ const log = [];
153
+ let ids = 0;
154
+ const made = [];
155
+ function node(tag) {
156
+ const id = `${tag}#${(ids += 1)}`;
157
+ const styles = {};
158
+ let text = '';
159
+ const self = {
160
+ id,
161
+ tag,
162
+ children: [],
163
+ parent: null,
164
+ dataset: {},
165
+ attrs: {},
166
+ rect: null,
167
+ size: null,
168
+ style: new Proxy(styles, {
169
+ get: (target, key) => target[key] ?? '',
170
+ set(target, key, value) {
171
+ target[key] = String(value);
172
+ log.push(`${id} style.${String(key)}=${value}`);
173
+ return true;
174
+ },
175
+ }),
176
+ styles,
177
+ get textContent() {
178
+ return text;
179
+ },
180
+ set textContent(value) {
181
+ text = String(value);
182
+ log.push(`${id} text=${text}`);
183
+ },
184
+ appendChild(child) {
185
+ child.parent = self;
186
+ self.children.push(child);
187
+ log.push(`${id} +${child.id}`);
188
+ return child;
189
+ },
190
+ append(...kids) {
191
+ kids.forEach((kid) => self.appendChild(kid));
192
+ },
193
+ remove() {
194
+ if (self.parent) {
195
+ self.parent.children = self.parent.children.filter((c) => c !== self);
196
+ log.push(`${id} removed`);
197
+ }
198
+ },
199
+ replaceChildren(...kids) {
200
+ self.children = [];
201
+ log.push(`${id} emptied`);
202
+ kids.forEach((kid) => self.appendChild(kid));
203
+ },
204
+ addEventListener() {},
205
+ removeEventListener() {},
206
+ setAttribute(key, value) {
207
+ self.attrs[key] = String(value);
208
+ },
209
+ getAttribute: (key) => self.attrs[key] ?? null,
210
+ focus() {},
211
+ blur() {},
212
+ // A stand-in layout: 7 px a character and 20 of padding, one 24 px line.
213
+ get offsetWidth() {
214
+ log.push(`${id} read`);
215
+ return (typeof self.size === 'function' ? self.size(self) : self.size)?.w ?? text.length * 7 + 20;
216
+ },
217
+ get offsetHeight() {
218
+ log.push(`${id} read`);
219
+ return (typeof self.size === 'function' ? self.size(self) : self.size)?.h ?? 24;
220
+ },
221
+ get offsetLeft() {
222
+ return parseFloat(styles.left) || 0;
223
+ },
224
+ get clientWidth() {
225
+ return self.client ?? self.offsetWidth;
226
+ },
227
+ get scrollWidth() {
228
+ return self.scroll ?? self.offsetWidth;
229
+ },
230
+ getBoundingClientRect() {
231
+ const r = self.rect ?? { left: 0, right: 0, top: 0, bottom: 0 };
232
+ return { ...r, width: r.right - r.left, height: r.bottom - r.top };
233
+ },
234
+ };
235
+ made.push(self);
236
+ return self;
237
+ }
238
+ const body = node('body');
239
+ globalThis.document = { body, createElement: node, activeElement: null };
240
+ globalThis.window = { innerWidth: view.viewW, innerHeight: view.viewH };
241
+ globalThis.innerWidth = view.viewW;
242
+ return { log, body, made };
243
+ }
244
+
245
+ const { makeChrome } = await import(process.env.HUB_TEST_CHROME ?? '../code/client/chrome.js');
246
+
247
+ function rig(view = PHONE) {
248
+ const dom = fakeDom(view);
249
+ const chrome = makeChrome({ onBack() {}, onAvatar() {}, avatarCount: 4 });
250
+ const root = dom.body.children[0];
251
+ const hud = root.children.find((c) => c.styles.maxWidth === '70vw');
252
+ const [hudTitle, hudBig, hudText] = hud.children;
253
+ const labels = root.children[root.children.indexOf(hud) + 1];
254
+ const pointers = dom.made.find((c) => c.parent === root && c !== labels && c.styles.inset === '0' && c.styles.position === 'absolute' && !c.styles.background && c.children.length === 0 && dom.made.indexOf(c) > dom.made.indexOf(labels));
255
+ const cabinet = dom.made.find((c) => c.dataset.hub === 'cabinet');
256
+ return { ...dom, chrome, root, hud, hudTitle, hudBig, hudText, labels, pointers, cabinet };
257
+ }
258
+
259
+ const SPEC = { title: 'Lantern Run', big: '12.3', text: 'find the lantern and bring it home' };
260
+ const LABELS = [
261
+ { id: 'sign', x: 180, y: 90, text: 'Start here', speech: false, npc: false, opacity: 1 },
262
+ { id: 'npc', x: 170, y: 60, text: 'Take the lantern to the well.', speech: true, npc: true, opacity: 1 },
263
+ { id: 'mark', x: 30, y: 80, text: 'IT', color: '#ff0000' },
264
+ { id: 'far', x: 180, y: 400, text: 'Well', speech: false, npc: false, opacity: 0.5 },
265
+ { id: 'wave', x: 200, y: 70, text: '👋', emote: true },
266
+ ];
267
+ const POINTERS = [
268
+ { id: 'goal:well', x: 180, y: 84, text: 'The well · 22 m', angle: -1.57 },
269
+ { id: 'to:p2', x: 200, y: 95, text: 'bo · 31 m', angle: null },
270
+ { id: 'goal:gate', x: 180, y: 500, text: 'Gate', angle: null },
271
+ ];
272
+
273
+ // A visit's frames, as the hub system draws them (`drawChrome`): the flag
274
+ // when `tell` gives one, then the HUD, the pointers, the labels, the ribbon.
275
+ function frames(r, tell, specs = [SPEC, { ...SPEC, big: '12.4' }, { title: 'Lantern Run', big: 'FINISH' }, null, { text: 'only words' }, SPEC]) {
276
+ for (const spec of specs) {
277
+ const on = tell();
278
+ if (on !== undefined) {
279
+ r.chrome.setHudClear(on);
280
+ }
281
+ r.chrome.setHud(spec, null);
282
+ r.chrome.setPointers(POINTERS);
283
+ r.chrome.setLabels(LABELS);
284
+ r.chrome.setRibbon({ x: 180, y: 96 }, '🏅 12.3 s');
285
+ // The frame's end. The hub system says it only in a place that asks; said
286
+ // here in every run, it must do nothing at all in one that does not.
287
+ r.chrome.flushHudClear?.();
288
+ }
289
+ }
290
+
291
+ // A style set to '' is one not set, as in a browser.
292
+ const styleOf = (node) => Object.fromEntries(Object.entries(node.styles).filter(([, value]) => value !== ''));
293
+ const tree = (node) => ({ id: node.id, text: node.children.length ? undefined : node.textContent, style: styleOf(node), kids: node.children.map(tree) });
294
+
295
+ // The HUD as it is today, written here so a change to the default shows.
296
+ const TODAY = {
297
+ hud: { position: 'absolute', top: 'max(12px, env(safe-area-inset-top))', left: '50%', transform: 'translateX(-50%)', textAlign: 'center', textShadow: '0 0 2px rgba(0,0,0,0.95), 0 0 2px rgba(0,0,0,0.95), 0 1px 4px rgba(0,0,0,0.75), 0 0 10px rgba(0,0,0,0.45)', display: 'block', maxWidth: '70vw' },
298
+ title: { fontSize: '13px', opacity: '0.85', letterSpacing: '0.06em', textTransform: 'uppercase' },
299
+ big: { fontSize: '56px', fontWeight: '800', lineHeight: '1.05', display: 'block' },
300
+ text: { fontSize: '14px', fontWeight: '500' },
301
+ };
302
+
303
+ test('never told: the HUD, labels and pointers are styled and placed as today', () => {
304
+ const r = rig();
305
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
306
+ frames(r, () => undefined);
307
+ assert.deepEqual(styleOf(r.hud), TODAY.hud);
308
+ assert.deepEqual(styleOf(r.hudTitle), TODAY.title);
309
+ assert.deepEqual(styleOf(r.hudBig), TODAY.big);
310
+ assert.deepEqual(styleOf(r.hudText), TODAY.text);
311
+ // Each label is where it was asked to be, under the HUD or not.
312
+ const top = Object.fromEntries(r.labels.children.map((c) => [c.textContent, c.styles.top]));
313
+ assert.deepEqual(top, { 'Start here': '90px', 'Take the lantern to the well.': '60px', IT: '80px', Well: '400px', '👋': '70px' });
314
+ assert.deepEqual(r.pointers.children.map((c) => c.styles.top), ['84px', '95px', '500px']);
315
+ // Nothing of a clear HUD was written anywhere.
316
+ for (const node of [r.hud, r.hudBig, r.hudText]) {
317
+ assert.ok(!r.log.some((line) => line.startsWith(`${node.id} `) && /overflow|maxHeight|webkit|textOverflow|margin|whiteSpace/.test(line)), node.id);
318
+ }
319
+ });
320
+
321
+ test('told no: the same writes in the same order as never told', () => {
322
+ const never = rig();
323
+ never.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
324
+ const base = never.log.length;
325
+ frames(never, () => undefined);
326
+ const no = rig();
327
+ no.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
328
+ assert.equal(no.log.length, base, 'the chrome is built the same');
329
+ frames(no, () => false);
330
+ assert.deepEqual(no.log, never.log);
331
+ assert.deepEqual(tree(no.root), tree(never.root));
332
+ });
333
+
334
+ test('asked: a big line over text is 32 px on one line; alone it is 56 px again', () => {
335
+ const r = rig();
336
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 82 };
337
+ r.chrome.setHudClear(true);
338
+ r.chrome.setHud(SPEC, null);
339
+ assert.equal(r.hudBig.styles.fontSize, '32px');
340
+ assert.equal(r.hudBig.styles.whiteSpace, 'nowrap');
341
+ assert.equal(r.hudBig.styles.overflow, 'hidden');
342
+ assert.equal(r.hudBig.styles.textOverflow, 'ellipsis');
343
+ assert.equal(r.hudBig.styles.maxWidth, '252px');
344
+ assert.equal(r.hudText.styles.webkitLineClamp ?? '', '', 'a tall view does not cut the text');
345
+ assert.equal(r.hud.styles.maxHeight ?? '', '');
346
+ // The timer is the big line when the place gives none.
347
+ r.chrome.setHud({ title: 'Lantern Run', text: 'go', timer: { since: 0 } }, 4.26);
348
+ assert.equal(r.hudBig.textContent, '4.3');
349
+ assert.equal(r.hudBig.styles.fontSize, '32px');
350
+ // Alone: the 56 px of always, free to be as it was.
351
+ r.chrome.setHud({ title: 'Lantern Run', big: 'FINISH' }, null);
352
+ assert.equal(r.hudBig.styles.fontSize, '56px');
353
+ assert.equal(r.hudBig.styles.whiteSpace, '');
354
+ assert.equal(r.hudBig.styles.overflow, '');
355
+ assert.equal(r.hudBig.styles.maxWidth, '');
356
+ // The drawn line is wider than its box: shrunk again by what was measured.
357
+ r.hudBig.client = 252;
358
+ r.hudBig.scroll = 336;
359
+ r.chrome.setHud({ big: 'LANTERN', text: 'x' }, null);
360
+ assert.equal(r.hudBig.styles.fontSize, '24px');
361
+ });
362
+
363
+ test('asked: beside a Back button the big line keeps to its own part of the row', () => {
364
+ const r = rig();
365
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 82 };
366
+ // The Back button as a phone draws it: 12 px in, about 78 wide.
367
+ r.root.children.find((c) => c.tag === 'button' && c.styles.left === '12px').size = { w: 78, h: 38 };
368
+ r.chrome.showBack(true, 'Town');
369
+ r.chrome.setHudClear(true);
370
+ r.chrome.setHud({ big: 'LOCKED OUT W', text: 'x' }, null);
371
+ assert.equal(r.hudBig.styles.maxWidth, '180px');
372
+ assert.ok(parseFloat(r.hudBig.styles.fontSize) < 32 && parseFloat(r.hudBig.styles.fontSize) >= BIG_MIN_PX);
373
+ });
374
+
375
+ test('asked, a phone on its side: two lines of text and a box no taller than its share', () => {
376
+ const r = rig(SIDEWAYS);
377
+ r.hud.rect = { left: 190, right: 550, top: 12, bottom: 96 };
378
+ r.chrome.setHudClear(true);
379
+ r.chrome.setHud(SPEC, null);
380
+ assert.equal(r.hudBig.styles.fontSize, '28px');
381
+ assert.equal(r.hudText.styles.display, '-webkit-box');
382
+ assert.equal(r.hudText.styles.webkitLineClamp, '2');
383
+ assert.equal(r.hudText.styles.webkitBoxOrient, 'vertical');
384
+ assert.equal(r.hudText.styles.overflow, 'hidden');
385
+ assert.equal(r.hud.styles.maxHeight, '144px');
386
+ assert.equal(r.hud.styles.overflow, 'hidden');
387
+ });
388
+
389
+ test('asked: labels, speech, marks, emotes, pointers and the ribbon over the HUD go just under it', () => {
390
+ const r = rig();
391
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
392
+ frames(r, () => true, [SPEC]);
393
+ const node = (text) => r.labels.children.find((c) => c.textContent === text);
394
+ // Each is 24 px tall here, hung by its bottom edge: clear is top 112, so 136.
395
+ assert.equal(node('Start here').styles.top, '136px');
396
+ assert.equal(node('Take the lantern to the well.').styles.top, '136px');
397
+ assert.equal(node('👋').styles.top, '136px');
398
+ // Beside the HUD (its box ends left of 90) and far below it: where they were.
399
+ assert.equal(node('IT').styles.top, '80px');
400
+ assert.equal(node('Well').styles.top, '400px');
401
+ // Never hidden, never moved sideways.
402
+ for (const c of r.labels.children) {
403
+ assert.notEqual(c.styles.display, 'none');
404
+ }
405
+ assert.equal(node('Start here').styles.left, '180px');
406
+ // An edge chip is centred on its point (top 112 is y 124); one over a friend hangs.
407
+ assert.deepEqual(r.pointers.children.map((c) => c.styles.top), ['124px', '136px', '500px']);
408
+ const ribbon = r.made.find((c) => c.textContent === '🏅 12.3 s');
409
+ assert.equal(ribbon.styles.top, '136px');
410
+ // Nothing drawn: nothing to keep clear of.
411
+ frames(r, () => true, [null]);
412
+ assert.equal(node('Start here').styles.top, '90px');
413
+ assert.deepEqual(r.pointers.children.map((c) => c.styles.top), ['84px', '95px', '500px']);
414
+ // A taller HUD pushes farther: the box is measured again when it changes.
415
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 140 };
416
+ frames(r, () => true, [{ ...SPEC, text: 'a much longer line of words that wraps' }]);
417
+ assert.equal(node('Start here').styles.top, '170px');
418
+ });
419
+
420
+ test('asked: a cabinet card over the HUD goes under it too', () => {
421
+ const r = rig();
422
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
423
+ r.chrome.setHudClear(true);
424
+ r.chrome.setHud(SPEC, null);
425
+ r.cabinet.size = { w: 120, h: 30 };
426
+ const item = { id: 'cab', rev: 1, card: { title: 'Arcade', top: [{ username: 'bo', score: 12 }] }, x: 180, y: 90, opacity: 1 };
427
+ r.chrome.setCabinet(item, () => null);
428
+ r.chrome.flushHudClear?.();
429
+ assert.equal(r.cabinet.styles.display, 'block');
430
+ // 30 px tall, hung by its bottom edge: clear is top 112, so 142.
431
+ assert.equal(r.cabinet.styles.top, '142px');
432
+ const off = rig();
433
+ off.hud.rect = r.hud.rect;
434
+ off.chrome.setHud(SPEC, null);
435
+ off.cabinet.size = { w: 120, h: 30 };
436
+ off.chrome.setCabinet(item, () => null);
437
+ off.chrome.flushHudClear?.();
438
+ assert.equal(off.cabinet.styles.display, 'block');
439
+ assert.equal(off.cabinet.styles.top, '90px');
440
+ });
441
+
442
+ test('after a place that asked, a place that does not is drawn as never told', () => {
443
+ for (const view of [PHONE, SIDEWAYS]) {
444
+ const never = rig(view);
445
+ never.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
446
+ frames(never, () => undefined);
447
+ const after = rig(view);
448
+ after.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
449
+ after.chrome.showBack(true, 'Town');
450
+ frames(after, () => true);
451
+ after.chrome.showBack(false);
452
+ const mark = after.log.length;
453
+ frames(after, () => false);
454
+ // Same styles and positions on every node the two runs share.
455
+ const strip = (t) => JSON.stringify(t).replace(/"id":"[^"]*",?/g, '');
456
+ const hudOf = (r) => strip(tree(r.hud));
457
+ assert.equal(hudOf(after), hudOf(never));
458
+ assert.equal(strip(tree(after.labels)), strip(tree(never.labels)));
459
+ assert.equal(strip(tree(after.pointers)), strip(tree(never.pointers)));
460
+ assert.deepEqual(styleOf(after.hudBig), TODAY.big);
461
+ assert.deepEqual(styleOf(after.hudText), TODAY.text);
462
+ assert.deepEqual(styleOf(after.hud), TODAY.hud);
463
+ // And after the one frame that put it back, nothing of it is written.
464
+ const putBack = after.log.slice(mark).filter((line) => /maxHeight|webkit|textOverflow/.test(line));
465
+ assert.equal(putBack.length, 4, 'put back once, in the first frame');
466
+ }
467
+ });
468
+
469
+ test('asked: a bubble that wraps by where it is is measured again when it moves', () => {
470
+ const r = rig();
471
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
472
+ r.chrome.setHudClear(true);
473
+ r.chrome.setHud(SPEC, null);
474
+ // A player's chat bubble: its width is what is left of the view to its
475
+ // right, so nearer the right edge it is narrower and taller.
476
+ const bubble = (x) => [{ id: 'say:p2', x, y: 136, text: 'meet me by the well when the bell rings', speech: true, npc: false, opacity: 1 }];
477
+ let reads = 0;
478
+ r.chrome.setLabels(bubble(100));
479
+ r.chrome.flushHudClear?.();
480
+ const node = r.labels.children[0];
481
+ node.size = (self) => ((reads += 1), parseFloat(self.styles.left) > 200 ? { w: 150, h: 58 } : { w: 220, h: 24 });
482
+ r.chrome.setLabels(bubble(100));
483
+ r.chrome.flushHudClear?.();
484
+ assert.equal(node.styles.top, '136px', 'one line: its top is 112, clear');
485
+ // Moved right with the same words: three lines now, so its top would be 78.
486
+ r.chrome.setLabels(bubble(210));
487
+ r.chrome.flushHudClear?.();
488
+ assert.equal(node.styles.top, '170px', 'its top is 112 again');
489
+ // Standing still it is not measured every frame.
490
+ const before = reads;
491
+ for (let i = 0; i < 10; i += 1) {
492
+ r.chrome.setLabels(bubble(210));
493
+ r.chrome.flushHudClear?.();
494
+ }
495
+ assert.equal(reads, before);
496
+ // A label that never wraps is measured once, wherever it goes.
497
+ const sign = (x) => [{ id: 'sign', x, y: 90, text: 'Start here', speech: false, npc: false, opacity: 1 }];
498
+ r.chrome.setLabels(sign(100));
499
+ r.chrome.flushHudClear?.();
500
+ let signReads = 0;
501
+ r.labels.children[0].size = () => ((signReads += 1), { w: 90, h: 24 });
502
+ for (let x = 100; x < 260; x += 10) {
503
+ r.chrome.setLabels(sign(x));
504
+ r.chrome.flushHudClear?.();
505
+ }
506
+ assert.ok(signReads <= 2, `${signReads} reads`);
507
+ });
508
+
509
+ test('asked: a frame is all measured, then all moved -- no layout read after its first push, across pointers, labels, the card and the ribbon', () => {
510
+ const r = rig();
511
+ r.hud.rect = { left: 60, right: 300, top: 12, bottom: 106 };
512
+ // Four chat bubbles and two pointers drifting sideways under the HUD: each
513
+ // bubble's size is read again every frame (it wraps by where it is), and
514
+ // each pointer's words change with its distance.
515
+ const bubbles = (dx) => [0, 1, 2, 3].map((i) => ({ id: `say:p${i}`, x: 100 + i * 40 + dx, y: 60 + i * 10, text: `bubble ${i} has a few words in it`, speech: true, npc: false, opacity: 1 }));
516
+ const chips = (dx) => [0, 1].map((i) => ({ id: `to:p${i}`, x: 150 + i * 60 + dx, y: 90, text: `friend ${i} · ${20 + dx} m`, angle: null }));
517
+ const card = { id: 'cab', rev: 1, card: { title: 'Arcade', top: [{ username: 'bo', score: 12 }] }, x: 180, y: 90, opacity: 1 };
518
+ // The hub system's own order (`drawChrome`).
519
+ const frame = (dx) => {
520
+ r.chrome.setHudClear(true);
521
+ r.chrome.setHud(SPEC, null);
522
+ r.chrome.setPointers(chips(dx));
523
+ r.chrome.setLabels(bubbles(dx));
524
+ r.chrome.setCabinet(card, () => null);
525
+ r.chrome.setRibbon({ x: 180 + dx, y: 96 }, `🏅 ${dx} s`);
526
+ r.chrome.setToast('');
527
+ r.chrome.flushHudClear();
528
+ };
529
+ for (let dx = 0; dx < 8; dx += 1) {
530
+ const mark = r.log.length;
531
+ frame(dx);
532
+ const lines = r.log.slice(mark);
533
+ // A push is the second write of `top` to a node in the frame.
534
+ const tops = new Set();
535
+ const firstPush = lines.findIndex((line) => {
536
+ const m = /^(\S+) style\.top=/.exec(line);
537
+ if (!m) {
538
+ return false;
539
+ }
540
+ if (tops.has(m[1])) {
541
+ return true;
542
+ }
543
+ tops.add(m[1]);
544
+ return false;
545
+ });
546
+ assert.ok(firstPush > 0, 'something was pushed');
547
+ const before = lines.slice(0, firstPush);
548
+ const after = lines.slice(firstPush);
549
+ // Measured this frame: the four moving bubbles, the two pointers, the ribbon.
550
+ const measured = new Set(before.filter((line) => line.endsWith(' read')).map((line) => line.split(' ')[0]));
551
+ for (const node of [...r.labels.children, ...r.pointers.children]) {
552
+ assert.ok(measured.has(node.id), `${node.id} was measured`);
553
+ }
554
+ assert.ok(!after.some((line) => line.endsWith(' read')), `frame ${dx}: no read after the first push`);
555
+ assert.ok(after.every((line) => / style\.top=/.test(line)), 'only pushes after it');
556
+ // Eight pushes: four bubbles, two pointers, the card, the ribbon.
557
+ assert.equal(after.length, 8);
558
+ }
559
+ // And each is clear: 24 px tall, top 112.
560
+ assert.deepEqual(r.labels.children.map((c) => c.styles.top), ['136px', '136px', '136px', '136px']);
561
+ assert.deepEqual(r.pointers.children.map((c) => c.styles.top), ['136px', '136px']);
562
+ assert.equal(r.cabinet.styles.top, '136px');
563
+ assert.equal(r.made.find((c) => c.textContent === '🏅 7 s').styles.top, '136px');
564
+ });
565
+
566
+ test('never asked: the end of a frame reads and writes nothing', () => {
567
+ const r = rig();
568
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
569
+ frames(r, () => undefined, [SPEC]);
570
+ const mark = r.log.length;
571
+ r.chrome.flushHudClear();
572
+ r.chrome.flushHudClear();
573
+ assert.equal(r.log.length, mark);
574
+ // Nor after a place that asked has been left.
575
+ frames(r, () => true, [SPEC]);
576
+ r.chrome.setHudClear(false);
577
+ r.chrome.setLabels(LABELS);
578
+ const quiet = r.log.length;
579
+ r.chrome.flushHudClear();
580
+ assert.equal(r.log.length, quiet);
581
+ });
582
+
583
+ test('pushBelow: nothing is pushed off the bottom of the view', () => {
584
+ const hud = { left: 90, right: 270, top: 12, bottom: 94 };
585
+ // It fits under the HUD and in the view: moved clear, as without a view.
586
+ assert.equal(pushBelow(boxAt(180, 90, 100, 24), hud, HUD_GAP, 360), pushBelow(boxAt(180, 90, 100, 24), hud));
587
+ // A 276 px board on a 360 px view cannot be both: moved only as far as
588
+ // keeps it whole on screen, the HUD over its top.
589
+ const board = boxAt(180, 320, 204, 276);
590
+ assert.equal(pushBelow(board, hud), 56);
591
+ assert.equal(pushBelow(board, hud, HUD_GAP, 360), 40);
592
+ assert.equal(board.bottom + pushBelow(board, hud, HUD_GAP, 360), 360);
593
+ // Already at or past the bottom: left where it is, never moved up.
594
+ assert.equal(pushBelow(boxAt(180, 360, 204, 300), hud, HUD_GAP, 360), 0);
595
+ assert.equal(pushBelow(boxAt(180, 380, 204, 330), hud, HUD_GAP, 360), 0);
596
+ });
597
+
598
+ test('asked: an open cabinet board on a short view stays whole on screen', () => {
599
+ const r = rig(SIDEWAYS);
600
+ r.hud.rect = { left: 190, right: 550, top: 12, bottom: 94 };
601
+ r.chrome.setHudClear(true);
602
+ r.chrome.setHud(SPEC, null);
603
+ r.cabinet.size = { w: 204, h: 276 };
604
+ r.chrome.setCabinet({ id: 'cab', rev: 1, card: { title: 'Arcade', top: [{ username: 'bo', score: 12 }] }, x: 370, y: 320, opacity: 1 }, () => null);
605
+ r.chrome.flushHudClear?.();
606
+ assert.equal(r.cabinet.styles.display, 'block');
607
+ // Its bottom is its `top` style: never past the view's 360.
608
+ assert.equal(r.cabinet.styles.top, '360px');
609
+ // A small label there still goes clear under the HUD.
610
+ r.chrome.setLabels([{ id: 'sign', x: 370, y: 90, text: 'Start here', speech: false, npc: false, opacity: 1 }]);
611
+ r.chrome.flushHudClear?.();
612
+ assert.equal(r.labels.children[0].styles.top, '124px');
613
+ });
614
+
615
+ test('asked, then not, with no HUD drawn: put back at once, and written to as never told', () => {
616
+ for (const view of [PHONE, SIDEWAYS]) {
617
+ const later = [null, null, SPEC, { title: 'Lantern Run', big: 'FINISH' }, null];
618
+ const never = rig(view);
619
+ never.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
620
+ frames(never, () => undefined);
621
+ const neverMark = never.log.length;
622
+ frames(never, () => undefined, later);
623
+ const after = rig(view);
624
+ after.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
625
+ frames(after, () => true);
626
+ assert.notDeepEqual(styleOf(after.hudBig), TODAY.big);
627
+ // The place is left for one with no HUD: the option goes, then no HUD.
628
+ after.chrome.setHudClear(false);
629
+ for (const [node, today] of [[after.hud, TODAY.hud], [after.hudTitle, TODAY.title], [after.hudBig, TODAY.big], [after.hudText, TODAY.text]]) {
630
+ assert.deepEqual(styleOf(node), today, 'put back before any HUD is drawn');
631
+ }
632
+ const mark = after.log.length;
633
+ after.chrome.setHud(null, null);
634
+ assert.deepEqual(styleOf(after.hud), { ...TODAY.hud, display: 'none' });
635
+ assert.deepEqual(styleOf(after.hudBig), TODAY.big);
636
+ assert.deepEqual(styleOf(after.hudText), TODAY.text);
637
+ after.log.length = mark;
638
+ // From then on: the same writes, in the same order, as a client never told.
639
+ frames(after, () => undefined, later);
640
+ assert.deepEqual(after.log.slice(mark), never.log.slice(neverMark));
641
+ const strip = (t) => JSON.stringify(t).replace(/"id":"[^"]*",?/g, '');
642
+ assert.equal(strip(tree(after.root)), strip(tree(never.root)));
643
+ // Told no again and again: nothing more is written for it.
644
+ const quiet = after.log.length;
645
+ after.chrome.setHudClear(false);
646
+ after.chrome.setHudClear(false);
647
+ assert.equal(after.log.length, quiet);
648
+ }
649
+ });
650
+
651
+ // A setter called with no flush after it (outside the hub system's frame) is
652
+ // still moved, before the browser would paint: all read, then all written.
653
+ {
654
+ const r = rig();
655
+ r.hud.rect = { left: 90, right: 270, top: 12, bottom: 106 };
656
+ r.chrome.setHudClear(true);
657
+ r.chrome.setHud(SPEC, null);
658
+ const mark = r.log.length;
659
+ r.chrome.setPointers(POINTERS);
660
+ r.chrome.setLabels(LABELS);
661
+ assert.equal(r.labels.children[0].styles.top, '90px', 'not moved as it is asked');
662
+ await Promise.resolve();
663
+ assert.equal(r.labels.children[0].styles.top, '136px');
664
+ assert.deepEqual(r.pointers.children.map((c) => c.styles.top), ['124px', '136px', '500px']);
665
+ const lines = r.log.slice(mark);
666
+ const lastRead = lines.findLastIndex((line) => line.endsWith(' read'));
667
+ assert.ok(lines.slice(lastRead + 1).every((line) => / style\.top=/.test(line)), 'reads, then only pushes');
668
+ // Flushed once: the frame's own flush after it finds nothing left.
669
+ const quiet = r.log.length;
670
+ r.chrome.flushHudClear();
671
+ await Promise.resolve();
672
+ assert.equal(r.log.length, quiet);
673
+ n += 1;
674
+ }
675
+
676
+ console.log(`hub-game hud-clear: ${n} tests passed`);