castle-web-cli 0.4.228 → 0.4.230

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 (236) hide show
  1. package/dist/agent-prompts.d.ts +11 -3
  2. package/dist/agent-prompts.js +153 -53
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +43 -33
  5. package/dist/api.d.ts +47 -2
  6. package/dist/api.js +85 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +779 -3
  9. package/dist/castleJson.d.ts +59 -0
  10. package/dist/castleJson.js +267 -3
  11. package/dist/check.js +27 -1
  12. package/dist/cloudGit.d.ts +20 -1
  13. package/dist/cloudGit.js +267 -10
  14. package/dist/contentPack.d.ts +49 -0
  15. package/dist/contentPack.js +273 -0
  16. package/dist/devExperiments.d.ts +21 -0
  17. package/dist/devExperiments.js +139 -0
  18. package/dist/devHub.d.ts +55 -0
  19. package/dist/devHub.js +187 -0
  20. package/dist/devSessionChild.js +3 -1
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +229 -44
  23. package/dist/experimentAssignment.d.ts +15 -0
  24. package/dist/experimentAssignment.js +41 -0
  25. package/dist/importCheckHook.d.ts +40 -0
  26. package/dist/importCheckHook.js +171 -0
  27. package/dist/importRegression.d.ts +15 -0
  28. package/dist/importRegression.js +100 -0
  29. package/dist/importTrust.d.ts +2 -0
  30. package/dist/importTrust.js +27 -0
  31. package/dist/importUpdateHook.d.ts +18 -0
  32. package/dist/importUpdateHook.js +31 -8
  33. package/dist/importUpdateHookRunner.js +19 -7
  34. package/dist/imports.d.ts +16 -0
  35. package/dist/imports.js +145 -33
  36. package/dist/index.js +8 -1
  37. package/dist/init.d.ts +3 -0
  38. package/dist/init.js +125 -8
  39. package/dist/install.js +4 -1
  40. package/dist/localPaths.js +4 -0
  41. package/dist/login.js +2 -2
  42. package/dist/platformDoc.js +3 -8
  43. package/dist/playPagePolicy.js +3 -1
  44. package/dist/remix.js +5 -0
  45. package/dist/routerTaskReport.d.ts +28 -0
  46. package/dist/routerTaskReport.js +56 -0
  47. package/dist/save-deck.d.ts +18 -0
  48. package/dist/save-deck.js +145 -6
  49. package/dist/serve.d.ts +8 -0
  50. package/dist/serve.js +94 -13
  51. package/dist/serverBundle.d.ts +1 -1
  52. package/dist/serverBundle.js +5 -1
  53. package/dist/sourceLineage.d.ts +42 -0
  54. package/dist/sourceLineage.js +380 -0
  55. package/dist/webSourceSync.d.ts +11 -0
  56. package/dist/webSourceSync.js +26 -9
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +301 -52
  60. package/kits/base/sdk/agent.js +4 -4
  61. package/kits/base/sdk/avatar.d.ts +12 -0
  62. package/kits/base/sdk/avatar.js +23 -0
  63. package/kits/base/sdk/castle.d.ts +6 -1
  64. package/kits/base/sdk/castle.js +3 -0
  65. package/kits/base/sdk/commands.d.ts +208 -0
  66. package/kits/base/sdk/commands.js +16 -0
  67. package/kits/base/sdk/context.d.ts +6 -0
  68. package/kits/base/sdk/experiments.d.ts +15 -0
  69. package/kits/base/sdk/experiments.js +83 -0
  70. package/kits/base/sdk/hub.d.ts +85 -0
  71. package/kits/base/sdk/hub.js +107 -0
  72. package/kits/base/sdk/multiplayer.d.ts +86 -6
  73. package/kits/base/sdk/multiplayer.js +157 -4
  74. package/kits/base/sdk/server/hub.d.ts +169 -0
  75. package/kits/base/sdk/server/hub.js +125 -0
  76. package/kits/base/sdk/server/platformHandle.d.ts +49 -2
  77. package/kits/base/sdk/server/storage.d.ts +55 -2
  78. package/kits/base/sdk/server/wrapper.d.ts +85 -6
  79. package/kits/base/sdk/server/wrapper.js +393 -30
  80. package/kits/base/sdk/transport.d.ts +2 -1
  81. package/kits/base/sdk/transport.js +13 -1
  82. package/kits/hub-game/.prettierrc +5 -0
  83. package/kits/hub-game/CLAUDE.md +146 -0
  84. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  85. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  86. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  87. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  88. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  89. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  90. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  91. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  92. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  93. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  94. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  95. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  96. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  97. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  98. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  99. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  100. package/kits/hub-game/castle.json +96 -0
  101. package/kits/hub-game/code/client/attention.js +133 -0
  102. package/kits/hub-game/code/client/avatar.js +288 -0
  103. package/kits/hub-game/code/client/cabinets.js +90 -0
  104. package/kits/hub-game/code/client/chat.js +135 -0
  105. package/kits/hub-game/code/client/chrome.js +1001 -0
  106. package/kits/hub-game/code/client/doorFrame.js +15 -0
  107. package/kits/hub-game/code/client/firstJoin.js +14 -0
  108. package/kits/hub-game/code/client/grab.js +241 -0
  109. package/kits/hub-game/code/client/hookLayer.js +230 -0
  110. package/kits/hub-game/code/client/idle.js +326 -0
  111. package/kits/hub-game/code/client/keys.js +48 -0
  112. package/kits/hub-game/code/client/pack.js +232 -0
  113. package/kits/hub-game/code/client/packFormat.js +397 -0
  114. package/kits/hub-game/code/client/say.js +127 -0
  115. package/kits/hub-game/code/client/sha256.js +80 -0
  116. package/kits/hub-game/code/client/sound.js +147 -0
  117. package/kits/hub-game/code/client/state.js +131 -0
  118. package/kits/hub-game/code/client/talk.js +259 -0
  119. package/kits/hub-game/code/client/together.js +365 -0
  120. package/kits/hub-game/code/client/wear.js +149 -0
  121. package/kits/hub-game/code/server/actors.js +627 -0
  122. package/kits/hub-game/code/server/blocks.js +103 -0
  123. package/kits/hub-game/code/server/cabinets.js +309 -0
  124. package/kits/hub-game/code/server/game.js +137 -0
  125. package/kits/hub-game/code/server/grab.js +247 -0
  126. package/kits/hub-game/code/server/history.js +96 -0
  127. package/kits/hub-game/code/server/hub.js +1904 -0
  128. package/kits/hub-game/code/server/idle.js +412 -0
  129. package/kits/hub-game/code/server/index.js +64 -0
  130. package/kits/hub-game/code/server/labels.js +59 -0
  131. package/kits/hub-game/code/server/manifest.js +527 -0
  132. package/kits/hub-game/code/server/npcs.js +745 -0
  133. package/kits/hub-game/code/server/party.js +381 -0
  134. package/kits/hub-game/code/server/place.js +43 -0
  135. package/kits/hub-game/code/server/say.js +83 -0
  136. package/kits/hub-game/code/server/wear.js +142 -0
  137. package/kits/hub-game/code/systems/hub.js +2230 -0
  138. package/kits/hub-game/docs/content-pack.md +151 -0
  139. package/kits/hub-game/docs/contract.md +1139 -0
  140. package/kits/hub-game/docs/style.md +137 -0
  141. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  142. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  143. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  144. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  145. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  146. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  147. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  148. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  149. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  150. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  151. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  152. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  153. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  154. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  155. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  156. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  157. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  158. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  159. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  160. package/kits/hub-game/eslint.config.js +20 -0
  161. package/kits/hub-game/fonts.generated.js +9 -0
  162. package/kits/hub-game/index.html +22 -0
  163. package/kits/hub-game/main.jsx +6 -0
  164. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  165. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  166. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  167. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  168. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  169. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  170. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  171. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  172. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  174. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  175. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  176. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  177. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  178. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  179. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  180. package/kits/hub-game/package-lock.json +365 -0
  181. package/kits/hub-game/package.json +32 -0
  182. package/kits/hub-game/scenes/main.scene +318 -0
  183. package/kits/hub-game/scripts/check.mjs +339 -0
  184. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  185. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  186. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  187. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  188. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  189. package/kits/hub-game/scripts/pack.mjs +192 -0
  190. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  191. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  192. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  193. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  194. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  195. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  196. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  197. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  198. package/kits/hub-game/scripts/test-door.mjs +56 -0
  199. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  200. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  201. package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
  202. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  203. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  204. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  205. package/kits/hub-game/scripts/test-manifest.mjs +224 -0
  206. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  207. package/kits/hub-game/scripts/test-npcs.mjs +714 -0
  208. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  209. package/kits/hub-game/scripts/test-party.mjs +286 -0
  210. package/kits/hub-game/scripts/test-say.mjs +149 -0
  211. package/kits/hub-game/scripts/test-together.mjs +183 -0
  212. package/kits/hub-game/theme.style +3 -0
  213. package/kits/multiplayer-3d/castle.json +5 -5
  214. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  215. package/kits/multiplayer-3d/code/server/players.js +4 -1
  216. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  217. package/kits/multiplayer-3d/code/server/world.js +20 -7
  218. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  219. package/kits/physics-2d/castle.json +2 -2
  220. package/kits/physics-2d/engine/art.js +13 -0
  221. package/kits/physics-2d/engine/assets.js +4 -2
  222. package/kits/physics-2d/engine/scene.js +25 -5
  223. package/kits/physics-3d/CLAUDE.md +6 -1
  224. package/kits/physics-3d/castle.json +3 -3
  225. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
  226. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  227. package/kits/physics-3d/engine3d/materials.js +29 -1
  228. package/kits/physics-3d/engine3d/world3d.js +65 -1
  229. package/kits/physics-3d/systems/physics3d.js +5 -2
  230. package/kits/real-time/castle.json +2 -2
  231. package/kits/real-time/code/client/connection.js +77 -7
  232. package/kits/real-time/code/client/gameHooks.js +63 -3
  233. package/kits/real-time/code/client/messages.js +7 -0
  234. package/kits/real-time/code/server/gameHooks.js +24 -0
  235. package/kits/real-time/code/server/session.js +390 -22
  236. package/package.json +2 -2
