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,424 @@
1
+ // Tests for the content pack (docs/content-pack.md): the exporter's closure
2
+ // against a throwaway deck, the validator the town's engine runs, and the
3
+ // hash check, the place's sounds (`audio`) and the content the town's engine
4
+ // mounts from it. Run by the kit's `npm run check`.
5
+
6
+ import assert from 'node:assert/strict';
7
+ import crypto from 'node:crypto';
8
+ import fs from 'node:fs';
9
+ import { register } from 'node:module';
10
+ import os from 'node:os';
11
+ import path from 'node:path';
12
+ import pack from './pack.mjs';
13
+ import { PACK_LIMITS, validatePack } from '../code/client/packFormat.js';
14
+ import { sha256Hex } from '../code/client/sha256.js';
15
+
16
+ // The engine modules the mount path (code/client/pack.js, sound.js) imports.
17
+ const engineStub = [
18
+ 'export const initialMedia = globalThis.__packTestMedia ?? {};',
19
+ 'export const collectAssets = () => ({ sprites: {} });',
20
+ 'export const playMedia = () => {}; export const stopMedia = () => {};',
21
+ ].join('\n');
22
+ register(
23
+ 'data:text/javascript,' +
24
+ encodeURIComponent(`
25
+ export async function resolve(specifier, context, next) {
26
+ if (specifier.startsWith('@imports/castle.physics-2d/') || specifier.startsWith('@imports/castle.physics-3d/')) {
27
+ return { url: ${JSON.stringify('data:text/javascript,' + encodeURIComponent(engineStub))}, shortCircuit: true };
28
+ }
29
+ return next(specifier, context);
30
+ }`),
31
+ );
32
+
33
+ let passed = 0;
34
+ async function test(name, fn) {
35
+ await fn();
36
+ passed += 1;
37
+ console.log(`ok: ${name}`);
38
+ }
39
+
40
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'hub-pack-test-'));
41
+ const write = (rel, value) => {
42
+ const file = path.join(root, rel);
43
+ fs.mkdirSync(path.dirname(file), { recursive: true });
44
+ fs.writeFileSync(file, typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value));
45
+ };
46
+
47
+ write('castle.json', {
48
+ server: { main: 'imports/castle.hub-game/code/server/index.js', maxPlayers: 8 },
49
+ imports: { 'castle.hub-game': { source: 'builtin', kit: 'hub-game', version: '9.9.9' } },
50
+ hub: { contract: 1, title: 'Test', controller: 'walk', result: { outcomes: ['win', 'lose'] } },
51
+ });
52
+ write('scenes/main.scene', {
53
+ actors: [
54
+ { id: 'a', blueprint: 'blueprints/crate.scene', components: { Transform: { x: 0 } } },
55
+ { id: 'b', components: { Model: { file: '@imports/castle.physics-3d/models/lamp.pxmodel' }, HubDoor: { id: 'x' } } },
56
+ { id: 'p', blueprint: 'imports/castle.hub-game/blueprints/avatar-0.scene', components: {} },
57
+ { id: 'q', blueprint: 'imports/castle.hub-game/blueprints/pixel/avatar-1.scene', components: {} },
58
+ { id: 'n', blueprint: 'blueprints/npcs/avatar-7.scene', components: {} },
59
+ ],
60
+ ground: { art: 'drawings/grass.png' },
61
+ });
62
+ write('blueprints/crate.scene', {
63
+ actors: [{ components: { Model: { file: 'models/crate.pxmodel' }, Sprite: { art: 'drawings/door.sprite#1' } } }],
64
+ });
65
+ write('drawings/door.sprite', { frames: [] });
66
+ write('models/crate.pxmodel', { parts: [] });
67
+ // An import's model naming a sibling without the import prefix: resolved inside that import.
68
+ write('imports/castle.physics-3d/models/lamp.pxmodel', { parts: [{ kind: 'model', src: 'models/bulb.pxmodel' }] });
69
+ write('imports/castle.physics-3d/models/bulb.pxmodel', { parts: [] });
70
+ write('imports/castle.physics-3d/models/unused.pxmodel', { parts: [] });
71
+ write('imports/castle.hub-game/blueprints/avatar-0.scene', { actors: [{ components: { Model: { file: 'models/avatar-0.pxmodel' } } }] });
72
+ write('imports/castle.hub-game/models/avatar-0.pxmodel', { parts: [] });
73
+ // Another avatar set (`Avatar.STYLES`), with art of its own.
74
+ // A game's own blueprint that only looks like an avatar's is the game's.
75
+ write('blueprints/npcs/avatar-7.scene', { actors: [{ components: { Model: { file: 'models/guard.pxmodel' } } }] });
76
+ write('models/guard.pxmodel', { parts: [] });
77
+ write('imports/castle.hub-game/blueprints/pixel/avatar-1.scene', { actors: [{ components: { Model: { file: 'models/pixel/avatar-1.pxmodel' } } }] });
78
+ write('imports/castle.hub-game/models/pixel/avatar-1.pxmodel', { parts: [{ art: { front: 'imports/castle.hub-game/drawings/pixel/face.sprite' } }] });
79
+ write('imports/castle.hub-game/drawings/pixel/face.sprite', { frames: [] });
80
+ write('drawings/grass.png', Buffer.from([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]));
81
+ write('drawings/unused.png', Buffer.from([1]));
82
+ write('code/server/game.js', 'export default {};');
83
+ write('behaviors/Evil.jsx', 'export class Evil {}');
84
+
85
+ const outFile = path.join(root, 'out.json');
86
+ const result = await pack({ deckDir: root, outFile });
87
+ const doc = JSON.parse(fs.readFileSync(outFile, 'utf8'));
88
+
89
+ await test('the exporter succeeds and writes a valid pack', () => {
90
+ assert.deepEqual(result.errors, []);
91
+ assert.deepEqual(validatePack(doc).errors, []);
92
+ assert.equal(doc.kit.version, '9.9.9');
93
+ assert.equal(doc.places.main.title, 'Test');
94
+ });
95
+
96
+ await test('the closure follows scenes, blueprints, models, framed art refs and model refs inside an import', () => {
97
+ assert.deepEqual(Object.keys(doc.files).sort(), [
98
+ 'blueprints/crate.scene',
99
+ 'blueprints/npcs/avatar-7.scene',
100
+ 'drawings/door.sprite',
101
+ 'imports/castle.physics-3d/models/bulb.pxmodel',
102
+ 'imports/castle.physics-3d/models/lamp.pxmodel',
103
+ 'models/crate.pxmodel',
104
+ 'models/guard.pxmodel',
105
+ 'scenes/main.scene',
106
+ ]);
107
+ assert.deepEqual(Object.keys(doc.media), ['drawings/grass.png']);
108
+ assert.equal(doc.media['drawings/grass.png'].type, 'image/png');
109
+ });
110
+
111
+ await test('code, the avatars and unreferenced files never go in', () => {
112
+ const all = [...Object.keys(doc.files), ...Object.keys(doc.media)].join(' ');
113
+ for (const absent of ['game.js', 'Evil', 'avatar-0', 'avatar-1', 'castle.hub-game', 'face.sprite', 'unused']) {
114
+ assert.ok(!all.includes(absent), absent);
115
+ }
116
+ });
117
+
118
+ await test('every entry carries its own sha256', () => {
119
+ const text = doc.files['scenes/main.scene'].text;
120
+ assert.equal(doc.files['scenes/main.scene'].sha256, crypto.createHash('sha256').update(text).digest('hex'));
121
+ });
122
+
123
+ const valid = () => structuredClone(doc);
124
+ const refuses = (name, mutate, needle) =>
125
+ test(`the validator refuses ${name}`, () => {
126
+ const p = valid();
127
+ mutate(p);
128
+ const { errors } = validatePack(p);
129
+ assert.ok(errors.some((e) => e.includes(needle)), `${needle} not in ${JSON.stringify(errors)}`);
130
+ });
131
+
132
+ await refuses('another format', (p) => (p.format = 'html'), 'format');
133
+ await refuses('a newer pack version', (p) => (p.version = 2), 'pack version');
134
+ await refuses('a contract this engine does not read', (p) => (p.contract = 7), 'hub contract');
135
+ await refuses('code', (p) => (p.files['behaviors/Evil.jsx'] = { sha256: 'a'.repeat(64), text: '' }), 'Evil.jsx');
136
+ await refuses('a path that climbs', (p) => (p.files['../x.scene'] = { sha256: 'a'.repeat(64), text: '{}' }), '../x.scene');
137
+ await refuses('media of the wrong type', (p) => (p.media['drawings/grass.png'].type = 'text/html'), 'media entry');
138
+ await refuses('a place whose scene is missing', (p) => (p.places.main.scene = 'scenes/gone.scene'), 'not in the pack');
139
+ await refuses('a scene that is not JSON', (p) => (p.files['scenes/main.scene'].text = '{'), 'not a scene');
140
+ await refuses(
141
+ 'a component name that is not one',
142
+ (p) => (p.files['scenes/main.scene'].text = JSON.stringify({ actors: [{ id: 'a', components: { 'x y': {} } }] })),
143
+ 'component name',
144
+ );
145
+ await refuses(
146
+ 'a scene over the actor budget',
147
+ (p) =>
148
+ (p.files['scenes/main.scene'].text = JSON.stringify({
149
+ actors: Array.from({ length: PACK_LIMITS.sceneActors + 1 }, (_, i) => ({ id: `a${i}` })),
150
+ })),
151
+ 'actors, over',
152
+ );
153
+ await test('the validator refuses a pack over the byte budget', () => {
154
+ assert.ok(validatePack(valid(), { byteLength: PACK_LIMITS.bytes + 1 }).errors.length > 0);
155
+ });
156
+
157
+ await refuses(
158
+ 'a model that expands past the budget',
159
+ (p) => {
160
+ // Five nested parts, each repeated 64 times: a few hundred bytes, a billion meshes.
161
+ const parts = Array.from({ length: 5 }, (_, i) => ({ id: `p${i}`, parent: i ? `p${i - 1}` : '', kind: 'box', repeat: { count: 64 } }));
162
+ p.files['models/crate.pxmodel'].text = JSON.stringify({ parts });
163
+ },
164
+ 'expands to over',
165
+ );
166
+ await refuses(
167
+ 'a model that expands past the budget through references',
168
+ (p) => {
169
+ p.files['models/crate.pxmodel'].text = JSON.stringify({
170
+ parts: [{ id: 'a', kind: 'model', src: 'models/many.pxmodel', repeat: { count: 64 } }],
171
+ });
172
+ p.files['models/many.pxmodel'] = {
173
+ sha256: 'b'.repeat(64),
174
+ text: JSON.stringify({ parts: [{ id: 'b', kind: 'box', repeat: { count: 64 } }, { id: 'c', kind: 'box', parent: 'b', repeat: { count: 64 } }] }),
175
+ };
176
+ },
177
+ 'expands to over',
178
+ );
179
+ await refuses(
180
+ 'a model whose nested repeats hang off a parent that does not exist',
181
+ (p) => {
182
+ const parts = Array.from({ length: 5 }, (_, i) => ({ id: `p${i}`, parent: i ? `p${i - 1}` : 'nobody', kind: 'box', repeat: { count: 64 } }));
183
+ p.files['models/crate.pxmodel'].text = JSON.stringify({ parts });
184
+ },
185
+ 'expands to over',
186
+ );
187
+ await refuses(
188
+ 'a scene of blueprint instances whose models add up past the scene budget',
189
+ (p) => {
190
+ p.files['models/crate.pxmodel'].text = JSON.stringify({ parts: [{ id: 'a', kind: 'box', repeat: { count: 64 } }, { id: 'b', parent: 'a', kind: 'box', repeat: { count: 64 } }] });
191
+ p.files['scenes/main.scene'].text = JSON.stringify({
192
+ actors: Array.from({ length: 100 }, (_, i) => ({ id: `c${i}`, blueprint: 'blueprints/crate.scene' })),
193
+ });
194
+ },
195
+ 'draws over',
196
+ );
197
+ await refuses(
198
+ "an imported blueprint whose model, resolved inside the import, adds up past the scene budget",
199
+ (p) => {
200
+ p.files['imports/castle.fixture/blueprints/a.scene'] = {
201
+ sha256: 'c'.repeat(64),
202
+ text: JSON.stringify({ actors: [{ components: { Model: { file: 'models/a.pxmodel' } } }] }),
203
+ };
204
+ p.files['imports/castle.fixture/models/a.pxmodel'] = {
205
+ sha256: 'd'.repeat(64),
206
+ text: JSON.stringify({ parts: [{ id: 'a', kind: 'box', repeat: { count: 64 } }, { id: 'b', parent: 'a', kind: 'box', repeat: { count: 64 } }] }),
207
+ };
208
+ p.files['scenes/main.scene'].text = JSON.stringify({
209
+ actors: Array.from({ length: 100 }, (_, i) => ({ id: `c${i}`, blueprint: '@imports/castle.fixture/blueprints/a.scene' })),
210
+ });
211
+ },
212
+ 'draws over',
213
+ );
214
+ await refuses('a scene whose actors is not a list', (p) => (p.files['scenes/main.scene'].text = '{"actors":{}}'), 'not a scene');
215
+ await refuses(
216
+ 'a place actor without components',
217
+ (p) => (p.files['scenes/main.scene'].text = JSON.stringify({ actors: [{ id: 'a' }] })),
218
+ 'an actor is { id, components }',
219
+ );
220
+ await refuses(
221
+ 'a place actor without an id',
222
+ (p) => (p.files['scenes/main.scene'].text = JSON.stringify({ actors: [{ components: {} }] })),
223
+ 'an actor is { id, components }',
224
+ );
225
+ await refuses(
226
+ 'a component that is not an object',
227
+ (p) => (p.files['scenes/main.scene'].text = JSON.stringify({ actors: [{ id: 'a', components: { Transform: 3 } }] })),
228
+ 'not an object',
229
+ );
230
+ await test('a blueprint template needs no actor ids', () => {
231
+ assert.deepEqual(validatePack(valid()).errors, []);
232
+ });
233
+
234
+ await test('the hash check agrees with node', async () => {
235
+ const bytes = fs.readFileSync(outFile);
236
+ assert.equal(await sha256Hex(new Uint8Array(bytes)), crypto.createHash('sha256').update(bytes).digest('hex'));
237
+ });
238
+
239
+ await test('a deck the contract refuses does not pack', async () => {
240
+ write('castle.json', { hub: { contract: 1, title: '' } });
241
+ const refused = await pack({ deckDir: root, outFile: path.join(root, 'refused.json') });
242
+ assert.ok(refused.errors.length > 0);
243
+ assert.ok(!fs.existsSync(path.join(root, 'refused.json')));
244
+ });
245
+
246
+ fs.rmSync(root, { recursive: true, force: true });
247
+
248
+ // --- audio --------------------------------------------------------------------
249
+
250
+ // A few hundred bytes that only name themselves sounds: nothing here decodes audio.
251
+ const fakeWav = Buffer.concat([Buffer.from('RIFF\x00\x00\x00\x00WAVEfmt '), Buffer.alloc(300, 7)]);
252
+ const fakeMp3 = Buffer.concat([Buffer.from([0xff, 0xfb, 0x90, 0x00]), Buffer.alloc(200, 3)]);
253
+ const soundRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'hub-pack-audio-test-'));
254
+ const writeIn = (rel, value) => {
255
+ const file = path.join(soundRoot, rel);
256
+ fs.mkdirSync(path.dirname(file), { recursive: true });
257
+ fs.writeFileSync(file, typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value));
258
+ };
259
+ writeIn('castle.json', {
260
+ server: { main: 'imports/castle.hub-game/code/server/index.js', maxPlayers: 8 },
261
+ imports: { 'castle.hub-game': { source: 'builtin', kit: 'hub-game', version: '9.9.9' } },
262
+ hub: {
263
+ contract: 1,
264
+ title: 'Loud',
265
+ controller: 'walk',
266
+ result: { outcomes: ['win', 'lose'] },
267
+ idle: { sounds: ['sounds/horn.wav', 'sounds/beep.mp3'] },
268
+ },
269
+ });
270
+ writeIn('scenes/main.scene', { actors: [{ id: 'goal', components: { Transform: { x: 0 } } }] });
271
+ writeIn('sounds/horn.wav', fakeWav);
272
+ writeIn('sounds/beep.mp3', fakeMp3);
273
+ writeIn('sounds/unlisted.mp3', fakeMp3);
274
+ const loudFile = path.join(soundRoot, 'out.json');
275
+ const loudResult = await pack({ deckDir: soundRoot, outFile: loudFile });
276
+ const loudText = fs.readFileSync(loudFile, 'utf8');
277
+ const loud = JSON.parse(loudText);
278
+
279
+ await test('the exporter carries exactly the listed sounds in audio', () => {
280
+ assert.deepEqual(loudResult.errors, []);
281
+ assert.match(loudResult.summary, /2 sounds/);
282
+ assert.deepEqual(Object.keys(loud.audio).sort(), ['sounds/beep.mp3', 'sounds/horn.wav']);
283
+ assert.equal(loud.audio['sounds/horn.wav'].type, 'audio/wav');
284
+ assert.equal(loud.audio['sounds/beep.mp3'].type, 'audio/mpeg');
285
+ assert.equal(loud.audio['sounds/horn.wav'].sha256, crypto.createHash('sha256').update(fakeWav).digest('hex'));
286
+ assert.ok(Buffer.from(loud.audio['sounds/horn.wav'].base64, 'base64').equals(fakeWav));
287
+ assert.deepEqual(Object.keys(loud.media), []);
288
+ assert.deepEqual(validatePack(loud).errors, []);
289
+ });
290
+
291
+ await test('a pack with no sounds has no audio key', () => {
292
+ assert.ok(!('audio' in doc));
293
+ });
294
+
295
+ const loudCopy = () => structuredClone(loud);
296
+ const refusesAudio = (name, mutate, needle) =>
297
+ test(`the validator refuses ${name}`, () => {
298
+ const p = loudCopy();
299
+ mutate(p);
300
+ const { errors } = validatePack(p);
301
+ assert.ok(errors.some((e) => e.includes(needle)), `${needle} not in ${JSON.stringify(errors)}`);
302
+ });
303
+ await refusesAudio('audio that is not an object', (p) => (p.audio = []), 'audio is not an object');
304
+ await refusesAudio('audio of the wrong type', (p) => (p.audio['sounds/horn.wav'].type = 'audio/mpeg'), 'audio entry');
305
+ await refusesAudio('audio typed as a page', (p) => (p.audio['sounds/beep.mp3'].type = 'text/html'), 'audio entry');
306
+ await refusesAudio(
307
+ 'audio with an extension that is not a sound',
308
+ (p) => {
309
+ p.places.main.idle.sounds.push('sounds/x.ogg');
310
+ p.audio['sounds/x.ogg'] = { ...p.audio['sounds/beep.mp3'] };
311
+ },
312
+ 'not audio a pack may hold',
313
+ );
314
+ await refusesAudio(
315
+ 'audio with an upper-case extension',
316
+ (p) => {
317
+ p.places.main.idle.sounds.push('sounds/x.MP3');
318
+ p.audio['sounds/x.MP3'] = { ...p.audio['sounds/beep.mp3'] };
319
+ },
320
+ 'not audio a pack may hold',
321
+ );
322
+ await refusesAudio('audio at a path that climbs', (p) => (p.audio['../x.mp3'] = { ...p.audio['sounds/beep.mp3'] }), 'not audio a pack may hold');
323
+ await refusesAudio('audio no place lists', (p) => (p.audio['sounds/unlisted.mp3'] = { ...p.audio['sounds/beep.mp3'] }), 'no place');
324
+ await refusesAudio('audio that is not base64', (p) => (p.audio['sounds/beep.mp3'].base64 = '<script>'), 'audio entry');
325
+ await refusesAudio('audio without a sha256', (p) => delete p.audio['sounds/beep.mp3'].sha256, 'audio entry');
326
+ await refusesAudio(
327
+ 'a sound over the per-file budget',
328
+ (p) => (p.audio['sounds/horn.wav'].base64 = 'A'.repeat(Math.ceil(((PACK_LIMITS.audioFileBytes + 4) * 4) / 3 / 4) * 4)),
329
+ `over ${PACK_LIMITS.audioFileBytes}`,
330
+ );
331
+ await test('a sound exactly at the per-file budget is accepted (base64 padding is not counted)', () => {
332
+ const p = loudCopy();
333
+ p.audio['sounds/horn.wav'].base64 = Buffer.alloc(PACK_LIMITS.audioFileBytes).toString('base64');
334
+ assert.ok(p.audio['sounds/horn.wav'].base64.endsWith('='));
335
+ assert.deepEqual(validatePack(p).errors, []);
336
+ });
337
+ await refusesAudio(
338
+ 'sounds over the audio budget',
339
+ (p) => {
340
+ const each = 'A'.repeat(Math.floor((PACK_LIMITS.audioFileBytes * 4) / 3 / 4) * 4);
341
+ const many = Array.from({ length: 5 }, (_, i) => `sounds/s${i}.mp3`);
342
+ p.places.main.idle.sounds = many;
343
+ p.audio = Object.fromEntries(many.map((file) => [file, { sha256: 'a'.repeat(64), type: 'audio/mpeg', base64: each }]));
344
+ },
345
+ 'bytes of audio, over',
346
+ );
347
+ await refusesAudio(
348
+ 'audio that takes the pack over the file count',
349
+ (p) => {
350
+ for (let i = 0; i < PACK_LIMITS.files; i++) {
351
+ p.files[`blueprints/b${i}.scene`] = { sha256: 'a'.repeat(64), text: '{"actors":[]}' };
352
+ }
353
+ },
354
+ `over ${PACK_LIMITS.files} files`,
355
+ );
356
+
357
+ await test('the validator leaves a top-level key it does not know alone, as one that predates audio did', () => {
358
+ // An older engine reads only files, media and places: to it a pack with
359
+ // audio is the same pack without, which validates.
360
+ const older = loudCopy();
361
+ delete older.audio;
362
+ assert.deepEqual(validatePack(older).errors, []);
363
+ assert.deepEqual(validatePack({ ...loudCopy(), video: { x: 1 } }).errors, []);
364
+ });
365
+
366
+ await test('a deck listing a sound it does not have does not pack', async () => {
367
+ const castleJson = JSON.parse(fs.readFileSync(path.join(soundRoot, 'castle.json'), 'utf8'));
368
+ castleJson.hub.idle.sounds = ['sounds/gone.mp3'];
369
+ writeIn('castle.json', castleJson);
370
+ const refused = await pack({ deckDir: soundRoot, outFile: path.join(soundRoot, 'refused.json') });
371
+ assert.ok(refused.errors.some((e) => e.includes('sounds/gone.mp3')), JSON.stringify(refused.errors));
372
+ });
373
+
374
+ await test('the town mounts a pack with its sounds as data: URLs, and a place plays only its own content', async () => {
375
+ globalThis.__packTestMedia = { 'sounds/horn.wav': 'data:audio/wav;base64,TOWN' };
376
+ const Packs = await import('../code/client/pack.js');
377
+ const { soundUrl } = await import('../code/client/sound.js');
378
+ const sha = crypto.createHash('sha256').update(loudText).digest('hex');
379
+ const realFetch = globalThis.fetch;
380
+ globalThis.fetch = async () => new Response(loudText);
381
+ try {
382
+ // The town's own avatar sets, every one, and what they draw are laid over
383
+ // the pack; nothing else of the town's is.
384
+ const kit = 'imports/castle.hub-game/';
385
+ const blueprint = (file) => JSON.stringify({ actors: [{ components: { Model: { file } } }] });
386
+ const baseFiles = {
387
+ [`${kit}blueprints/avatar-0.scene`]: blueprint('models/avatar-0.pxmodel'),
388
+ [`${kit}models/avatar-0.pxmodel`]: '{"parts":[]}',
389
+ [`${kit}blueprints/pixel/avatar-0.scene`]: blueprint('models/pixel/avatar-0.pxmodel'),
390
+ [`${kit}models/pixel/avatar-0.pxmodel`]: JSON.stringify({ parts: [{ art: { front: 'imports/castle.hub-game/drawings/pixel/kai-face.sprite' } }] }),
391
+ [`${kit}drawings/pixel/kai-face.sprite`]: '{"frames":[]}',
392
+ [`${kit}blueprints/bean/avatar-3.scene`]: blueprint('models/bean/avatar-3.pxmodel'),
393
+ [`${kit}models/bean/avatar-3.pxmodel`]: '{"parts":[]}',
394
+ [`${kit}blueprints/bean/statue.scene`]: blueprint('models/statue.pxmodel'),
395
+ [`${kit}models/statue.pxmodel`]: '{"parts":[]}',
396
+ 'scenes/town.scene': '{"actors":[]}',
397
+ };
398
+ const content = await Packs.loadPack(sha, 'https://cdn/loud.json', { baseFiles });
399
+ const town = Object.keys(baseFiles).filter((path) => path in content.files && !content.carried.files.includes(path));
400
+ assert.deepEqual(
401
+ town.sort(),
402
+ Object.keys(baseFiles)
403
+ .filter((path) => !/statue|town/.test(path))
404
+ .sort(),
405
+ );
406
+ assert.equal(content.audio['sounds/horn.wav'], `data:audio/wav;base64,${loud.audio['sounds/horn.wav'].base64}`);
407
+ assert.equal(content.audio['sounds/beep.mp3'], `data:audio/mpeg;base64,${loud.audio['sounds/beep.mp3'].base64}`);
408
+ const listed = loud.places.main.idle.sounds;
409
+ // A pack place plays the pack's file, never the town's of the same name...
410
+ assert.equal(soundUrl('sounds/horn.wav', listed, content), content.audio['sounds/horn.wav']);
411
+ // ...the town plays its own...
412
+ assert.equal(soundUrl('sounds/horn.wav', listed, null), 'data:audio/wav;base64,TOWN');
413
+ // ...and neither plays a file the place does not list, or the other's.
414
+ assert.equal(soundUrl('sounds/unlisted.mp3', listed, content), null);
415
+ assert.equal(soundUrl('sounds/beep.mp3', listed, null), null);
416
+ // A pack built before audio is silent.
417
+ assert.equal(soundUrl('sounds/horn.wav', listed, { files: {} }), null);
418
+ } finally {
419
+ globalThis.fetch = realFetch;
420
+ }
421
+ });
422
+
423
+ fs.rmSync(soundRoot, { recursive: true, force: true });
424
+ console.log(`${passed} pack tests passed.`);