castle-web-cli 0.4.237 → 0.4.239
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/dist/bundle.d.ts +24 -0
- package/dist/bundle.js +447 -4
- package/dist/bundleLatin1.d.ts +6 -0
- package/dist/bundleLatin1.js +333 -0
- package/dist/castle-host/host.d.ts +10 -1
- package/dist/castle-host/host.js +133 -9
- package/dist/castleJson.d.ts +2 -0
- package/dist/castleJson.js +1 -0
- package/dist/deckExports.d.ts +35 -0
- package/dist/deckExports.js +365 -0
- package/dist/importCheckHook.d.ts +1 -0
- package/dist/importCheckHook.js +1 -1
- package/dist/index.js +21 -0
- package/dist/init.js +2 -1
- package/dist/multiplayerDoc.d.ts +1 -1
- package/dist/multiplayerDoc.js +9 -0
- package/dist/platformDoc.js +2 -1
- package/dist/playPagePolicy.d.ts +1 -0
- package/dist/playPagePolicy.js +8 -3
- package/dist/roomLogs.d.ts +39 -0
- package/dist/roomLogs.js +166 -0
- package/dist/save-deck.js +4 -0
- package/kits/base/CLAUDE.md +1 -0
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +113 -2
- package/kits/base/sdk/castle.d.ts +6 -2
- package/kits/base/sdk/castle.js +2 -0
- package/kits/base/sdk/commands.d.ts +56 -1
- package/kits/base/sdk/commands.js +29 -1
- package/kits/base/sdk/hub.d.ts +17 -0
- package/kits/base/sdk/hub.js +33 -9
- package/kits/base/sdk/localCache.d.ts +27 -0
- package/kits/base/sdk/localCache.js +115 -0
- package/kits/base/sdk/localCacheStore.d.ts +15 -0
- package/kits/base/sdk/localCacheStore.js +358 -0
- package/kits/base/sdk/multiplayer.js +30 -0
- package/kits/base/sdk/rooms.d.ts +11 -0
- package/kits/base/sdk/rooms.js +22 -0
- package/kits/base/sdk/store.d.ts +8 -2
- package/kits/base/sdk/store.js +5 -1
- package/kits/base/sdk/transport.d.ts +1 -0
- package/kits/base/sdk/transport.js +4 -5
- package/kits/hub-game/CLAUDE.md +22 -2
- package/kits/hub-game/castle.json +8 -5
- package/kits/hub-game/code/client/act.js +66 -91
- package/kits/hub-game/code/client/actCore.js +134 -0
- package/kits/hub-game/code/client/car.js +35 -0
- package/kits/hub-game/code/client/chrome.js +208 -381
- package/kits/hub-game/code/client/crowdNames.js +2 -2
- package/kits/hub-game/code/client/emoteWheel.js +109 -0
- package/kits/hub-game/code/client/hats.js +2 -0
- package/kits/hub-game/code/client/hookLayer.js +99 -48
- package/kits/hub-game/code/client/hostGo.js +77 -0
- package/kits/hub-game/code/client/hubControls.js +125 -0
- package/kits/hub-game/code/client/hubEntry.js +187 -0
- package/kits/hub-game/code/client/hudSurface.js +148 -0
- package/kits/hub-game/code/client/hudTree.js +96 -0
- package/kits/hub-game/code/client/idle.js +59 -13
- package/kits/hub-game/code/client/invites.js +13 -4
- package/kits/hub-game/code/client/menuIcons.js +8 -0
- package/kits/hub-game/code/client/menuRows.js +8 -2
- package/kits/hub-game/code/client/noTrips.js +38 -0
- package/kits/hub-game/code/client/qualityEntry.js +6 -1
- package/kits/hub-game/code/client/rehello.js +3 -1
- package/kits/hub-game/code/client/riders.js +26 -0
- package/kits/hub-game/code/client/role.js +79 -0
- package/kits/hub-game/code/client/safeLayout.js +32 -3
- package/kits/hub-game/code/client/sound.js +10 -3
- package/kits/hub-game/code/client/soundscape.js +2 -2
- package/kits/hub-game/code/client/soundscapeAmbience.js +38 -8
- package/kits/hub-game/code/client/soundscapeBaked.js +24 -5
- package/kits/hub-game/code/client/soundscapeCues.js +2 -1
- package/kits/hub-game/code/client/state.js +19 -0
- package/kits/hub-game/code/client/stuck.js +2 -1
- package/kits/hub-game/code/client/talkSurface.js +178 -0
- package/kits/hub-game/code/client/talkTree.js +238 -0
- package/kits/hub-game/code/client/tiers.js +8 -5
- package/kits/hub-game/code/client/uiDom.js +600 -0
- package/kits/hub-game/code/client/uiTree.js +359 -0
- package/kits/hub-game/code/client/vehicle.js +156 -0
- package/kits/hub-game/code/client/wear.js +27 -5
- package/kits/hub-game/code/client/worldLabels.js +13 -3
- package/kits/hub-game/code/client/yours.js +10 -3
- package/kits/hub-game/code/server/actors.js +4 -3
- package/kits/hub-game/code/server/analytics.js +61 -18
- package/kits/hub-game/code/server/decor.js +56 -16
- package/kits/hub-game/code/server/doorAnalytics.js +2 -5
- package/kits/hub-game/code/server/hub.js +261 -46
- package/kits/hub-game/code/server/idle.js +118 -5
- package/kits/hub-game/code/server/loadAbandon.js +2 -5
- package/kits/hub-game/code/server/manifest.js +46 -4
- package/kits/hub-game/code/server/npcs.js +4 -2
- package/kits/hub-game/code/server/players.js +3 -0
- package/kits/hub-game/code/server/role.js +142 -0
- package/kits/hub-game/code/server/standing.js +6 -4
- package/kits/hub-game/code/server/trackQueue.js +29 -0
- package/kits/hub-game/code/server/vehicle.js +126 -0
- package/kits/hub-game/code/shared/car.js +52 -0
- package/kits/hub-game/code/shared/deckFlags.js +34 -0
- package/kits/hub-game/code/shared/deviceClass.js +39 -0
- package/kits/hub-game/code/shared/expression.js +9 -0
- package/kits/hub-game/code/systems/hub.js +281 -86
- package/kits/hub-game/docs/analytics.md +15 -3
- package/kits/hub-game/docs/contract.md +352 -14
- package/kits/hub-game/docs/vehicle-preset.md +132 -0
- package/kits/hub-game/models/hats/baker-cap.pxmodel +89 -0
- package/kits/hub-game/models/hats/chef-toque.pxmodel +60 -0
- package/kits/hub-game/models/hats/delivery-cap.pxmodel +105 -0
- package/kits/hub-game/models/hats/fire-helmet.pxmodel +87 -0
- package/kits/hub-game/models/hats/hard-hat.pxmodel +71 -0
- package/kits/hub-game/models/hats/nurse-cap.pxmodel +61 -0
- package/kits/hub-game/models/hats/police-cap.pxmodel +105 -0
- package/kits/hub-game/models/hats/teacher-beret.pxmodel +52 -0
- package/kits/hub-game/models/role/square/top-apron.pxmodel +109 -0
- package/kits/hub-game/models/role/square/top-cardigan.pxmodel +141 -0
- package/kits/hub-game/models/role/square/top-coat.pxmodel +157 -0
- package/kits/hub-game/models/role/square/top-scrubs.pxmodel +93 -0
- package/kits/hub-game/models/role/square/top-tabard.pxmodel +109 -0
- package/kits/hub-game/models/role/square/top-vest.pxmodel +109 -0
- package/kits/hub-game/models/role/tool-bag.pxmodel +75 -0
- package/kits/hub-game/models/role/tool-bell.pxmodel +71 -0
- package/kits/hub-game/models/role/tool-hose.pxmodel +60 -0
- package/kits/hub-game/models/role/tool-parcel.pxmodel +61 -0
- package/kits/hub-game/models/role/tool-torch.pxmodel +55 -0
- package/kits/hub-game/models/role/tool-tray.pxmodel +75 -0
- package/kits/hub-game/models/role/top-apron.pxmodel +70 -0
- package/kits/hub-game/models/role/top-cardigan.pxmodel +102 -0
- package/kits/hub-game/models/role/top-coat.pxmodel +79 -0
- package/kits/hub-game/models/role/top-scrubs.pxmodel +54 -0
- package/kits/hub-game/models/role/top-tabard.pxmodel +70 -0
- package/kits/hub-game/models/role/top-vest.pxmodel +70 -0
- package/kits/hub-game/package-lock.json +1 -1
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/check.mjs +7 -4
- package/kits/hub-game/scripts/fixtures/car-vectors.json +279 -0
- package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +2 -1
- package/kits/hub-game/scripts/fixtures/hud-surface.json +670 -0
- package/kits/hub-game/scripts/fixtures/talk-surface.json +3930 -0
- package/kits/hub-game/scripts/gen-hats.mjs +46 -2
- package/kits/hub-game/scripts/gen-role.mjs +80 -0
- package/kits/hub-game/scripts/test-act-core.mjs +167 -0
- package/kits/hub-game/scripts/test-actions-client.mjs +34 -0
- package/kits/hub-game/scripts/test-actors.mjs +48 -0
- package/kits/hub-game/scripts/test-analytics.mjs +100 -6
- package/kits/hub-game/scripts/test-apply-deck.mjs +122 -0
- package/kits/hub-game/scripts/test-back-visibility.mjs +3 -1
- package/kits/hub-game/scripts/test-ballot.mjs +2 -0
- package/kits/hub-game/scripts/test-car.mjs +43 -0
- package/kits/hub-game/scripts/test-decor-client.mjs +2 -1
- package/kits/hub-game/scripts/test-decor.mjs +77 -0
- package/kits/hub-game/scripts/test-device-class.mjs +83 -0
- package/kits/hub-game/scripts/test-door-analytics.mjs +1 -1
- package/kits/hub-game/scripts/test-emote-wheel.mjs +374 -0
- package/kits/hub-game/scripts/test-hats.mjs +5 -4
- package/kits/hub-game/scripts/test-host-go.mjs +265 -0
- package/kits/hub-game/scripts/test-hub-controls.mjs +108 -0
- package/kits/hub-game/scripts/test-hub-entry.mjs +531 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +103 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +20 -1
- package/kits/hub-game/scripts/test-hud-clear.mjs +12 -2
- package/kits/hub-game/scripts/test-hud-rate.mjs +6 -2
- package/kits/hub-game/scripts/test-hud-surface.mjs +272 -0
- package/kits/hub-game/scripts/test-idle.mjs +347 -1
- package/kits/hub-game/scripts/test-interest-cells.mjs +8 -1
- package/kits/hub-game/scripts/test-invites-client.mjs +42 -1
- package/kits/hub-game/scripts/test-load-abandon.mjs +2 -2
- package/kits/hub-game/scripts/test-manifest.mjs +30 -0
- package/kits/hub-game/scripts/test-menu-client.mjs +1 -1
- package/kits/hub-game/scripts/test-no-trips.mjs +42 -0
- package/kits/hub-game/scripts/test-placement-cache.mjs +1 -1
- package/kits/hub-game/scripts/test-quality.mjs +13 -0
- package/kits/hub-game/scripts/test-rehello.mjs +258 -3
- package/kits/hub-game/scripts/test-resume-pose-server.mjs +203 -0
- package/kits/hub-game/scripts/test-resync.mjs +196 -0
- package/kits/hub-game/scripts/test-role-client.mjs +427 -0
- package/kits/hub-game/scripts/test-role.mjs +350 -0
- package/kits/hub-game/scripts/test-safe-layout.mjs +50 -1
- package/kits/hub-game/scripts/test-soundscape.mjs +124 -7
- package/kits/hub-game/scripts/test-standing.mjs +15 -1
- package/kits/hub-game/scripts/test-talk-surface.mjs +296 -0
- package/kits/hub-game/scripts/test-tiers.mjs +13 -0
- package/kits/hub-game/scripts/test-track-queue.mjs +64 -0
- package/kits/hub-game/scripts/test-ui-dom-dom.mjs +96 -0
- package/kits/hub-game/scripts/test-ui-tree.mjs +711 -0
- package/kits/hub-game/scripts/test-vehicle-client.mjs +168 -0
- package/kits/hub-game/scripts/test-vehicle.mjs +135 -0
- package/kits/hub-game/scripts/test-world-labels.mjs +40 -0
- package/kits/hub-game/scripts/test-yours.mjs +33 -0
- package/kits/hub-game/tools/hubScene/export.mjs +613 -0
- package/kits/hub-game/tools/hubScene/hubdata.mjs +921 -0
- package/kits/hub-game/tools/hubScene/merge-root.mjs +39 -0
- package/kits/hub-game/tools/hubScene/nodeworld.mjs +227 -0
- package/kits/hub-game/tools/hubScene/test/export-fixture.mjs +810 -0
- package/kits/hub-game/tools/hubScene/test/fixture.json +637 -0
- package/kits/hub-game/tools/hubScene/test/paint-export.mjs +40 -0
- package/kits/hub-game/tools/hubScene/test/upload.test.mjs +655 -0
- package/kits/hub-game/tools/hubScene/upload.mjs +636 -0
- package/kits/hub-game/tools/hubScene/validate-manifest.mjs +301 -0
- package/kits/multiplayer-3d/castle.json +4 -4
- package/kits/multiplayer-3d/code/client/resumeGate.js +30 -2
- package/kits/multiplayer-3d/code/client/steer.js +3 -1
- package/kits/multiplayer-3d/code/systems/multiplayer.js +30 -2
- package/kits/multiplayer-3d/package-lock.json +1 -1
- package/kits/multiplayer-3d/scripts/test-resume-gate.mjs +74 -2
- package/kits/physics-3d/CLAUDE.md +1 -1
- package/kits/physics-3d/behaviors/Player.jsx +37 -27
- package/kits/physics-3d/castle.json +2 -2
- package/kits/physics-3d/docs/engine-geometry-memory.md +23 -0
- package/kits/physics-3d/engine3d/memoryprofile.js +17 -0
- package/kits/physics-3d/engine3d/playerRun.js +6 -1
- package/kits/physics-3d/engine3d/pxmodel.js +6 -1
- package/kits/physics-3d/engine3d/world3d.js +6 -3
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/play/behaviors/Player.js +40 -28
- package/kits/physics-3d/scripts/test-memory-profile.mjs +237 -0
- package/kits/physics-3d/systems/physics3d.js +5 -2
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/client/messages.js +5 -0
- package/kits/real-time/code/server/session.js +28 -1
- package/kits/real-time/package-lock.json +1 -1
- package/package.json +5 -4
- package/vendor/three/LICENSE +21 -0
- package/vendor/three/build/three.core.js +58773 -0
- package/vendor/three/build/three.module.js +18251 -0
- package/vendor/three/examples/jsm/controls/OrbitControls.js +1860 -0
- package/vendor/three/examples/jsm/controls/TransformControls.js +1918 -0
- package/vendor/three/examples/jsm/exporters/GLTFExporter.js +3595 -0
- package/vendor/three/examples/jsm/geometries/RoundedBoxGeometry.js +216 -0
- package/vendor/three/examples/jsm/math/MeshSurfaceSampler.js +315 -0
- package/vendor/three/examples/jsm/postprocessing/EffectComposer.js +363 -0
- package/vendor/three/examples/jsm/postprocessing/MaskPass.js +195 -0
- package/vendor/three/examples/jsm/postprocessing/OutputPass.js +139 -0
- package/vendor/three/examples/jsm/postprocessing/Pass.js +191 -0
- package/vendor/three/examples/jsm/postprocessing/RenderPass.js +183 -0
- package/vendor/three/examples/jsm/postprocessing/ShaderPass.js +135 -0
- package/vendor/three/examples/jsm/shaders/CopyShader.js +52 -0
- package/vendor/three/examples/jsm/shaders/OutputShader.js +103 -0
- package/vendor/three/examples/jsm/utils/BufferGeometryUtils.js +1435 -0
- package/vendor/three/package.json +134 -0
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
// UI tree phase 2 (schema v0): the pure tree module, the DOM renderer on the
|
|
2
|
+
// stand-in DOM, the HUD and talk builders, and the chrome's sink seam.
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { fakeDom } from './fixtures/fake-dom.mjs';
|
|
5
|
+
import { dispatchUiEvent, formatClock, h, publishUiRoot, readClock, resetUiTrees, serialize, setUiTreeSink, setUiTreeTime, syncUiTreeTime, text, uiTreeDomEnabled, validate } from '../code/client/uiTree.js';
|
|
6
|
+
import { cssFor, edgeCss, lenCss, mountUiTree } from '../code/client/uiDom.js';
|
|
7
|
+
import { buildActPair, buildHud } from '../code/client/hudTree.js';
|
|
8
|
+
import { buildTalk, makeTalkView, submitCurrent } from '../code/client/talkTree.js';
|
|
9
|
+
|
|
10
|
+
let passed = 0;
|
|
11
|
+
const test = async (name, fn) => {
|
|
12
|
+
try {await fn();} catch (error) {error.message = `${name}: ${error.message}`; throw error;}
|
|
13
|
+
passed += 1;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
await test('h: keys, style, flattened children and words', () => {
|
|
17
|
+
const node = h('row', { key: 'r', style: { gap: 6 }, hub: 'x' }, ['a', null, [false, h('text', { key: 't', text: 'b' })]], 3);
|
|
18
|
+
assert.deepEqual(node, { t: 'row', k: 'r', s: { gap: 6 }, hub: 'x', c: [{ t: 'text', text: 'a' }, { t: 'text', k: 't', text: 'b' }, { t: 'text', text: '3' }] });
|
|
19
|
+
assert.deepEqual(text('hi', { key: 'k' }), { t: 'text', k: 'k', text: 'hi' });
|
|
20
|
+
assert.equal(h('spacer').c, undefined);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await test('serialize: binding ids from keyed paths; fresh closures never change the bytes', () => {
|
|
24
|
+
const build = (fn) => h('stack', { key: 'talk', on: { cancel: fn } },
|
|
25
|
+
h('column', { key: 'panel' }, h('row', { key: 'head' }, h('button', { key: 'done', on: { tap: fn } }, 'Done')), h('text', { text: 'x', on: { tap: fn } })));
|
|
26
|
+
const one = serialize(build(() => 1)), two = serialize(build(() => 2));
|
|
27
|
+
assert.equal(one.json, two.json);
|
|
28
|
+
assert.deepEqual([...one.handlers.keys()], ['talk#cancel', 'talk/panel/head/done#tap', 'talk/panel/#1#tap']);
|
|
29
|
+
assert.equal(two.handlers.get('talk/panel/head/done#tap')(), 2);
|
|
30
|
+
assert.equal(one.wire.c[0].c[0].c[0].on.tap, 'talk/panel/head/done#tap');
|
|
31
|
+
// Not data is dropped: functions outside `on`, undefined, NaN.
|
|
32
|
+
assert.deepEqual(serialize(h('text', { key: 'x', text: 'a', bad: () => 0, n: Number.NaN, u: undefined })).wire, { t: 'text', k: 'x', text: 'a' });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await test('validate: the closed schema', () => {
|
|
36
|
+
assert.deepEqual(validate(h('column', { key: 'a', style: { gap: 2 } }, h('text', { text: 'x' }))), []);
|
|
37
|
+
const problems = validate(h('column', { key: 'a', style: { margin: 2 }, at: { middle: 1 }, on: { hover: () => 0 } }, h('text', { key: 'k' }), h('text', { key: 'k' }), h('blob')));
|
|
38
|
+
assert.deepEqual(problems, ['a: unknown style margin', 'a: unknown placement middle', 'a: unknown event hover', 'a: duplicate key k', 'a/#2: unknown type blob']);
|
|
39
|
+
assert.deepEqual(validate(h('text', {}, 'child')), ['#0: text has children']);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await test('Len, edges and styles: the strings the chrome writes', () => {
|
|
43
|
+
assert.equal(lenCss([{ vw: 92 }, { px: 420 }]), 'min(92vw, 420px)');
|
|
44
|
+
assert.equal(lenCss([{ vw: 70 }]), '70vw');
|
|
45
|
+
assert.equal(lenCss([{ vw: 100, px: -24 }]), 'calc(100vw - 24px)');
|
|
46
|
+
assert.equal(lenCss([{ dvh: 100, px: -88, safe: 'tb' }]), 'calc(100dvh - 88px - env(safe-area-inset-top) - env(safe-area-inset-bottom))');
|
|
47
|
+
assert.equal(lenCss([{ vw: 56 }, { max: [220, { vw: 100, px: -344 }] }, { px: 560 }]), 'min(56vw, max(220px, calc(100vw - 344px)), 560px)');
|
|
48
|
+
assert.equal(lenCss([{ vw: 60 }, { px: 560 }, { vw: 100, sub: { l: 16, r: 16 } }]), 'min(60vw, 560px, calc(100vw - max(16px, env(safe-area-inset-left)) - max(16px, env(safe-area-inset-right))))');
|
|
49
|
+
assert.equal(lenCss('wrap'), 'max-content');
|
|
50
|
+
assert.equal(lenCss('fill'), '100%');
|
|
51
|
+
assert.equal(edgeCss([12, 0], 'top'), 'max(12px, env(safe-area-inset-top))');
|
|
52
|
+
assert.equal(edgeCss([54, 48], 'bottom'), 'max(54px, calc(env(safe-area-inset-bottom) + 48px))');
|
|
53
|
+
assert.equal(edgeCss(12, 'left'), '12px');
|
|
54
|
+
assert.deepEqual(cssFor(h('text', { key: 't', text: 'x', at: { anchor: 'top', top: [12, 0], z: 2 }, style: { pad: [8, 14], radius: 8, bg: 'toast', w: 'wrap', box: 'border', gone: true } }), 'stack'), {
|
|
55
|
+
position: 'absolute', top: 'max(12px, env(safe-area-inset-top))', left: '50%', transform: 'translateX(-50%)', zIndex: '2', display: 'none',
|
|
56
|
+
boxSizing: 'border-box', width: 'max-content', padding: '8px 14px', background: 'rgba(20,16,12,0.8)', borderRadius: '8px',
|
|
57
|
+
});
|
|
58
|
+
// A two-line hard clip: lines × size × line height + padding.
|
|
59
|
+
assert.equal(cssFor(h('text', { style: { size: 13, lh: 1.3, pad: [4, 8], lines: 2, ellipsis: false } }), 'column').maxHeight, '41.8px');
|
|
60
|
+
// A button's own font, color and box start from the page's.
|
|
61
|
+
const button = cssFor(h('button', { style: { size: 13, lh: 'normal' } }), 'row');
|
|
62
|
+
assert.deepEqual(Object.keys(button).slice(0, 2), ['font', 'color']);
|
|
63
|
+
assert.equal(button.font, 'inherit');
|
|
64
|
+
assert.equal(button.pointerEvents, 'auto');
|
|
65
|
+
// A plain column of blocks is a block box; one with a gap is a flex column.
|
|
66
|
+
assert.equal(cssFor(h('column', {}, h('text', { text: 'a' })), 'stack').display, 'block');
|
|
67
|
+
assert.equal(cssFor(h('column', { style: { gap: 4 } }, h('text', { text: 'a' })), 'stack').display, 'flex');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// The stand-in DOM, with focus and blur counted.
|
|
71
|
+
function stage(view = { viewW: 390, viewH: 844 }) {
|
|
72
|
+
const dom = fakeDom(view);
|
|
73
|
+
const create = document.createElement;
|
|
74
|
+
const calls = [];
|
|
75
|
+
document.createElement = (tag) => {
|
|
76
|
+
const node = create(tag);
|
|
77
|
+
node.focus = () => {calls.push(['focus', node]); document.activeElement = node;};
|
|
78
|
+
node.blur = () => {calls.push(['blur', node]); if (document.activeElement === node) {document.activeElement = null;}};
|
|
79
|
+
return node;
|
|
80
|
+
};
|
|
81
|
+
const container = document.createElement('div');
|
|
82
|
+
dom.body.appendChild(container);
|
|
83
|
+
return { dom, container, calls };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
await test('uiDom: keyed reconcile keeps elements, reorders, removes; words as text', () => {
|
|
87
|
+
const { container } = stage();
|
|
88
|
+
const made = [], gone = [];
|
|
89
|
+
const ui = mountUiTree(container, { created: (path) => made.push(path), removed: (path) => gone.push(path) });
|
|
90
|
+
const tree = (keys, label = 'Go') => h('stack', { key: 'r' }, h('column', { key: 'list', style: { gap: 2 } }, keys.map((k) => h('button', { key: k }, `${label} ${k}`))));
|
|
91
|
+
ui.render(tree(['a', 'b', 'c']));
|
|
92
|
+
const list = ui.nodeFor('r/list'), a = ui.nodeFor('r/list/a'), c = ui.nodeFor('r/list/c');
|
|
93
|
+
assert.equal(container.children[0], list);
|
|
94
|
+
assert.deepEqual(list.children.map((n) => n.textContent), ['Go a', 'Go b', 'Go c']);
|
|
95
|
+
assert.equal(a.type, 'button');
|
|
96
|
+
ui.render(tree(['c', 'a'], 'Went'));
|
|
97
|
+
assert.equal(ui.nodeFor('r/list/a'), a, 'a kept');
|
|
98
|
+
assert.equal(ui.nodeFor('r/list/c'), c, 'c kept');
|
|
99
|
+
assert.deepEqual(list.children, [c, a], 'reordered');
|
|
100
|
+
assert.equal(a.textContent, 'Went a');
|
|
101
|
+
assert.equal(ui.nodeFor('r/list/b'), null);
|
|
102
|
+
assert.deepEqual(gone, ['r/list/b']);
|
|
103
|
+
assert.deepEqual(made, ['r/list/a', 'r/list/b', 'r/list/c', 'r/list'], 'created once drawn, children first');
|
|
104
|
+
// A type with another tag is a new element; the same tag restyles in place.
|
|
105
|
+
ui.render(h('stack', { key: 'r' }, h('row', { key: 'list', style: { gap: 2 } })));
|
|
106
|
+
assert.equal(ui.nodeFor('r/list'), list);
|
|
107
|
+
assert.equal(list.style.flexDirection, '');
|
|
108
|
+
ui.render(h('stack', { key: 'r' }, h('text', { key: 'list', text: 'x' })));
|
|
109
|
+
assert.equal(ui.nodeFor('r/list'), list, 'div to div');
|
|
110
|
+
ui.render(h('stack', { key: 'r' }, h('button', { key: 'list' }, 'x')));
|
|
111
|
+
assert.notEqual(ui.nodeFor('r/list'), list);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
await test('uiDom: only changed declarations are written; foreign writes are kept and restored', () => {
|
|
115
|
+
const { dom, container } = stage();
|
|
116
|
+
const ui = mountUiTree(container);
|
|
117
|
+
const toast = (wrap) => h('stack', { key: 'r' }, h('text', { key: 't', text: 'hi', at: { anchor: 'top', top: [12, 0], avoid: ['x'] }, style: { maxW: wrap ? [{ vw: 90 }] : [{ vw: 100, px: -24 }], ...(wrap ? { wrap: 'anywhere' } : {}) } }));
|
|
118
|
+
ui.render(toast(false));
|
|
119
|
+
const node = ui.nodeFor('r/t');
|
|
120
|
+
dom.log.length = 0;
|
|
121
|
+
ui.render(toast(false));
|
|
122
|
+
assert.deepEqual(dom.log, [], 'an unchanged tree writes nothing');
|
|
123
|
+
// Measured placement owns an avoid node's edges after it is made.
|
|
124
|
+
node.style.top = '40px';
|
|
125
|
+
ui.render(h('stack', { key: 'r' }, h('text', { key: 't', text: 'hi', at: { anchor: 'top', top: [20, 0], avoid: ['x'] } })));
|
|
126
|
+
assert.equal(node.style.top, '40px');
|
|
127
|
+
// Someone else's max width comes back when the temporary one ends.
|
|
128
|
+
ui.render(toast(false));
|
|
129
|
+
node.style.maxWidth = '240px';
|
|
130
|
+
ui.render(toast(true));
|
|
131
|
+
assert.equal(node.style.maxWidth, '90vw');
|
|
132
|
+
assert.equal(node.style.overflowWrap, 'anywhere');
|
|
133
|
+
ui.render(toast(false));
|
|
134
|
+
assert.equal(node.style.maxWidth, '240px');
|
|
135
|
+
assert.equal(node.style.overflowWrap, '');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
await test('uiDom: a field is controlled by its version; focus is a request counter', () => {
|
|
139
|
+
const { container, calls } = stage();
|
|
140
|
+
const ui = mountUiTree(container);
|
|
141
|
+
const seen = [];
|
|
142
|
+
const field = (props) => h('stack', { key: 'r' }, h('field', { key: 'f', name: 'say', max: 280, ph: 'Say…', enter: 'send', cap: 'sentences', spell: true, on: { input: (e) => seen.push(['input', e]), submit: (e) => seen.push(['submit', e]), cancel: () => seen.push(['cancel']) }, ...props }));
|
|
143
|
+
ui.render(field({ value: '', ver: 1, focus: 0 }));
|
|
144
|
+
const input = ui.nodeFor('r/f');
|
|
145
|
+
assert.equal(input.tag, 'input');
|
|
146
|
+
assert.deepEqual([input.type, input.name, input.maxLength, input.placeholder, input.enterKeyHint, input.autocomplete, input.attrs.autocapitalize, input.spellcheck], ['text', 'say', 280, 'Say…', 'send', 'off', 'sentences', true]);
|
|
147
|
+
input.value = 'hel';
|
|
148
|
+
input.handlers.input({});
|
|
149
|
+
assert.deepEqual(seen.pop(), ['input', { text: 'hel', base: 1, composing: false }]);
|
|
150
|
+
// A rebuild with the same version never touches what is typed.
|
|
151
|
+
ui.render(field({ value: '', ver: 1, focus: 0 }));
|
|
152
|
+
assert.equal(input.value, 'hel');
|
|
153
|
+
// A write (a cleared field) moves the version and is adopted.
|
|
154
|
+
ui.render(field({ value: '', ver: 2, focus: 0 }));
|
|
155
|
+
assert.equal(input.value, '');
|
|
156
|
+
// Enter submits its own words; never while an IME composes; Escape cancels.
|
|
157
|
+
input.value = 'hi there';
|
|
158
|
+
const key = (k, extra = {}) => ({ key: k, preventDefault() {}, stopPropagation() {}, ...extra });
|
|
159
|
+
input.handlers.keydown(key('Enter', { isComposing: true }));
|
|
160
|
+
input.handlers.keydown(key('Enter', { keyCode: 229 }));
|
|
161
|
+
assert.equal(seen.length, 0);
|
|
162
|
+
input.handlers.keydown(key('Enter'));
|
|
163
|
+
assert.deepEqual(seen.pop(), ['submit', { text: 'hi there', base: 2 }]);
|
|
164
|
+
input.handlers.keydown(key('Escape'));
|
|
165
|
+
assert.deepEqual(seen.pop(), ['cancel']);
|
|
166
|
+
// Focus requests: up focuses, the same count does nothing, negative blurs.
|
|
167
|
+
ui.render(field({ value: '', ver: 2, focus: 1 }));
|
|
168
|
+
ui.render(field({ value: '', ver: 2, focus: 1 }));
|
|
169
|
+
ui.render(field({ value: '', ver: 2, focus: -2 }));
|
|
170
|
+
assert.deepEqual(calls.map(([what]) => what), ['focus', 'blur']);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
await test('uiDom: buttons fire on press or release with the newest closure; holds report their pointer', () => {
|
|
174
|
+
const { container } = stage();
|
|
175
|
+
const ui = mountUiTree(container);
|
|
176
|
+
const got = [];
|
|
177
|
+
const event = (extra = {}) => ({ pointerId: 7, defaulted: false, stopped: false, preventDefault() {this.defaulted = true;}, stopPropagation() {this.stopped = true;}, ...extra });
|
|
178
|
+
const tree = (n) => h('stack', { key: 'r' },
|
|
179
|
+
h('button', { key: 'down', fire: 'down', on: { tap: () => got.push(`down${n}`) } }, 'D'),
|
|
180
|
+
h('button', { key: 'keep', fire: 'down', keepFocus: true, on: { tap: () => got.push(`keep${n}`) } }, 'K'),
|
|
181
|
+
h('button', { key: 'up', on: { tap: () => got.push(`up${n}`) } }, 'U'),
|
|
182
|
+
h('button', { key: 'hold', on: { press: (e) => got.push(`press${e.pointerId}`), release: (e) => got.push(`release${e.pointerId}`), cancel: () => got.push('cancel') } }, 'H'));
|
|
183
|
+
ui.render(tree(1));
|
|
184
|
+
ui.render(tree(2));
|
|
185
|
+
const down = event();
|
|
186
|
+
ui.nodeFor('r/down').handlers.pointerdown(down);
|
|
187
|
+
assert(down.stopped && !down.defaulted, 'a press is the button\'s, focus moves as it always did');
|
|
188
|
+
const keep = event();
|
|
189
|
+
ui.nodeFor('r/keep').handlers.pointerdown(keep);
|
|
190
|
+
assert(keep.stopped && keep.defaulted, 'keepFocus leaves focus where it is');
|
|
191
|
+
const up = event();
|
|
192
|
+
ui.nodeFor('r/up').handlers.pointerdown(up);
|
|
193
|
+
assert(up.stopped, 'touch never reaches the scene');
|
|
194
|
+
ui.nodeFor('r/up').handlers.click(event());
|
|
195
|
+
const hold = ui.nodeFor('r/hold');
|
|
196
|
+
hold.handlers.pointerdown(event());
|
|
197
|
+
hold.handlers.pointerup(event());
|
|
198
|
+
hold.handlers.lostpointercapture(event());
|
|
199
|
+
assert.deepEqual(got, ['down2', 'keep2', 'up2', 'press7', 'release7', 'cancel']);
|
|
200
|
+
assert.equal(hold.style.touchAction, 'none');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
await test('builders: every HUD and talk variant is a valid tree, and stable bytes', () => {
|
|
204
|
+
const on = { back() {} };
|
|
205
|
+
const pairOn = { press() {}, release() {}, cancel() {}, jump() {} };
|
|
206
|
+
for (const landscape of [false, true]) {
|
|
207
|
+
for (const safeInsets of [false, true]) {
|
|
208
|
+
for (const hud of [{ show: false }, { show: true, title: 'Town', big: '', prompt: 'Go.', promptShown: true }, { show: true, title: '', big: '41.6', prompt: '', promptShown: false }]) {
|
|
209
|
+
for (const toast of [{ text: '' }, { text: 'Bo is here' }, { text: 'x'.repeat(120), wrap: true }]) {
|
|
210
|
+
const state = { view: { landscape, safeInsets }, back: { show: true, label: 'Town' }, hud, toast, actPair: { show: true, enabled: false, on: pairOn }, on };
|
|
211
|
+
const tree = buildHud(state);
|
|
212
|
+
assert.deepEqual(validate(tree), [], JSON.stringify(state));
|
|
213
|
+
assert.equal(serialize(tree).json, serialize(buildHud({ ...state, on: { back() {} } })).json);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const pair = serialize(buildActPair({ show: true, enabled: true, on: pairOn }), 'hud');
|
|
219
|
+
assert.deepEqual([...pair.handlers.keys()], ['hud/act#press', 'hud/act#release', 'hud/act#cancel', 'hud/jump#tap']);
|
|
220
|
+
const talks = [];
|
|
221
|
+
for (const mind of [false, true]) {
|
|
222
|
+
const view = makeTalkView();
|
|
223
|
+
view.openTalk('Biscuit, the puppy', { mind });
|
|
224
|
+
view.setTyping(true);
|
|
225
|
+
talks.push(structuredClone(view.state));
|
|
226
|
+
view.line('them', 'Woof!');
|
|
227
|
+
view.line('me', 'Hi');
|
|
228
|
+
view.line('them', 'A scripted line', 'Scripted reply: Biscuit is busy with someone else');
|
|
229
|
+
view.setAsks(['a', 'b', 'c', 'd']);
|
|
230
|
+
talks.push(structuredClone(view.state));
|
|
231
|
+
view.setAsks([]);
|
|
232
|
+
view.setWaiting(true);
|
|
233
|
+
talks.push(structuredClone(view.state));
|
|
234
|
+
view.setTyping(false);
|
|
235
|
+
view.setHint('Talking with Biscuit is for players 13 and up');
|
|
236
|
+
view.setAsks([{ id: 'hi', label: 'Hi!' }, { id: 'a', label: 'A' }, { id: 'b', label: 'B' }, { id: 'c', label: 'C' }, { id: 'd', label: 'D' }]);
|
|
237
|
+
talks.push(structuredClone(view.state));
|
|
238
|
+
}
|
|
239
|
+
for (const state of talks) {
|
|
240
|
+
for (const env of [{}, { landscape: true, w: 700 }, { landscape: true, w: 932 }, { desktop: true, safeInsets: true }]) {
|
|
241
|
+
assert.deepEqual(validate(buildTalk(state, { ...env, on: {} })), []);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const tree = buildTalk(talks[7], {});
|
|
245
|
+
const asks = tree.c[0].c.find((n) => n.k === 'asks');
|
|
246
|
+
assert.equal(asks.c.length, 4, 'a child gets four choices and no "or try:"');
|
|
247
|
+
assert.equal(tree.c[0].c.at(-1).k, 'asks', 'a mind\'s choices come after the field');
|
|
248
|
+
const authored = buildTalk(talks[1], {});
|
|
249
|
+
assert.deepEqual(authored.c[0].c.map((n) => n.k), ['head', 'log', 'asks', 'form'], 'an authored NPC\'s choices come before the field');
|
|
250
|
+
assert.equal(authored.c[0].c[2].c.length, 3, 'three questions at most');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
await test('talk view: keys per line, the opening, scroll rules, field versions', () => {
|
|
254
|
+
const view = makeTalkView();
|
|
255
|
+
view.openTalk('Rowan', { mind: true });
|
|
256
|
+
view.line('them', 'Hi');
|
|
257
|
+
const opening = view.state.lines[0].key;
|
|
258
|
+
assert.equal(view.state.opening, opening);
|
|
259
|
+
assert.deepEqual(view.state.follow, { key: opening, mode: 'top', endPx: 8 });
|
|
260
|
+
view.line('me', 'Yo');
|
|
261
|
+
assert.equal(view.state.follow.mode, 'end');
|
|
262
|
+
assert(view.setOpening('Hello'));
|
|
263
|
+
assert.equal(view.state.lines[0].text, 'Hello');
|
|
264
|
+
view.setWaiting(true);
|
|
265
|
+
assert(view.state.typing);
|
|
266
|
+
view.line('them', 'Reply');
|
|
267
|
+
assert(!view.state.typing, 'a reply takes the dots away');
|
|
268
|
+
const ver = view.state.field.ver;
|
|
269
|
+
view.input({ text: 'old', base: ver - 1 });
|
|
270
|
+
assert.equal(view.state.field.value, '', 'input from before a reset is dropped');
|
|
271
|
+
view.input({ text: 'new', base: ver });
|
|
272
|
+
assert.equal(view.state.field.value, 'new');
|
|
273
|
+
view.submitted();
|
|
274
|
+
assert.deepEqual([view.state.field.value, view.state.field.ver], ['', ver + 1]);
|
|
275
|
+
const refollow = view.state.refollow;
|
|
276
|
+
view.setAsks(['a']);
|
|
277
|
+
assert.equal(view.state.refollow, refollow + 1, 'chips arriving re-apply the last alignment');
|
|
278
|
+
view.openTalk('Mabel', {});
|
|
279
|
+
assert.notEqual(view.state.lines[0]?.key, opening);
|
|
280
|
+
view.line('them', 'x');
|
|
281
|
+
assert.notEqual(view.state.lines[0].key, opening, 'a new conversation never reuses a line key');
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
await test('sink: changed roots only, newest closures, replay, drop', () => {
|
|
285
|
+
resetUiTrees();
|
|
286
|
+
const sent = [];
|
|
287
|
+
let calls = 0;
|
|
288
|
+
const tree = (label) => h('stack', { key: 'hud' }, h('button', { key: 'back', on: { tap: () => {calls += label === 'b' ? 10 : 1;} } }, `‹ ${label}`));
|
|
289
|
+
assert.equal(publishUiRoot('hud', tree('a')), false, 'no sink: nothing sent, the root is kept');
|
|
290
|
+
setUiTreeSink((name, wire, handlers, info) => sent.push([name, wire && wire.c[0].c[0].text, info.rev]));
|
|
291
|
+
assert.deepEqual(sent, [['hud', '‹ a', 1]], 'a new sink is sent every live root');
|
|
292
|
+
publishUiRoot('hud', tree('a'));
|
|
293
|
+
assert.equal(sent.length, 1, 'same bytes, no resend');
|
|
294
|
+
assert(dispatchUiEvent('hud', 'hud/back#tap'));
|
|
295
|
+
assert.equal(calls, 1);
|
|
296
|
+
publishUiRoot('hud', tree('b'));
|
|
297
|
+
assert.deepEqual(sent.at(-1), ['hud', '‹ b', 2]);
|
|
298
|
+
assert(dispatchUiEvent('hud', 'hud/back#tap'));
|
|
299
|
+
assert.equal(calls, 11, 'the newest closure runs');
|
|
300
|
+
assert.equal(dispatchUiEvent('hud', 'hud/gone#tap'), false);
|
|
301
|
+
publishUiRoot('hud', null);
|
|
302
|
+
assert.deepEqual(sent.at(-1), ['hud', null, 3]);
|
|
303
|
+
assert.equal(dispatchUiEvent('hud', 'hud/back#tap'), false, 'a dropped root takes no events');
|
|
304
|
+
assert(uiTreeDomEnabled());
|
|
305
|
+
setUiTreeSink(() => {}, { dom: false });
|
|
306
|
+
assert(!uiTreeDomEnabled());
|
|
307
|
+
resetUiTrees();
|
|
308
|
+
assert(uiTreeDomEnabled());
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
await test('chrome: the HUD and talk roots reach a sink, and events come back', async () => {
|
|
312
|
+
resetUiTrees();
|
|
313
|
+
stage();
|
|
314
|
+
globalThis.matchMedia = () => ({ matches: false });
|
|
315
|
+
const { makeChrome } = await import('../code/client/chrome.js');
|
|
316
|
+
const roots = new Map();
|
|
317
|
+
let sends = 0;
|
|
318
|
+
setUiTreeSink((name, wire) => {roots.set(name, wire); sends += 1;});
|
|
319
|
+
let backs = 0;
|
|
320
|
+
const chrome = makeChrome({ avatarCount: 4, onBack: () => {backs += 1;}, onAvatar() {} });
|
|
321
|
+
const said = [];
|
|
322
|
+
chrome.onTalk({ open() {}, send: (t) => said.push(t), end: () => said.push('<end>') });
|
|
323
|
+
chrome.showBack(true, 'Town');
|
|
324
|
+
chrome.setHud({ title: 'Fishing', text: 'Cast. Then reel.' }, 41.6);
|
|
325
|
+
const before = sends;
|
|
326
|
+
for (let i = 0; i < 5; i++) {chrome.setHud({ title: 'Fishing', text: 'Cast. Then reel.' }, 41.6);}
|
|
327
|
+
assert.equal(sends, before, 'an unchanged frame sends nothing');
|
|
328
|
+
const hud = roots.get('hud');
|
|
329
|
+
assert.equal(hud.c.find((n) => n.k === 'back').c[0].text, '‹ Town');
|
|
330
|
+
assert.equal(hud.c.find((n) => n.k === 'prompt').text, 'Cast.', 'the first sentence only');
|
|
331
|
+
assert.equal(hud.c.find((n) => n.k === 'hud').c.find((n) => n.k === 'big').text, '41.6');
|
|
332
|
+
assert(dispatchUiEvent('hud', 'hud/back#tap'));
|
|
333
|
+
assert.equal(backs, 1);
|
|
334
|
+
chrome.openTalk('Biscuit, the puppy', { mind: true });
|
|
335
|
+
chrome.talkLine('them', 'Woof!');
|
|
336
|
+
chrome.talkAsks(['Where?', 'Who?'], (ask) => said.push(`ask:${ask}`));
|
|
337
|
+
const talk = roots.get('talk');
|
|
338
|
+
const panel = talk.c[0];
|
|
339
|
+
assert.equal(panel.s.gone, false);
|
|
340
|
+
assert.deepEqual(panel.c.map((n) => n.k), ['head', 'log', 'form', 'asks']);
|
|
341
|
+
assert(dispatchUiEvent('talk', 'talk/panel/asks/a1#tap'));
|
|
342
|
+
assert.deepEqual(said.at(-1), 'ask:Who?');
|
|
343
|
+
// While the page draws the panel the draft is the input's own value.
|
|
344
|
+
chrome.talkDraft('half a question');
|
|
345
|
+
assert.equal(chrome.talkDraft(), 'half a question');
|
|
346
|
+
assert(dispatchUiEvent('talk', 'talk/panel/form/send#tap'));
|
|
347
|
+
assert.equal(said.at(-1), 'half a question');
|
|
348
|
+
assert.equal(chrome.talkDraft(), '', 'sending clears the field');
|
|
349
|
+
assert(dispatchUiEvent('talk', 'talk/panel/head/done#tap'));
|
|
350
|
+
assert.equal(said.at(-1), '<end>');
|
|
351
|
+
// A host that draws the trees itself: the page stands down, and the
|
|
352
|
+
// field's state is the mirror of the host's events.
|
|
353
|
+
setUiTreeSink((name, wire) => roots.set(name, wire), { dom: false });
|
|
354
|
+
const ver = roots.get('talk').c[0].c.find((n) => n.k === 'form').c[0].ver;
|
|
355
|
+
dispatchUiEvent('talk', 'talk/panel/form/say#focus', {});
|
|
356
|
+
assert.equal(chrome.talkFocused(), true);
|
|
357
|
+
dispatchUiEvent('talk', 'talk/panel/form/say#input', { text: 'typed natively', base: ver });
|
|
358
|
+
assert.equal(chrome.talkDraft(), 'typed natively');
|
|
359
|
+
dispatchUiEvent('talk', 'talk/panel/form/say#submit', { text: 'typed natively', base: ver });
|
|
360
|
+
assert.equal(said.at(-1), 'typed natively');
|
|
361
|
+
const field = roots.get('talk').c[0].c.find((n) => n.k === 'form').c[0];
|
|
362
|
+
assert.deepEqual([field.value, field.ver], ['', ver + 1], 'submit clears by a version bump');
|
|
363
|
+
dispatchUiEvent('talk', 'talk/panel/form/say#blur', {});
|
|
364
|
+
assert.equal(chrome.talkFocused(), false);
|
|
365
|
+
chrome.closeTalk();
|
|
366
|
+
assert.equal(roots.get('talk').c[0].s.gone, true);
|
|
367
|
+
chrome.dispose();
|
|
368
|
+
assert.equal(roots.get('hud'), null, 'dispose drops the roots');
|
|
369
|
+
resetUiTrees();
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
await test('paths: a key with a slash is one escaped segment, never another node\'s path', () => {
|
|
373
|
+
const tree = h('stack', { key: 'r' },
|
|
374
|
+
h('button', { key: 'a/b', on: { tap: () => 'slash' } }, 'x'),
|
|
375
|
+
h('column', { key: 'a' }, h('button', { key: 'b', on: { tap: () => 'nested' } }, 'y')),
|
|
376
|
+
h('button', { key: '%2F', on: { tap: () => 'percent' } }, 'z'));
|
|
377
|
+
const { handlers, wire } = serialize(tree);
|
|
378
|
+
assert.deepEqual([...handlers.keys()], ['r/a%2Fb#tap', 'r/a/b#tap', 'r/%252F#tap']);
|
|
379
|
+
assert.deepEqual([...handlers.values()].map((fn) => fn()), ['slash', 'nested', 'percent']);
|
|
380
|
+
assert.equal(wire.c[0].k, 'a/b', 'the wire keeps the key as given');
|
|
381
|
+
assert.deepEqual(validate(tree), []);
|
|
382
|
+
resetUiTrees();
|
|
383
|
+
publishUiRoot('r', tree);
|
|
384
|
+
assert.equal(dispatchUiEvent('r', 'r/a%2Fb#tap'), true);
|
|
385
|
+
// The page draws both, each at its own path.
|
|
386
|
+
const { container } = stage();
|
|
387
|
+
const ui = mountUiTree(container);
|
|
388
|
+
ui.render(tree);
|
|
389
|
+
assert.equal(ui.nodeFor('r/a%2Fb').textContent, 'x');
|
|
390
|
+
assert.equal(ui.nodeFor('r/a/b').textContent, 'y');
|
|
391
|
+
assert.notEqual(ui.nodeFor('r/a%2Fb'), ui.nodeFor('r/a/b'));
|
|
392
|
+
resetUiTrees();
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
await test('talk: a submit is taken only at the field\'s current version, in an open panel', () => {
|
|
396
|
+
const talk = makeTalkView();
|
|
397
|
+
talk.openTalk('A');
|
|
398
|
+
const verA = talk.state.field.ver;
|
|
399
|
+
assert(submitCurrent(talk.state, { text: 'hi', base: verA }));
|
|
400
|
+
assert(!submitCurrent(talk.state, { text: 'hi' }), 'no version');
|
|
401
|
+
talk.closeTalk();
|
|
402
|
+
assert(!submitCurrent(talk.state, { text: 'hi', base: verA }), 'closed');
|
|
403
|
+
talk.openTalk('B');
|
|
404
|
+
assert(!submitCurrent(talk.state, { text: 'hi', base: verA }), 'another conversation');
|
|
405
|
+
assert(submitCurrent(talk.state, { text: 'hi', base: talk.state.field.ver }));
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
await test('chrome: a late submit from a closed conversation reaches neither the next NPC nor its draft', async () => {
|
|
409
|
+
resetUiTrees();
|
|
410
|
+
stage();
|
|
411
|
+
globalThis.matchMedia = () => ({ matches: false });
|
|
412
|
+
const { makeChrome } = await import('../code/client/chrome.js');
|
|
413
|
+
const roots = new Map();
|
|
414
|
+
setUiTreeSink((name, wire) => roots.set(name, wire), { dom: false });
|
|
415
|
+
const chrome = makeChrome({ avatarCount: 4, onBack() {}, onAvatar() {} });
|
|
416
|
+
const said = [];
|
|
417
|
+
chrome.onTalk({ open() {}, send: (t) => said.push(t), end() {} });
|
|
418
|
+
const field = () => roots.get('talk').c[0].c.find((n) => n.k === 'form').c[0];
|
|
419
|
+
chrome.openTalk('Mabel', { mind: false });
|
|
420
|
+
const verA = field().ver;
|
|
421
|
+
dispatchUiEvent('talk', 'talk/panel/form/say#input', { text: 'for Mabel', base: verA });
|
|
422
|
+
chrome.closeTalk();
|
|
423
|
+
chrome.openTalk('Biscuit', { mind: true });
|
|
424
|
+
chrome.talkDraft('half a question for Biscuit');
|
|
425
|
+
const verB = field().ver;
|
|
426
|
+
assert(verB > verA);
|
|
427
|
+
dispatchUiEvent('talk', 'talk/panel/form/say#submit', { text: 'for Mabel', base: verA });
|
|
428
|
+
assert.deepEqual(said, [], 'nothing sent');
|
|
429
|
+
assert.equal(chrome.talkDraft(), 'half a question for Biscuit', 'the draft is kept');
|
|
430
|
+
assert.equal(field().ver, verB, 'the field is not cleared');
|
|
431
|
+
dispatchUiEvent('talk', 'talk/panel/form/say#submit', { text: 'half a question for Biscuit', base: verB });
|
|
432
|
+
assert.deepEqual(said, ['half a question for Biscuit']);
|
|
433
|
+
chrome.dispose();
|
|
434
|
+
resetUiTrees();
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
await test('chrome: a talk frame that changes nothing builds, serializes and draws nothing', async () => {
|
|
438
|
+
resetUiTrees();
|
|
439
|
+
stage();
|
|
440
|
+
globalThis.matchMedia = () => ({ matches: false });
|
|
441
|
+
const { makeChrome } = await import('../code/client/chrome.js');
|
|
442
|
+
let sends = 0;
|
|
443
|
+
setUiTreeSink(() => {sends += 1;});
|
|
444
|
+
const chrome = makeChrome({ avatarCount: 4, onBack() {}, onAvatar() {} });
|
|
445
|
+
chrome.onTalk({ open() {}, send() {}, end() {} });
|
|
446
|
+
chrome.openTalk('Biscuit, the puppy', { mind: true });
|
|
447
|
+
for (let i = 0; i < 100; i++) {chrome.talkLine(i % 2 ? 'me' : 'them', `line ${i} ${'words '.repeat(6)}`);}
|
|
448
|
+
chrome.talkWaiting(false);
|
|
449
|
+
// Count every serialized character, as talk.js says talkWaiting each frame.
|
|
450
|
+
const stringify = JSON.stringify;
|
|
451
|
+
let chars = 0;
|
|
452
|
+
JSON.stringify = (...args) => {const out = stringify(...args); chars += out?.length ?? 0; return out;};
|
|
453
|
+
const frames = (n, waiting) => {for (let i = 0; i < n; i++) {chrome.talkWaiting(waiting);}};
|
|
454
|
+
let before = sends;
|
|
455
|
+
try {
|
|
456
|
+
frames(120, false);
|
|
457
|
+
assert.equal(chars, 0, 'open, unchanged: nothing serialized');
|
|
458
|
+
assert.equal(sends, before);
|
|
459
|
+
frames(60, true);
|
|
460
|
+
assert.equal(sends, before + 1, 'waiting starts: one send');
|
|
461
|
+
assert(chars > 0);
|
|
462
|
+
chars = 0;
|
|
463
|
+
frames(60, true);
|
|
464
|
+
assert.equal(chars, 0);
|
|
465
|
+
frames(1, false);
|
|
466
|
+
chrome.closeTalk();
|
|
467
|
+
before = sends;
|
|
468
|
+
chars = 0;
|
|
469
|
+
frames(120, false);
|
|
470
|
+
chrome.talkLine('them', 'a reply that came after the panel closed');
|
|
471
|
+
frames(120, false);
|
|
472
|
+
assert.equal(chars, 0, 'closed: nothing serialized');
|
|
473
|
+
assert.equal(sends, before, 'closed: nothing sent');
|
|
474
|
+
} finally {
|
|
475
|
+
JSON.stringify = stringify;
|
|
476
|
+
}
|
|
477
|
+
// The next open starts a fresh log.
|
|
478
|
+
chrome.openTalk('Mabel', { mind: false });
|
|
479
|
+
const opened = sends;
|
|
480
|
+
assert(opened > before);
|
|
481
|
+
chrome.talkLine('them', 'Hello');
|
|
482
|
+
assert.equal(sends, opened + 1);
|
|
483
|
+
chrome.dispose();
|
|
484
|
+
resetUiTrees();
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
await test('clock: absolute in sim seconds; format, clamp at min, warn below the mark', () => {
|
|
488
|
+
assert.equal(formatClock(41.65, 's.1'), (41.65).toFixed(1));
|
|
489
|
+
assert.equal(formatClock(65.2, 'm:ss'), '1:06');
|
|
490
|
+
assert.equal(formatClock(-3, 'm:ss'), '0:00');
|
|
491
|
+
// A countdown to t0 = 1012: v0 0, rate -1.
|
|
492
|
+
const clock = { gen: 1, t0: 1012, v0: 0, rate: -1, fmt: 's.1', min: 0, warn: { below: 10, s: { bg: '#e64646d9' } } };
|
|
493
|
+
assert.deepEqual(readClock(clock, 1001.5), { value: 10.5, text: '10.5', warn: false });
|
|
494
|
+
assert.deepEqual(readClock(clock, 1002.5), { value: 9.5, text: '9.5', warn: true });
|
|
495
|
+
assert.deepEqual(readClock(clock, 1030), { value: 0, text: '0.0', warn: true });
|
|
496
|
+
assert.equal(readClock({ gen: 1, t0: 100, v0: 1, rate: 1, fmt: 's.1' }, 102).text, '3.0');
|
|
497
|
+
assert.deepEqual(validate(h('text', { key: 'big', clock })), []);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
await test('uiDom: a clock reads the sim time on each draw and tick; warn style comes and goes', () => {
|
|
501
|
+
const { container } = stage();
|
|
502
|
+
let now = 1000;
|
|
503
|
+
const ui = mountUiTree(container, { clockNow: () => now });
|
|
504
|
+
const tree = (clock) => h('stack', { key: 'r' }, h('text', { key: 'big', clock, style: { bg: 'copy', size: 56 } }));
|
|
505
|
+
const warn = { below: 10, s: { bg: '#e64646d9' } };
|
|
506
|
+
const clock = { gen: 1, t0: 1011, v0: 0, rate: -1, fmt: 's.1', min: 0, warn };
|
|
507
|
+
ui.render(tree(clock));
|
|
508
|
+
const big = ui.nodeFor('r/big');
|
|
509
|
+
assert.equal(big.textContent, '11.0');
|
|
510
|
+
assert.equal(big.style.background, 'rgba(20,16,12,0.78)');
|
|
511
|
+
now = 1000.6;
|
|
512
|
+
ui.tick();
|
|
513
|
+
assert.equal(big.textContent, '10.4');
|
|
514
|
+
now = 1001.25;
|
|
515
|
+
ui.tick();
|
|
516
|
+
assert.equal(big.textContent, '9.8');
|
|
517
|
+
assert.equal(big.style.background, '#e64646d9', 'below the mark');
|
|
518
|
+
// A rebuild of the same clock reads the same time: nothing restarts.
|
|
519
|
+
ui.render(tree({ ...clock }));
|
|
520
|
+
assert.equal(big.textContent, '9.8');
|
|
521
|
+
assert.equal(big.style.background, '#e64646d9');
|
|
522
|
+
now = 1020;
|
|
523
|
+
ui.tick();
|
|
524
|
+
assert.equal(big.textContent, '0.0', 'stops at min');
|
|
525
|
+
ui.render(tree({ ...clock, gen: 2, t0: 1050 }));
|
|
526
|
+
assert.equal(big.textContent, '30.0');
|
|
527
|
+
assert.equal(big.style.background, 'rgba(20,16,12,0.78)', 'the warning ends');
|
|
528
|
+
ui.render(h('stack', { key: 'r' }, h('text', { key: 'big', text: 'Done', style: { bg: 'copy', size: 56 } })));
|
|
529
|
+
now = 1025;
|
|
530
|
+
ui.tick();
|
|
531
|
+
assert.equal(big.textContent, 'Done');
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
// The chrome on the stand-in DOM, with a sink, a page monotonic clock
|
|
535
|
+
// (`rig.mono`, ms) and a server offset (`rig.offset`): sim time is their sum.
|
|
536
|
+
// Moving `rig.sim` moves the monotonic clock (time passing); moving
|
|
537
|
+
// `rig.offset` is a server correction. `frame()` is one hub frame: setHud,
|
|
538
|
+
// then the time sync.
|
|
539
|
+
async function timerRig({ attach = true } = {}) {
|
|
540
|
+
resetUiTrees();
|
|
541
|
+
const { dom } = stage();
|
|
542
|
+
globalThis.matchMedia = () => ({ matches: false });
|
|
543
|
+
const { makeChrome } = await import('../code/client/chrome.js');
|
|
544
|
+
const realNow = performance.now;
|
|
545
|
+
const rig = {
|
|
546
|
+
mono: 1_000,
|
|
547
|
+
offset: 1_700_000_000_000,
|
|
548
|
+
get sim() {return this.mono + this.offset;},
|
|
549
|
+
set sim(value) {this.mono += value - this.sim;},
|
|
550
|
+
sent: [],
|
|
551
|
+
times: [],
|
|
552
|
+
spec: null,
|
|
553
|
+
host: null,
|
|
554
|
+
};
|
|
555
|
+
performance.now = () => rig.mono;
|
|
556
|
+
setUiTreeTime(() => rig.sim / 1000);
|
|
557
|
+
// A real host: the sim time it was last told, and its own monotonic
|
|
558
|
+
// clock at that receipt.
|
|
559
|
+
const told = (simNow) => {if (simNow !== null && simNow !== undefined) {rig.host = { simNow, mono: rig.mono };}};
|
|
560
|
+
rig.sink = (name, wire, handlers, info) => {
|
|
561
|
+
told(info.simNow);
|
|
562
|
+
if (name === 'hud') {rig.sent.push({ wire, ...info });}
|
|
563
|
+
};
|
|
564
|
+
rig.onTime = (simNow) => {rig.times.push(simNow); told(simNow);};
|
|
565
|
+
rig.attach = () => setUiTreeSink(rig.sink, { onTime: rig.onTime });
|
|
566
|
+
if (attach) {rig.attach();}
|
|
567
|
+
rig.chrome = makeChrome({ avatarCount: 4, onBack() {}, onAvatar() {} });
|
|
568
|
+
rig.frame = () => {
|
|
569
|
+
const t = rig.spec.timer;
|
|
570
|
+
const timer = Math.max(0, (t.since !== undefined ? rig.sim - t.since : t.until - rig.sim) / 1000);
|
|
571
|
+
rig.chrome.setHud(rig.spec, timer, rig.sim);
|
|
572
|
+
syncUiTreeTime();
|
|
573
|
+
return timer;
|
|
574
|
+
};
|
|
575
|
+
rig.bigEl = () => dom.made.find((node) => node.style.fontSize === '56px' && node.style.fontWeight === '800');
|
|
576
|
+
const bigOf = (wire) => wire.c.find((n) => n.k === 'hud').c.find((n) => n.k === 'big');
|
|
577
|
+
// What the host shows: the last root it got, read at the sim time it was
|
|
578
|
+
// last told plus its monotonic time since.
|
|
579
|
+
rig.hostShows = () => {
|
|
580
|
+
const last = rig.sent.at(-1);
|
|
581
|
+
return readClock(bigOf(last.wire).clock, rig.host.simNow + (rig.mono - rig.host.mono) / 1000).text;
|
|
582
|
+
};
|
|
583
|
+
rig.clockOf = (entry) => JSON.stringify(bigOf(entry.wire).clock);
|
|
584
|
+
rig.end = () => {
|
|
585
|
+
rig.chrome.dispose();
|
|
586
|
+
resetUiTrees();
|
|
587
|
+
performance.now = realNow;
|
|
588
|
+
};
|
|
589
|
+
return rig;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
await test('chrome: a running HUD timer is an absolute clock; the page reads toFixed(1) each frame', async () => {
|
|
593
|
+
const rig = await timerRig();
|
|
594
|
+
const until = rig.sim + 41_600;
|
|
595
|
+
rig.spec = { title: 'Fishing', text: 'Cast. Then reel.', timer: { until } };
|
|
596
|
+
rig.frame();
|
|
597
|
+
const first = rig.sent.length;
|
|
598
|
+
const clock = JSON.parse(rig.clockOf(rig.sent.at(-1)));
|
|
599
|
+
assert.deepEqual(clock, { gen: clock.gen, t0: until / 1000, v0: 0, rate: -1, fmt: 's.1', min: 0 });
|
|
600
|
+
assert.equal(rig.sent.at(-1).simNow, rig.sim / 1000, 'each post carries the sim time');
|
|
601
|
+
const bigEl = rig.bigEl();
|
|
602
|
+
for (let i = 0; i < 120; i++) {
|
|
603
|
+
rig.sim += 16.7;
|
|
604
|
+
const timer = rig.frame();
|
|
605
|
+
assert.equal(bigEl.textContent, timer.toFixed(1));
|
|
606
|
+
assert.equal(rig.hostShows(), timer.toFixed(1));
|
|
607
|
+
}
|
|
608
|
+
assert.equal(rig.sent.length, first, 'no resend while the timer counts');
|
|
609
|
+
// A since timer.
|
|
610
|
+
rig.spec = { title: 'Fishing', timer: { since: rig.sim - 2_000 } };
|
|
611
|
+
{const timer = rig.frame(); assert.equal(bigEl.textContent, timer.toFixed(1));}
|
|
612
|
+
assert.equal(rig.sent.length, first + 1);
|
|
613
|
+
rig.chrome.setHud({ title: 'Fishing', text: 'Cast. Then reel.' }, null, rig.sim);
|
|
614
|
+
assert.equal(rig.sent.at(-1).wire.c.find((n) => n.k === 'hud').c.find((n) => n.k === 'big').clock, undefined);
|
|
615
|
+
rig.chrome.setHud({ title: 'Fishing', big: '3 fish', timer: { until } }, 1, rig.sim);
|
|
616
|
+
assert.equal(bigEl.textContent, '3 fish');
|
|
617
|
+
rig.end();
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
await test('chrome: a server correction of 180 ms sends no root; a time sync brings the host to it', async () => {
|
|
621
|
+
const rig = await timerRig();
|
|
622
|
+
rig.spec = { title: 'Race', timer: { until: rig.sim + 30_000 } };
|
|
623
|
+
rig.frame();
|
|
624
|
+
assert.deepEqual(rig.times, [rig.sim / 1000], 'told the time on attach');
|
|
625
|
+
const sends = rig.sent.length, times = rig.times.length;
|
|
626
|
+
rig.sim += 1_000;
|
|
627
|
+
rig.frame();
|
|
628
|
+
// Jitter under the threshold: nothing.
|
|
629
|
+
rig.offset += 20;
|
|
630
|
+
rig.frame();
|
|
631
|
+
rig.offset -= 20;
|
|
632
|
+
rig.frame();
|
|
633
|
+
assert.equal(rig.times.length, times);
|
|
634
|
+
rig.offset += 180; // the server says it is 180 ms later than we thought
|
|
635
|
+
const timer = rig.frame();
|
|
636
|
+
assert.equal(rig.sent.length, sends, 'no root resend');
|
|
637
|
+
assert.equal(rig.times.length, times + 1, 'one time sync');
|
|
638
|
+
assert.equal(rig.times.at(-1), rig.sim / 1000);
|
|
639
|
+
assert.equal(timer.toFixed(1), '28.8');
|
|
640
|
+
assert.equal(rig.bigEl().textContent, '28.8');
|
|
641
|
+
assert.equal(rig.hostShows(), '28.8');
|
|
642
|
+
rig.sim += 1_000;
|
|
643
|
+
rig.frame();
|
|
644
|
+
assert.equal(rig.hostShows(), '27.8');
|
|
645
|
+
assert.equal(rig.times.length, times + 1);
|
|
646
|
+
rig.end();
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
await test('chrome: a timer restarted at the value it started at counts again from now', async () => {
|
|
650
|
+
const rig = await timerRig();
|
|
651
|
+
rig.spec = { title: 'Race', timer: { until: rig.sim + 30_000 } };
|
|
652
|
+
rig.frame();
|
|
653
|
+
assert.equal(rig.bigEl().textContent, '30.0');
|
|
654
|
+
rig.sim += 5_000;
|
|
655
|
+
rig.frame();
|
|
656
|
+
assert.equal(rig.bigEl().textContent, '25.0');
|
|
657
|
+
const before = rig.sent.length;
|
|
658
|
+
rig.spec = { title: 'Race', timer: { until: rig.sim + 30_000 } };
|
|
659
|
+
rig.frame();
|
|
660
|
+
assert.equal(rig.bigEl().textContent, '30.0', 'the page restarts it');
|
|
661
|
+
assert.equal(rig.sent.length, before + 1, 'the host is sent the restart');
|
|
662
|
+
assert.equal(rig.hostShows(), '30.0');
|
|
663
|
+
rig.sim += 1_000;
|
|
664
|
+
rig.frame();
|
|
665
|
+
assert.equal(rig.bigEl().textContent, '29.0');
|
|
666
|
+
assert.equal(rig.hostShows(), '29.0');
|
|
667
|
+
rig.end();
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
await test('sink: toast at 5 s, sink attached at 6 s, toast at 7 s of a 30 s countdown: 24.0 then 23.0, no restart', async () => {
|
|
671
|
+
const rig = await timerRig({ attach: false });
|
|
672
|
+
rig.spec = { title: 'Race', timer: { until: rig.sim + 30_000 } };
|
|
673
|
+
rig.frame();
|
|
674
|
+
for (let i = 0; i < 50; i++) {rig.sim += 100; rig.frame();}
|
|
675
|
+
rig.chrome.setToast('Bo is here'); // an unrelated rebuild at 5 s
|
|
676
|
+
for (let i = 0; i < 10; i++) {rig.sim += 100; rig.frame();}
|
|
677
|
+
rig.attach(); // attached at 6 s: the cached root, as it is
|
|
678
|
+
assert.equal(rig.sent.length, 1);
|
|
679
|
+
assert.equal(rig.sent[0].simNow, rig.sim / 1000);
|
|
680
|
+
assert.equal(rig.hostShows(), '24.0');
|
|
681
|
+
assert.equal(rig.bigEl().textContent, '24.0');
|
|
682
|
+
const replayed = rig.clockOf(rig.sent[0]);
|
|
683
|
+
for (let i = 0; i < 10; i++) {rig.sim += 100; rig.frame();}
|
|
684
|
+
rig.chrome.setToast('Ada is here'); // another rebuild at 7 s
|
|
685
|
+
assert.equal(rig.sent.length, 2);
|
|
686
|
+
assert.equal(rig.clockOf(rig.sent[1]), replayed, 'the same clock: not a restart');
|
|
687
|
+
assert.equal(rig.hostShows(), '23.0');
|
|
688
|
+
assert.equal(rig.bigEl().textContent, '23.0');
|
|
689
|
+
rig.end();
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
await test('sink: within one generation every send carries byte-identical clock fields', async () => {
|
|
693
|
+
const rig = await timerRig();
|
|
694
|
+
rig.spec = { title: 'Race', timer: { until: rig.sim + 30_000 } };
|
|
695
|
+
rig.frame();
|
|
696
|
+
const first = rig.clockOf(rig.sent.at(-1));
|
|
697
|
+
rig.sim += 2_000;
|
|
698
|
+
rig.frame();
|
|
699
|
+
rig.chrome.setToast('Bo is here');
|
|
700
|
+
rig.sim += 180;
|
|
701
|
+
rig.frame();
|
|
702
|
+
rig.chrome.showBack(true, 'Town');
|
|
703
|
+
rig.sim += 3_000;
|
|
704
|
+
rig.attach();
|
|
705
|
+
const clocks = rig.sent.filter((entry) => entry.wire).map(rig.clockOf).filter(Boolean);
|
|
706
|
+
assert(clocks.length >= 4, `${clocks.length} sends`);
|
|
707
|
+
assert(clocks.every((clock) => clock === first), clocks.join('\n'));
|
|
708
|
+
rig.end();
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
console.log(`hub-game UI tree: ${passed} tests passed`);
|