@@ -0,0 +1,103 @@
1
+ // Tests over a town's labels by attention (code/client/attention.js): one
2
+ // plate and one NPC line, the nearest, fading in and out without flicker, and
3
+ // other players' names near the player only. `npm run check` runs this.
4
+
5
+ import assert from 'node:assert/strict';
6
+ import { attend, fadeNames, makeAttention, nameOpacity, NAME_SCALE } from '../code/client/attention.js';
7
+
8
+ // Fifteen statues round a fountain, the player at the arrival spot 8 m out: none.
9
+ const ring = Array.from({ length: 15 }, (_, i) => ({ id: `plinth-${i}`, d: 8 + i * 0.3, speech: false }));
10
+ let a = makeAttention();
11
+ assert.equal(attend(a, [...ring, { id: 'meter', d: 6, speech: false }], 0).size, 0, 'nothing from the arrival spot');
12
+
13
+ // Walk to one statue: only it, fading in over a quarter second.
14
+ const near = (d0, d1) => [
15
+ { id: 'plinth-0', d: d0, speech: false },
16
+ { id: 'plinth-1', d: d1, speech: false },
17
+ { id: 'meter', d: 6, speech: false },
18
+ ];
19
+ a = makeAttention();
20
+ let shown = attend(a, near(2, 3), 1000);
21
+ assert.deepEqual([...shown.keys()], ['plinth-0'], 'the nearest plate only');
22
+ assert.equal(shown.get('plinth-0'), 1, 'a first frame shows it whole');
23
+ shown = attend(a, near(2, 3), 1016);
24
+ assert.equal(shown.get('plinth-0'), 1);
25
+
26
+ // Between two statues the shown one holds until another is clearly nearer.
27
+ shown = attend(a, near(2.5, 2.2), 1032);
28
+ assert.equal(shown.get('plinth-0'), 1, 'a near tie keeps the plate shown');
29
+ assert.equal(shown.has('plinth-1'), false);
30
+ shown = attend(a, near(3.5, 1.5), 1048);
31
+ assert.ok(shown.get('plinth-0') < 1, 'the old plate fades out');
32
+ assert.ok(shown.get('plinth-1') > 0 && shown.get('plinth-1') < 1, 'the new one fades in');
33
+ for (let t = 1064; t < 1500; t += 16) {
34
+ shown = attend(a, near(3.5, 1.5), t);
35
+ }
36
+ assert.deepEqual([...shown.entries()], [['plinth-1', 1]], 'then only the new one');
37
+
38
+ // Walk away: it fades and is forgotten.
39
+ for (let t = 1500; t < 2000; t += 16) {
40
+ shown = attend(a, near(9, 9), t);
41
+ }
42
+ assert.equal(shown.size, 0);
43
+ assert.equal(a.alpha.size, 0);
44
+
45
+ // One NPC line, the nearest within reach, beside the one plate.
46
+ a = makeAttention();
47
+ shown = attend(
48
+ a,
49
+ [
50
+ { id: 'barker-a', d: 5, speech: true },
51
+ { id: 'barker-b', d: 7, speech: true },
52
+ { id: 'barker-c', d: 12, speech: true },
53
+ { id: 'board', d: 3, speech: false },
54
+ ],
55
+ 0,
56
+ );
57
+ assert.deepEqual([...shown.keys()].sort(), ['barker-a', 'board']);
58
+
59
+ // A label the place cleared is gone at once.
60
+ shown = attend(a, [{ id: 'barker-a', d: 5, speech: true }], 16);
61
+ assert.deepEqual([...shown.keys()], ['barker-a']);
62
+ assert.equal(a.alpha.has('board'), false);
63
+
64
+ // Names: whole within 12 m, gone by 14.
65
+ assert.equal(nameOpacity(0), 1);
66
+ assert.equal(nameOpacity(12), 1);
67
+ assert.equal(nameOpacity(13), 0.5);
68
+ assert.equal(nameOpacity(14), 0);
69
+ assert.equal(nameOpacity(30), 0);
70
+
71
+ const sprite = (x, z) => ({ position: { x, y: 1, z }, scale: { x: 1, y: 0.2 }, visible: true, material: { opacity: 1 } });
72
+ const tags = new Map([
73
+ ['self', { sprite: sprite(0, 0) }],
74
+ ['p-near', { sprite: sprite(3, 4) }],
75
+ ['p-far', { sprite: sprite(30, 0) }],
76
+ ]);
77
+ const mp = { avatars: { tags } };
78
+ const touched = new WeakMap();
79
+ fadeNames(mp, { x: 0, z: 0 }, true, touched);
80
+ assert.equal(tags.get('self').sprite.visible, true);
81
+ assert.equal(tags.get('p-near').sprite.visible, true);
82
+ assert.equal(tags.get('p-far').sprite.visible, false);
83
+ assert.equal(tags.get('p-near').sprite.scale.x, NAME_SCALE);
84
+ // A frame the multiplayer kit did not resize a tag is not shrunk twice.
85
+ fadeNames(mp, { x: 0, z: 0 }, true, touched);
86
+ assert.equal(tags.get('p-near').sprite.scale.x, NAME_SCALE);
87
+ // It resizes it next frame: shrunk once again from there.
88
+ tags.get('p-near').sprite.scale.x = 0.5;
89
+ fadeNames(mp, { x: 0, z: 0 }, true, touched);
90
+ assert.equal(tags.get('p-near').sprite.scale.x, 0.5 * NAME_SCALE);
91
+ // Out of the town: every tag is put back.
92
+ fadeNames(mp, { x: 0, z: 0 }, false, touched);
93
+ assert.equal(tags.get('p-far').sprite.visible, true);
94
+ assert.equal(tags.get('p-far').sprite.material.opacity, 1);
95
+ assert.equal(tags.get('p-near').sprite.scale.x, 0.5, 'a shrunk tag the multiplayer kit did not resize is put back');
96
+ assert.equal(tags.get('self').sprite.scale.y, 0.2);
97
+ // Town, out, town again with no resize between: shrunk once, not twice.
98
+ fadeNames(mp, { x: 0, z: 0 }, true, touched);
99
+ fadeNames(mp, { x: 0, z: 0 }, false, touched);
100
+ fadeNames(mp, { x: 0, z: 0 }, true, touched);
101
+ assert.equal(tags.get('p-near').sprite.scale.x, 0.5 * NAME_SCALE);
102
+
103
+ console.log('attention: all passed');
@@ -0,0 +1,472 @@
1
+ // Tests over the platform avatar seam (contract §11, code/client/avatar.js):
2
+ // the account's avatar from the host when there is one, the stand-in pick when
3
+ // there is not, picks written one at a time and undone when ghost refuses
4
+ // them. castle-web-sdk is stubbed (scripts/fixtures/sdk-stub.mjs); the host is
5
+ // faked. `npm run check` runs this.
6
+
7
+ import assert from 'node:assert/strict';
8
+ import { readFile } from 'node:fs/promises';
9
+ import { register } from 'node:module';
10
+
11
+ register(
12
+ 'data:text/javascript,' +
13
+ encodeURIComponent(`
14
+ export async function resolve(specifier, context, next) {
15
+ if (specifier === 'castle-web-sdk') {
16
+ return { url: ${JSON.stringify(new URL('./fixtures/sdk-stub.mjs', import.meta.url).href)}, shortCircuit: true };
17
+ }
18
+ return next(specifier, context);
19
+ }`),
20
+ );
21
+
22
+ const Avatar = await import('../code/client/avatar.js');
23
+
24
+ // A host whose answers the test releases by hand, in any order.
25
+ function host() {
26
+ const calls = [];
27
+ const pending = [];
28
+ const call = (name, arg) =>
29
+ new Promise((resolve, reject) => {
30
+ calls.push(arg === undefined ? [name] : [name, arg]);
31
+ pending.push({ name, arg, resolve, reject });
32
+ });
33
+ const settle = async (pick, how) => {
34
+ const i = pick ? pending.findIndex(pick) : 0;
35
+ assert.ok(i >= 0, 'no such call in flight');
36
+ const [p] = pending.splice(i, 1);
37
+ how(p);
38
+ await new Promise((r) => setTimeout(r, 0));
39
+ };
40
+ return {
41
+ api: { get: () => call('get'), set: (avatar) => call('set', avatar) },
42
+ calls,
43
+ inFlight: () => pending.map((p) => (p.arg === undefined ? [p.name] : [p.name, p.arg])),
44
+ reply: (value, pick) => settle(pick, (p) => p.resolve(value)),
45
+ fail: (error, pick) => settle(pick, (p) => p.reject(error)),
46
+ };
47
+ }
48
+
49
+ const ok = (avatar, chosen = true) => ({ status: 'ok', avatar, chosen });
50
+ const unavailable = { status: 'unavailable', avatar: null, chosen: false };
51
+ const setOf = (avatar) => (p) => p.name === 'set' && p.arg === avatar;
52
+
53
+ let n = 0;
54
+ async function test(name, fn) {
55
+ await fn();
56
+ n += 1;
57
+ }
58
+
59
+ function ready(h, avatar, scene) {
60
+ Avatar.resetPlatformForTests(h.api);
61
+ Avatar.load(scene);
62
+ return h.reply(ok(avatar, false));
63
+ }
64
+
65
+ await test("wears the account's avatar once the host answers, and reads it once", async () => {
66
+ const h = host();
67
+ const scene = { persistent: { hubAvatar: 0 } };
68
+ Avatar.resetPlatformForTests(h.api);
69
+ Avatar.load(scene);
70
+ Avatar.load(scene);
71
+ assert.equal(Avatar.current(scene), 0);
72
+ await h.reply(ok(2, false));
73
+ assert.equal(Avatar.current(scene), 2);
74
+ assert.deepEqual(h.calls, [['get']]);
75
+ });
76
+
77
+ await test('without a platform avatar the stand-in pick is worn and chosen', async () => {
78
+ for (const settle of [(h) => h.reply(unavailable), (h) => h.fail({ code: 'UNKNOWN_COMMAND' }), (h) => h.reply(ok(Avatar.AVATARS))]) {
79
+ const h = host();
80
+ const scene = { persistent: { hubAvatar: 1 } };
81
+ Avatar.resetPlatformForTests(h.api);
82
+ Avatar.load(scene);
83
+ await settle(h);
84
+ assert.equal(Avatar.current(scene), 1);
85
+ Avatar.choose(scene, 3);
86
+ assert.equal(Avatar.current(scene), 3);
87
+ assert.equal(scene.persistent.hubAvatar, 3);
88
+ assert.deepEqual(h.calls, [['get']]);
89
+ }
90
+ // An SDK from before Avatar.
91
+ const scene = { persistent: { hubAvatar: 2 } };
92
+ Avatar.resetPlatformForTests(null);
93
+ Avatar.load(scene);
94
+ Avatar.choose(scene, 0);
95
+ assert.equal(Avatar.current(scene), 0);
96
+ });
97
+
98
+ await test("a pick is the account's: worn at once, kept as ghost answers", async () => {
99
+ const h = host();
100
+ const scene = { persistent: {} };
101
+ await ready(h, 0, scene);
102
+ Avatar.choose(scene, 3);
103
+ assert.equal(Avatar.current(scene), 3);
104
+ assert.deepEqual(h.inFlight(), [['set', 3]]);
105
+ await h.reply(ok(3));
106
+ assert.equal(Avatar.current(scene), 3);
107
+ assert.equal(scene.persistent.hubAvatar, undefined);
108
+ });
109
+
110
+ await test('picks are written one at a time, so answers cannot cross', async () => {
111
+ const h = host();
112
+ const scene = { persistent: {} };
113
+ await ready(h, 1, scene);
114
+ Avatar.choose(scene, 2);
115
+ Avatar.choose(scene, 3);
116
+ // Only the first is in flight; the latest waits for it.
117
+ assert.deepEqual(h.inFlight(), [['set', 2]]);
118
+ assert.equal(Avatar.current(scene), 3);
119
+ await h.reply(ok(2));
120
+ assert.deepEqual(h.inFlight(), [['set', 3]]);
121
+ assert.equal(Avatar.current(scene), 3);
122
+ // The latest refused: back to what ghost last kept, which is 2.
123
+ await h.fail({ code: 'RATE_LIMIT_EXCEEDED' });
124
+ assert.equal(Avatar.current(scene), 2);
125
+ assert.deepEqual(h.inFlight(), []);
126
+ });
127
+
128
+ await test('a failed write is undone; a newer pick still goes out after it', async () => {
129
+ const h = host();
130
+ const scene = { persistent: {} };
131
+ await ready(h, 1, scene);
132
+ Avatar.choose(scene, 2);
133
+ Avatar.choose(scene, 3);
134
+ await h.fail({ code: 'INTERNAL' }, setOf(2));
135
+ assert.equal(Avatar.current(scene), 3);
136
+ assert.deepEqual(h.inFlight(), [['set', 3]]);
137
+ await h.reply(ok(3));
138
+ assert.equal(Avatar.current(scene), 3);
139
+ // Picking the same index again after a failure is a fresh write.
140
+ Avatar.choose(scene, 0);
141
+ await h.fail({ code: 'INTERNAL' });
142
+ assert.equal(Avatar.current(scene), 3);
143
+ Avatar.choose(scene, 0);
144
+ assert.deepEqual(h.inFlight(), [['set', 0]]);
145
+ await h.reply(ok(0));
146
+ assert.equal(Avatar.current(scene), 0);
147
+ });
148
+
149
+ await test('a pick made while the avatar loads is kept and written once it is known', async () => {
150
+ const h = host();
151
+ const scene = { persistent: {} };
152
+ Avatar.resetPlatformForTests(h.api);
153
+ Avatar.load(scene);
154
+ Avatar.choose(scene, 2);
155
+ assert.equal(Avatar.current(scene), 2);
156
+ assert.deepEqual(h.inFlight(), [['get']]);
157
+ await h.reply(ok(0, false));
158
+ assert.equal(Avatar.current(scene), 2);
159
+ assert.deepEqual(h.inFlight(), [['set', 2]]);
160
+ await h.reply(ok(2));
161
+ assert.equal(Avatar.current(scene), 2);
162
+
163
+ // Loading ends with no platform avatar: the pick becomes the stand-in.
164
+ const g = host();
165
+ const other = { persistent: {} };
166
+ Avatar.resetPlatformForTests(g.api);
167
+ Avatar.load(other);
168
+ Avatar.choose(other, 1);
169
+ await g.reply(unavailable);
170
+ assert.equal(Avatar.current(other), 1);
171
+ assert.equal(other.persistent.hubAvatar, 1);
172
+ });
173
+
174
+ await test('a place that may not change the avatar keeps the pick as its own', async () => {
175
+ const h = host();
176
+ const scene = { persistent: {} };
177
+ await ready(h, 0, scene);
178
+ Avatar.choose(scene, 2);
179
+ await h.reply(unavailable);
180
+ assert.equal(Avatar.current(scene), 2);
181
+ assert.equal(scene.persistent.hubAvatar, 2);
182
+ Avatar.choose(scene, 3);
183
+ assert.equal(Avatar.current(scene), 3);
184
+ assert.deepEqual(h.inFlight(), []);
185
+ });
186
+
187
+ await test('an index outside the templates is ignored', async () => {
188
+ const h = host();
189
+ const scene = { persistent: { hubAvatar: 1 } };
190
+ await ready(h, 0, scene);
191
+ for (const bad of [-1, Avatar.AVATARS, 1.5, '2']) {
192
+ Avatar.choose(scene, bad);
193
+ }
194
+ assert.equal(Avatar.current(scene), 0);
195
+ assert.deepEqual(h.calls, [['get']]);
196
+ });
197
+
198
+ await test("an avatar's model is its blueprint's own deck's file, wherever the kit is", async () => {
199
+ const blueprintOf = (file) => JSON.stringify({ actors: [{ components: { Model: { file } } }] });
200
+ // The kit served as itself, then imported by a deck.
201
+ assert.equal(Avatar.model({ 'blueprints/avatar-1.scene': blueprintOf('models/avatar-1.pxmodel') }, 1), 'models/avatar-1.pxmodel');
202
+ const imported = { 'imports/castle.hub-game/blueprints/avatar-1.scene': blueprintOf('models/avatar-1.pxmodel') };
203
+ assert.equal(Avatar.model(imported, 1), 'imports/castle.hub-game/models/avatar-1.pxmodel');
204
+ // A file of another import is named whole.
205
+ for (const file of ['@imports/castle.physics-3d/models/player.pxmodel', 'imports/castle.physics-3d/models/player.pxmodel']) {
206
+ const files = { 'imports/castle.hub-game/blueprints/avatar-0.scene': blueprintOf(file) };
207
+ assert.equal(Avatar.model(files, 0), 'imports/castle.physics-3d/models/player.pxmodel');
208
+ }
209
+ assert.equal(Avatar.model({ 'blueprints/avatar-2.scene': '{}' }, 2), null);
210
+ assert.equal(Avatar.model({}, 2), null);
211
+ });
212
+
213
+ await test('every avatar has its blueprint, its model and a height', async () => {
214
+ const kit = new URL('../', import.meta.url);
215
+ for (let i = 0; i < Avatar.AVATARS; i++) {
216
+ const path = `blueprints/avatar-${i}.scene`;
217
+ const files = { [path]: await readFile(new URL(path, kit), 'utf8') };
218
+ const model = Avatar.model(files, i);
219
+ assert.equal(model, `models/avatar-${i}.pxmodel`);
220
+ const parts = JSON.parse(await readFile(new URL(model, kit), 'utf8')).parts.map((part) => part.id);
221
+ // The parts the walk moves.
222
+ for (const id of ['torso', 'head', 'legL', 'legR', 'armL', 'armR']) {
223
+ assert.ok(parts.includes(id), `${model} has no ${id}`);
224
+ }
225
+ assert.ok(Avatar.top(i) >= 1.2 && Avatar.top(i) <= 1.75, `avatar ${i} top ${Avatar.top(i)}`);
226
+ }
227
+ assert.equal(Avatar.top(Avatar.AVATARS), Avatar.top(undefined));
228
+ });
229
+
230
+ // --- avatar sets (the style key) ---
231
+
232
+ const at = (href) => {
233
+ const url = new URL(href);
234
+ return { hostname: url.hostname, search: url.search };
235
+ };
236
+ const blueprintOf = (file) => JSON.stringify({ actors: [{ components: { Model: { file } } }] });
237
+ // A deck's files with the kit imported: toon and one more set.
238
+ const deckFiles = (...styles) => {
239
+ const files = {};
240
+ for (const style of ['toon', ...styles]) {
241
+ const dir = style === 'toon' ? '' : `${style}/`;
242
+ for (let i = 0; i < Avatar.AVATARS; i++) {
243
+ files[`imports/castle.hub-game/blueprints/${dir}avatar-${i}.scene`] = blueprintOf(`models/${dir}avatar-${i}.pxmodel`);
244
+ }
245
+ }
246
+ return files;
247
+ };
248
+
249
+ await test('with nothing said the set is toon, and everything is where it was', () => {
250
+ assert.deepEqual(Avatar.STYLES, ['toon', 'blocky', 'bean', 'pixel']);
251
+ // The real page and SDK of this test: no location, an SDK without `experiment`.
252
+ Avatar.resetStyleForTests(undefined);
253
+ assert.equal(Avatar.style(), 'toon');
254
+ const files = deckFiles('blocky', 'bean', 'pixel');
255
+ const toonTops = [1.38, 1.5, 1.42, 1.56];
256
+ for (let i = 0; i < Avatar.AVATARS; i++) {
257
+ assert.equal(Avatar.blueprint(files, i), `imports/castle.hub-game/blueprints/avatar-${i}.scene`);
258
+ assert.equal(Avatar.model(files, i), `imports/castle.hub-game/models/avatar-${i}.pxmodel`);
259
+ assert.equal(Avatar.top(i), toonTops[i]);
260
+ assert.equal(Avatar.drawn(files, i), 'toon');
261
+ }
262
+ assert.equal(Avatar.top(9), 1.65);
263
+ assert.equal(Avatar.blueprint(null, 0), null);
264
+ });
265
+
266
+ await test('the experiment names the set; anything it does not know is toon', () => {
267
+ const asked = [];
268
+ for (const [answer, want] of [
269
+ ['blocky', 'blocky'],
270
+ ['bean', 'bean'],
271
+ ['pixel', 'pixel'],
272
+ ['toon', 'toon'],
273
+ [null, 'toon'],
274
+ [undefined, 'toon'],
275
+ ['voxel', 'toon'],
276
+ ['Pixel', 'toon'],
277
+ ['', 'toon'],
278
+ [2, 'toon'],
279
+ [{ variant: 'bean' }, 'toon'],
280
+ ['constructor', 'toon'],
281
+ ]) {
282
+ Avatar.resetStyleForTests({
283
+ location: at('https://castle.xyz/d/abc'),
284
+ experiment: (key) => {
285
+ asked.push(key);
286
+ return answer;
287
+ },
288
+ });
289
+ assert.equal(Avatar.style(), want, JSON.stringify(answer));
290
+ }
291
+ assert.ok(asked.length > 0 && asked.every((key) => key === 'hub_avatar_style'));
292
+ });
293
+
294
+ await test('an SDK that throws, or has no experiment, leaves toon', () => {
295
+ Avatar.resetStyleForTests({
296
+ location: at('https://castle.xyz/d/abc'),
297
+ experiment: () => {
298
+ throw new Error('no bridge');
299
+ },
300
+ });
301
+ assert.equal(Avatar.style(), 'toon');
302
+ for (const experiment of [undefined, null, 'bean', {}]) {
303
+ Avatar.resetStyleForTests({ location: undefined, experiment });
304
+ assert.equal(Avatar.style(), 'toon');
305
+ }
306
+ });
307
+
308
+ await test('the SDK hears once, before the first read, that experiments are read here', () => {
309
+ const calls = [];
310
+ let answer = null;
311
+ const env = {
312
+ location: undefined,
313
+ experimentsReady: () => {
314
+ calls.push('ready');
315
+ return new Promise(() => {});
316
+ },
317
+ experiment: () => {
318
+ calls.push('read');
319
+ return answer;
320
+ },
321
+ };
322
+ Avatar.resetStyleForTests(env);
323
+ // Nothing waits on it: the read is null, toon is drawn, and a later read has the answer.
324
+ assert.equal(Avatar.style(), 'toon');
325
+ assert.equal(Avatar.style(), 'toon');
326
+ answer = 'blocky';
327
+ assert.equal(Avatar.style(), 'blocky');
328
+ assert.deepEqual(calls, ['ready', 'read', 'read', 'read']);
329
+ // One that throws or rejects changes nothing; an SDK without it is read as before.
330
+ for (const experimentsReady of [
331
+ () => {
332
+ throw new Error('no bridge');
333
+ },
334
+ () => Promise.reject(new Error('refused')),
335
+ undefined,
336
+ 'yes',
337
+ ]) {
338
+ Avatar.resetStyleForTests({ location: undefined, experimentsReady, experiment: () => 'bean' });
339
+ assert.equal(Avatar.style(), 'bean');
340
+ }
341
+ });
342
+
343
+ await test('?hubStyle= overrides the experiment on localhost and 127.0.0.1 only', () => {
344
+ const experiment = () => 'bean';
345
+ for (const [href, want] of [
346
+ ['http://localhost:5173/index.html?edit=0&hubStyle=pixel', 'pixel'],
347
+ ['http://127.0.0.1:5173/?hubStyle=blocky', 'blocky'],
348
+ ['http://localhost:5173/?hubStyle=toon', 'toon'],
349
+ // Not a set: the experiment still answers.
350
+ ['http://localhost:5173/?hubStyle=voxel', 'bean'],
351
+ ['http://localhost:5173/?edit=0', 'bean'],
352
+ // Anywhere else the parameter is not read.
353
+ ['https://castle.xyz/d/abc?hubStyle=pixel', 'bean'],
354
+ ['https://localhost.evil.example/?hubStyle=pixel', 'bean'],
355
+ ['http://192.168.1.4:5173/?hubStyle=pixel', 'bean'],
356
+ ]) {
357
+ Avatar.resetStyleForTests({ location: at(href), experiment });
358
+ assert.equal(Avatar.style(), want, href);
359
+ }
360
+ // A location that cannot be read is no override.
361
+ Avatar.resetStyleForTests({
362
+ location: {
363
+ get hostname() {
364
+ throw new Error('cross-origin');
365
+ },
366
+ },
367
+ experiment,
368
+ });
369
+ assert.equal(Avatar.style(), 'bean');
370
+ });
371
+
372
+ await test("the viewer's set draws every avatar; the answer may arrive late", () => {
373
+ let answer = null;
374
+ Avatar.resetStyleForTests({ location: undefined, experiment: () => answer });
375
+ const files = deckFiles('blocky', 'bean', 'pixel');
376
+ assert.equal(Avatar.blueprint(files, 2), 'imports/castle.hub-game/blueprints/avatar-2.scene');
377
+ answer = 'pixel';
378
+ assert.equal(Avatar.blueprint(files, 2), 'imports/castle.hub-game/blueprints/pixel/avatar-2.scene');
379
+ assert.equal(Avatar.model(files, 2), 'imports/castle.hub-game/models/pixel/avatar-2.pxmodel');
380
+ assert.equal(Avatar.drawn(files, 2), 'pixel');
381
+ assert.equal(Avatar.top(2), Avatar.top(2, 'pixel'));
382
+ // Asked for by name, whatever the viewer's is.
383
+ assert.equal(Avatar.blueprint(files, 1, 'bean'), 'imports/castle.hub-game/blueprints/bean/avatar-1.scene');
384
+ assert.equal(Avatar.blueprint(files, 1, 'toon'), 'imports/castle.hub-game/blueprints/avatar-1.scene');
385
+ // The kit served as itself keeps its sets at the root.
386
+ assert.equal(Avatar.model({ 'blueprints/bean/avatar-0.scene': blueprintOf('models/bean/avatar-0.pxmodel') }, 0, 'bean'), 'models/bean/avatar-0.pxmodel');
387
+ });
388
+
389
+ await test("a set these files do not have is drawn from toon's, at toon's height", () => {
390
+ Avatar.resetStyleForTests({ location: undefined, experiment: () => 'bean' });
391
+ const files = deckFiles('pixel');
392
+ for (let i = 0; i < Avatar.AVATARS; i++) {
393
+ assert.equal(Avatar.blueprint(files, i), `imports/castle.hub-game/blueprints/avatar-${i}.scene`);
394
+ assert.equal(Avatar.model(files, i), `imports/castle.hub-game/models/avatar-${i}.pxmodel`);
395
+ assert.equal(Avatar.drawn(files, i), 'toon');
396
+ assert.equal(Avatar.top(i, Avatar.drawn(files, i)), Avatar.top(i, 'toon'));
397
+ }
398
+ // A name that is not a set is toon wherever it comes from.
399
+ assert.equal(Avatar.blueprint(files, 0, '../models'), 'imports/castle.hub-game/blueprints/avatar-0.scene');
400
+ assert.equal(Avatar.drawn(files, 0, 'voxel'), 'toon');
401
+ assert.equal(Avatar.top(0, 'voxel'), Avatar.top(0, 'toon'));
402
+ assert.equal(Avatar.top(0, 'constructor'), Avatar.top(0, 'toon'));
403
+ // Before the files are known the set asked for stands.
404
+ assert.equal(Avatar.drawn(null, 0), 'bean');
405
+ });
406
+
407
+ await test("every set has four blueprints shaped like toon's, models with the parts the walk moves, art it can find and a height", async () => {
408
+ const kit = new URL('../', import.meta.url);
409
+ const refs = (value, out = []) => {
410
+ if (typeof value === 'string') {
411
+ out.push(value);
412
+ } else if (value && typeof value === 'object') {
413
+ Object.values(value).forEach((v) => refs(v, out));
414
+ }
415
+ return out;
416
+ };
417
+ for (const style of Avatar.STYLES) {
418
+ const dir = style === 'toon' ? '' : `${style}/`;
419
+ for (let i = 0; i < Avatar.AVATARS; i++) {
420
+ const path = `blueprints/${dir}avatar-${i}.scene`;
421
+ const text = await readFile(new URL(path, kit), 'utf8');
422
+ const files = { [path]: text };
423
+ assert.equal(Avatar.blueprint(files, i, style), path);
424
+ const model = Avatar.model(files, i, style);
425
+ assert.equal(model, `models/${dir}avatar-${i}.pxmodel`);
426
+ // The body is toon's: only the model differs.
427
+ const body = (source) => {
428
+ const { Model, ...rest } = JSON.parse(source).actors[0].components;
429
+ return { rest, yOffset: Model.yOffset };
430
+ };
431
+ assert.deepEqual(body(text), body(await readFile(new URL(`blueprints/avatar-${i}.scene`, kit), 'utf8')), path);
432
+ const document = JSON.parse(await readFile(new URL(model, kit), 'utf8'));
433
+ const parts = document.parts.map((part) => part.id);
434
+ for (const id of ['torso', 'head', 'legL', 'legR', 'armL', 'armR']) {
435
+ assert.ok(parts.includes(id), `${model} has no ${id}`);
436
+ }
437
+ // The sets do not draw an outline pass: it doubles triangles and draw calls.
438
+ if (style !== 'toon') {
439
+ assert.equal(document.look?.outline ?? 0, 0, `${model} outline`);
440
+ }
441
+ // Art is the kit's own, in the set's directory, and is there. The engine
442
+ // looks an art ref up as written, in the deck's files, so a model of
443
+ // this kit names its art through the import.
444
+ const art = `imports/castle.hub-game/drawings/${dir}`;
445
+ for (const ref of refs(document.parts).filter((r) => /\.(sprite|pxart|png|jpg|webp)(#\d+)?$/.test(r))) {
446
+ assert.ok(ref.startsWith(art), `${model}: ${ref}`);
447
+ await readFile(new URL(ref.slice('imports/castle.hub-game/'.length).replace(/#\d+$/, ''), kit));
448
+ }
449
+ const top = Avatar.top(i, style);
450
+ assert.ok(top >= 1.2 && top <= 1.75, `${style} avatar ${i} top ${top}`);
451
+ }
452
+ }
453
+ });
454
+
455
+ Avatar.resetStyleForTests(undefined);
456
+
457
+ await test('the pack exporter and the town overlay know every set, and only the sets', async () => {
458
+ const kit = new URL('../', import.meta.url);
459
+ for (const file of ['scripts/pack.mjs', 'code/client/pack.js']) {
460
+ const source = await readFile(new URL(file, kit), 'utf8');
461
+ const avatar = new RegExp(/const AVATAR(?:_BLUEPRINT)? = \/(.+)\/;/.exec(source)[1]);
462
+ for (const style of Avatar.STYLES) {
463
+ const dir = style === 'toon' ? '' : `${style}/`;
464
+ assert.ok(avatar.test(`blueprints/${dir}avatar-2.scene`), `${file} ${style}`);
465
+ assert.ok(avatar.test(`imports/castle.hub-game/blueprints/${dir}avatar-2.scene`), `${file} ${style}`);
466
+ }
467
+ // A game's own folder of blueprints is the game's.
468
+ assert.ok(!avatar.test('blueprints/npcs/avatar-2.scene'), file);
469
+ }
470
+ });
471
+
472
+ console.log(`hub-game avatar: ${n} tests passed`);