castle-web-cli 0.4.229 → 0.4.231

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 (259) hide show
  1. package/dist/agent-prompts.d.ts +2 -0
  2. package/dist/agent-prompts.js +16 -8
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +21 -21
  5. package/dist/api.d.ts +28 -2
  6. package/dist/api.js +74 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +860 -3
  9. package/dist/castleJson.d.ts +44 -0
  10. package/dist/castleJson.js +131 -0
  11. package/dist/check.js +27 -1
  12. package/dist/contentPack.d.ts +49 -0
  13. package/dist/contentPack.js +273 -0
  14. package/dist/devExperiments.d.ts +21 -0
  15. package/dist/devExperiments.js +139 -0
  16. package/dist/devHub.d.ts +55 -0
  17. package/dist/devHub.js +187 -0
  18. package/dist/devSaveStorage.d.ts +36 -0
  19. package/dist/devSaveStorage.js +469 -0
  20. package/dist/devSessionChild.js +25 -13
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +237 -46
  23. package/dist/devSessionStorage.d.ts +9 -0
  24. package/dist/devSessionStorage.js +66 -1
  25. package/dist/experimentAssignment.d.ts +15 -0
  26. package/dist/experimentAssignment.js +41 -0
  27. package/dist/importCheckHook.d.ts +40 -0
  28. package/dist/importCheckHook.js +171 -0
  29. package/dist/importRegression.d.ts +15 -0
  30. package/dist/importRegression.js +100 -0
  31. package/dist/importTrust.d.ts +2 -0
  32. package/dist/importTrust.js +27 -0
  33. package/dist/importUpdateHook.d.ts +18 -0
  34. package/dist/importUpdateHook.js +31 -8
  35. package/dist/importUpdateHookRunner.js +19 -7
  36. package/dist/imports.d.ts +16 -0
  37. package/dist/imports.js +141 -31
  38. package/dist/index.js +3 -1
  39. package/dist/init.d.ts +3 -0
  40. package/dist/init.js +126 -9
  41. package/dist/install.js +4 -1
  42. package/dist/localPaths.js +4 -0
  43. package/dist/login.js +2 -2
  44. package/dist/platformDoc.js +3 -8
  45. package/dist/playPagePolicy.js +3 -1
  46. package/dist/remix.js +5 -0
  47. package/dist/routerTaskReport.d.ts +28 -0
  48. package/dist/routerTaskReport.js +56 -0
  49. package/dist/save-deck.d.ts +18 -0
  50. package/dist/save-deck.js +142 -6
  51. package/dist/serve.d.ts +8 -0
  52. package/dist/serve.js +99 -14
  53. package/dist/serverBundle.d.ts +1 -1
  54. package/dist/serverBundle.js +5 -1
  55. package/dist/webSourceSync.d.ts +2 -0
  56. package/dist/webSourceSync.js +2 -0
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +406 -53
  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 +9 -0
  64. package/kits/base/sdk/castle.js +5 -0
  65. package/kits/base/sdk/commands.d.ts +223 -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 +45 -1
  73. package/kits/base/sdk/multiplayer.js +158 -8
  74. package/kits/base/sdk/save.d.ts +197 -0
  75. package/kits/base/sdk/save.js +525 -0
  76. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  77. package/kits/base/sdk/saveCoordinator.js +252 -0
  78. package/kits/base/sdk/server/hub.d.ts +169 -0
  79. package/kits/base/sdk/server/hub.js +125 -0
  80. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  81. package/kits/base/sdk/server/storage.d.ts +58 -4
  82. package/kits/base/sdk/server/storage.js +25 -5
  83. package/kits/base/sdk/server/wrapper.d.ts +36 -2
  84. package/kits/base/sdk/server/wrapper.js +92 -11
  85. package/kits/base/sdk/store.d.ts +3 -2
  86. package/kits/base/sdk/store.js +5 -0
  87. package/kits/base/sdk/transport.d.ts +2 -1
  88. package/kits/base/sdk/transport.js +13 -1
  89. package/kits/hub-game/.prettierrc +5 -0
  90. package/kits/hub-game/CLAUDE.md +149 -0
  91. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  92. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  93. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  94. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  95. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  96. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  97. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  98. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  99. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  100. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  101. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  102. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  103. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  104. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  105. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  106. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  107. package/kits/hub-game/castle.json +96 -0
  108. package/kits/hub-game/code/client/attention.js +133 -0
  109. package/kits/hub-game/code/client/avatar.js +288 -0
  110. package/kits/hub-game/code/client/cabinets.js +90 -0
  111. package/kits/hub-game/code/client/chat.js +135 -0
  112. package/kits/hub-game/code/client/chrome.js +1035 -0
  113. package/kits/hub-game/code/client/doorFrame.js +15 -0
  114. package/kits/hub-game/code/client/firstJoin.js +14 -0
  115. package/kits/hub-game/code/client/grab.js +376 -0
  116. package/kits/hub-game/code/client/hookLayer.js +236 -0
  117. package/kits/hub-game/code/client/idle.js +326 -0
  118. package/kits/hub-game/code/client/keys.js +48 -0
  119. package/kits/hub-game/code/client/pack.js +232 -0
  120. package/kits/hub-game/code/client/packFormat.js +397 -0
  121. package/kits/hub-game/code/client/say.js +127 -0
  122. package/kits/hub-game/code/client/sha256.js +80 -0
  123. package/kits/hub-game/code/client/sound.js +147 -0
  124. package/kits/hub-game/code/client/state.js +143 -0
  125. package/kits/hub-game/code/client/talk.js +233 -0
  126. package/kits/hub-game/code/client/tiers.js +480 -0
  127. package/kits/hub-game/code/client/together.js +440 -0
  128. package/kits/hub-game/code/client/wear.js +149 -0
  129. package/kits/hub-game/code/server/actors.js +627 -0
  130. package/kits/hub-game/code/server/blocks.js +103 -0
  131. package/kits/hub-game/code/server/cabinets.js +309 -0
  132. package/kits/hub-game/code/server/game.js +137 -0
  133. package/kits/hub-game/code/server/grab.js +248 -0
  134. package/kits/hub-game/code/server/history.js +96 -0
  135. package/kits/hub-game/code/server/hub.js +1998 -0
  136. package/kits/hub-game/code/server/idle.js +412 -0
  137. package/kits/hub-game/code/server/index.js +64 -0
  138. package/kits/hub-game/code/server/labels.js +59 -0
  139. package/kits/hub-game/code/server/manifest.js +554 -0
  140. package/kits/hub-game/code/server/npcs.js +733 -0
  141. package/kits/hub-game/code/server/party.js +439 -0
  142. package/kits/hub-game/code/server/place.js +43 -0
  143. package/kits/hub-game/code/server/say.js +83 -0
  144. package/kits/hub-game/code/server/wear.js +142 -0
  145. package/kits/hub-game/code/systems/hub.js +2347 -0
  146. package/kits/hub-game/docs/content-pack.md +151 -0
  147. package/kits/hub-game/docs/contract.md +1209 -0
  148. package/kits/hub-game/docs/style.md +137 -0
  149. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  150. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  151. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  152. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  153. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  154. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  155. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  156. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  157. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  158. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  159. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  160. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  161. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  162. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  163. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  164. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  165. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  166. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  167. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  168. package/kits/hub-game/eslint.config.js +20 -0
  169. package/kits/hub-game/fonts.generated.js +9 -0
  170. package/kits/hub-game/index.html +22 -0
  171. package/kits/hub-game/main.jsx +6 -0
  172. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  174. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  175. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  176. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  177. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  178. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  179. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  180. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  181. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  182. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  183. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  184. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  185. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  186. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  187. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  188. package/kits/hub-game/package-lock.json +365 -0
  189. package/kits/hub-game/package.json +32 -0
  190. package/kits/hub-game/scenes/main.scene +318 -0
  191. package/kits/hub-game/scripts/check.mjs +339 -0
  192. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  193. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  194. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  195. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  196. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  197. package/kits/hub-game/scripts/pack.mjs +192 -0
  198. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  199. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  200. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  201. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  202. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  203. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  204. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  205. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  206. package/kits/hub-game/scripts/test-door.mjs +56 -0
  207. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  208. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  209. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  210. package/kits/hub-game/scripts/test-hub-trip.mjs +1518 -0
  211. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  212. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  213. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  214. package/kits/hub-game/scripts/test-manifest.mjs +270 -0
  215. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  216. package/kits/hub-game/scripts/test-npcs.mjs +724 -0
  217. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  218. package/kits/hub-game/scripts/test-party.mjs +369 -0
  219. package/kits/hub-game/scripts/test-say.mjs +149 -0
  220. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  221. package/kits/hub-game/scripts/test-together.mjs +215 -0
  222. package/kits/hub-game/theme.style +3 -0
  223. package/kits/multiplayer-3d/castle.json +5 -5
  224. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  225. package/kits/multiplayer-3d/code/server/players.js +4 -1
  226. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  227. package/kits/multiplayer-3d/code/server/world.js +20 -7
  228. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  229. package/kits/physics-2d/CLAUDE.md +2 -0
  230. package/kits/physics-2d/castle.json +2 -2
  231. package/kits/physics-2d/engine/art.js +13 -0
  232. package/kits/physics-2d/engine/assets.js +4 -2
  233. package/kits/physics-2d/engine/scene.js +117 -5
  234. package/kits/physics-2d/package.json +1 -1
  235. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  236. package/kits/physics-3d/CLAUDE.md +285 -1
  237. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  238. package/kits/physics-3d/castle.json +3 -3
  239. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
  240. package/kits/physics-3d/engine3d/actornode.js +253 -0
  241. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  242. package/kits/physics-3d/engine3d/materials.js +29 -1
  243. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  244. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  245. package/kits/physics-3d/engine3d/world3d.js +527 -5
  246. package/kits/physics-3d/package.json +1 -1
  247. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  248. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  249. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  250. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  251. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  252. package/kits/physics-3d/systems/physics3d.js +187 -5
  253. package/kits/real-time/castle.json +2 -2
  254. package/kits/real-time/code/client/connection.js +77 -7
  255. package/kits/real-time/code/client/gameHooks.js +63 -3
  256. package/kits/real-time/code/client/messages.js +7 -0
  257. package/kits/real-time/code/server/gameHooks.js +24 -0
  258. package/kits/real-time/code/server/session.js +390 -22
  259. package/package.json +2 -2
@@ -0,0 +1,1518 @@
1
+ // Tests over the client's production door path (code/systems/hub.js): a pad
2
+ // ghost fired is a host-acquired trip (Hub.go) whose content pack loads under
3
+ // the cover and whose join is a hub join on the handoff; the way back is
4
+ // Hub.back; an acquisition the trip will not use is given back; the host's
5
+ // Back control is answered and the kit's own Back steps aside for it. The
6
+ // dev serve's keyed doors are unchanged. castle-web-sdk and the pack loader
7
+ // are stubbed. `npm run check` runs this.
8
+
9
+ import assert from 'node:assert/strict';
10
+ import { register } from 'node:module';
11
+
12
+ const sdkStub = 'export const Hub = globalThis.__tripHub;';
13
+ const packStub = `
14
+ export const loadPack = (...args) => globalThis.__tripLoadPack(...args);
15
+ export const cachedPacks = () => [];
16
+ export const matchesOwn = (...args) => Boolean(globalThis.__tripMatchesOwn?.(...args));
17
+ export const resolveDeckDoor = () => Promise.reject(new Error('no dev decks here'));
18
+ `;
19
+ // The engine's media, which the place's sounds play from (client/sound.js).
20
+ const mediaStub = 'export const initialMedia = {}; export const playMedia = () => {}; export const stopMedia = () => {};';
21
+ register(
22
+ 'data:text/javascript,' +
23
+ encodeURIComponent(`
24
+ export async function resolve(specifier, context, next) {
25
+ if (specifier === 'castle-web-sdk') {
26
+ return { url: ${JSON.stringify('data:text/javascript,' + encodeURIComponent(sdkStub))}, shortCircuit: true };
27
+ }
28
+ if (specifier.startsWith('@imports/castle.physics-2d/engine/')) {
29
+ return { url: ${JSON.stringify('data:text/javascript,' + encodeURIComponent(mediaStub))}, shortCircuit: true };
30
+ }
31
+ if (specifier === '../client/pack.js') {
32
+ return { url: ${JSON.stringify('data:text/javascript,' + encodeURIComponent(packStub))}, shortCircuit: true };
33
+ }
34
+ return next(specifier, context);
35
+ }`),
36
+ );
37
+
38
+ const calls = [];
39
+ const answers = {};
40
+ globalThis.__tripHub = {
41
+ go: async (doorId) => {
42
+ calls.push(['go', doorId]);
43
+ if (answers.goError) {
44
+ throw Object.assign(new Error('refused'), { code: answers.goError });
45
+ }
46
+ return answers.go;
47
+ },
48
+ back: async () => {
49
+ calls.push(['back']);
50
+ if (answers.backError) {
51
+ throw Object.assign(new Error('refused'), { code: answers.backError });
52
+ }
53
+ return answers.backs?.length ? answers.backs.shift() : answers.back;
54
+ },
55
+ report: async (userId, reason) => {
56
+ calls.push(['report', userId, reason]);
57
+ if (answers.reportError) {
58
+ throw Object.assign(new Error('refused'), { code: answers.reportError });
59
+ }
60
+ return answers.report;
61
+ },
62
+ jump: async (userId) => {
63
+ calls.push(['jump', userId]);
64
+ return answers.jump;
65
+ },
66
+ friends: async () => {
67
+ calls.push(['friends']);
68
+ if (answers.friendsError) {
69
+ throw Object.assign(new Error(answers.friendsError), { code: answers.friendsError });
70
+ }
71
+ return answers.friends;
72
+ },
73
+ cancel: async () => {
74
+ calls.push(['cancel']);
75
+ return { status: 'ok' };
76
+ },
77
+ onBack: (listener) => {
78
+ calls.push(['onBack']);
79
+ answers.backListener = listener;
80
+ return () => {};
81
+ },
82
+ };
83
+ globalThis.__tripLoadPack = async (sha, url) => {
84
+ calls.push(['loadPack', sha, url]);
85
+ if (answers.loadPackError) {
86
+ throw new Error(answers.loadPackError);
87
+ }
88
+ return { id: `pack-${sha}`, sha256: sha, bytes: 10, ms: 1, cached: false, places: { main: { scene: 'scenes/race.scene', title: 'Foot Race' } } };
89
+ };
90
+
91
+ const { installSystem, BANNED_TEXT } = await import('../code/systems/hub.js');
92
+ const HubState = await import('../code/client/state.js');
93
+ const { makeTogether } = await import('../code/client/together.js');
94
+
95
+ const tick = () => new Promise((resolve) => setTimeout(resolve, 0));
96
+ const flush = async () => {
97
+ for (let i = 0; i < 4; i += 1) {
98
+ await tick();
99
+ }
100
+ };
101
+ const handoff = (extra = {}) => ({ status: 'ok', url: 'wss://x', nonce: 'n', sessionId: 's', bundleHash: 'abc', contentUrl: 'https://cdn/abc.json', ...extra });
102
+
103
+ function setup() {
104
+ let system;
105
+ installSystem({ registerSystem: (s) => (system = s) });
106
+ system.chrome = { setCover() {}, showBack(...args) { system.backShown = args; }, setToast(text) { system.toastShown = text; } };
107
+ system.scenePath = 'scenes/main.scene';
108
+ const scene = {
109
+ persistent: { hubTickets: [] },
110
+ multiplayer: { target: { mode: 'public' } },
111
+ content: null,
112
+ files: {},
113
+ mountsContent: true,
114
+ setContent() {},
115
+ actorWith: () => null,
116
+ getActor: () => null,
117
+ getActors: () => [],
118
+ };
119
+ calls.length = 0;
120
+ for (const key of Object.keys(answers)) {
121
+ delete answers[key];
122
+ }
123
+ return { system, scene };
124
+ }
125
+
126
+ const warn = console.warn;
127
+ const log = console.log;
128
+ console.warn = () => {};
129
+ console.log = () => {};
130
+
131
+ let n = 0;
132
+ async function test(name, fn) {
133
+ try {
134
+ await fn();
135
+ } catch (err) {
136
+ console.warn = warn;
137
+ console.log = log;
138
+ console.error(`not ok: ${name}`);
139
+ throw err;
140
+ }
141
+ n += 1;
142
+ }
143
+
144
+ await test('a fired pad is Hub.go, then the pack the handoff names, then a hub join', async () => {
145
+ const { system, scene } = setup();
146
+ answers.go = handoff();
147
+ system.goHub(scene, { k: 'hub-go', door: 'race', pad: 'race.1', key: null, title: 'Foot Race', party: 2 });
148
+ assert.equal(scene.persistent.hubTickets.length, 1);
149
+ assert.equal(scene.persistent.hubTickets[0].hub, true);
150
+ await flush();
151
+ const trip = system.trip;
152
+ assert.equal(system.contentReady(scene, trip, 0), true);
153
+ assert.deepEqual(calls, [['go', 'race'], ['loadPack', 'abc', 'https://cdn/abc.json']]);
154
+ assert.deepEqual(trip.target.join, { mode: 'hub', handoff: answers.go });
155
+ assert.equal(trip.target.scene, 'scenes/race.scene');
156
+ assert.equal(trip.target.content.sha256, 'abc');
157
+ });
158
+
159
+ await test('a door the host refuses stays shut, and nothing is given back', async () => {
160
+ const { system, scene } = setup();
161
+ answers.go = { status: 'full', url: null, nonce: null };
162
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
163
+ await flush();
164
+ assert.equal(system.contentReady(scene, system.trip, 0), false);
165
+ assert.equal(system.trip.shut, 'full');
166
+ assert.equal(scene.persistent.hubTickets.length, 0);
167
+ assert.deepEqual(calls, [['go', 'race']]);
168
+ });
169
+
170
+ await test('a handoff with no content pack is given back and the door stays shut', async () => {
171
+ const { system, scene } = setup();
172
+ answers.go = handoff({ contentUrl: undefined });
173
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
174
+ await flush();
175
+ assert.equal(system.contentReady(scene, system.trip, 0), false);
176
+ assert.match(system.trip.shut, /no content pack/);
177
+ assert.deepEqual(calls, [['go', 'race'], ['cancel']]);
178
+ });
179
+
180
+ await test('a pack that fails to load is given back', async () => {
181
+ const { system, scene } = setup();
182
+ answers.go = handoff();
183
+ answers.loadPackError = 'hash mismatch';
184
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
185
+ await flush();
186
+ assert.equal(system.contentReady(scene, system.trip, 0), false);
187
+ assert.equal(system.trip.shut, 'hash mismatch');
188
+ assert.deepEqual(calls.at(-1), ['cancel']);
189
+ });
190
+
191
+ await test('an acquisition that lands after the trip gave up is given back', async () => {
192
+ const { system, scene } = setup();
193
+ let release;
194
+ const go = globalThis.__tripHub.go;
195
+ globalThis.__tripHub.go = (doorId) => {
196
+ calls.push(['go', doorId]);
197
+ return new Promise((resolve) => (release = resolve));
198
+ };
199
+ try {
200
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
201
+ await flush();
202
+ const trip = system.trip;
203
+ // Past the loading limit with nothing back.
204
+ assert.equal(system.contentReady(scene, trip, 0), false);
205
+ trip.startedAt = -1e9;
206
+ assert.equal(system.contentReady(scene, trip, 0), false);
207
+ assert.equal(trip.shut, 'it took too long');
208
+ release(handoff());
209
+ await flush();
210
+ await flush();
211
+ assert.deepEqual(calls.at(-1), ['cancel']);
212
+ } finally {
213
+ globalThis.__tripHub.go = go;
214
+ }
215
+ });
216
+
217
+ await test('the way back from a hub trip is Hub.back', async () => {
218
+ const { system, scene } = setup();
219
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
220
+ scene.persistent.hubTickets.push(ticket);
221
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
222
+ system.back(scene);
223
+ await flush();
224
+ const trip = system.trip;
225
+ assert.equal(system.contentReady(scene, trip, 0), true);
226
+ // This runtime's own content is no release ghost can name: the pack is
227
+ // loaded, so the place is drawn from what its server runs (unless the pack
228
+ // is the deck's own content byte for byte, below).
229
+ assert.deepEqual(calls, [['back'], ['loadPack', 'town', 'https://cdn/town.json']]);
230
+ assert.deepEqual(trip.target.join, { mode: 'hub', handoff: answers.back });
231
+ assert.equal(trip.target.content.sha256, 'town');
232
+ assert.equal(trip.target.arrive.returning, true);
233
+ });
234
+
235
+ await test("a way back into the deck's own place from a pack that is its own content keeps that content", async () => {
236
+ const { system, scene } = setup();
237
+ scene.ownContent = { files: { 'scenes/main.scene': 'town' } };
238
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
239
+ scene.persistent.hubTickets.push(ticket);
240
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
241
+ const compared = [];
242
+ globalThis.__tripMatchesOwn = (content, baseFiles) => {
243
+ compared.push([content.sha256, baseFiles]);
244
+ return true;
245
+ };
246
+ try {
247
+ system.back(scene);
248
+ await flush();
249
+ const trip = system.trip;
250
+ assert.equal(system.contentReady(scene, trip, 0), true);
251
+ assert.deepEqual(calls, [['back'], ['loadPack', 'town', 'https://cdn/town.json']]);
252
+ // Compared against the deck's own files, not whatever is mounted.
253
+ assert.deepEqual(compared, [['town', scene.ownContent.files]]);
254
+ // The deck's own content and the ticket's scene: the parked town is drawn again.
255
+ assert.equal(trip.target.content, null);
256
+ assert.equal(trip.target.scene, 'scenes/main.scene');
257
+ assert.equal(trip.pack, undefined);
258
+ assert.deepEqual(trip.target.join, { mode: 'hub', handoff: answers.back });
259
+ } finally {
260
+ delete globalThis.__tripMatchesOwn;
261
+ }
262
+
263
+ // A pack that is not the deck's own content (a newer release) is mounted.
264
+ const again = setup();
265
+ again.scene.persistent.hubTickets.push({ ...ticket });
266
+ answers.back = handoff({ bundleHash: 'town-v2', contentUrl: 'https://cdn/town-v2.json' });
267
+ globalThis.__tripMatchesOwn = () => false;
268
+ try {
269
+ again.system.back(again.scene);
270
+ await flush();
271
+ assert.equal(again.system.contentReady(again.scene, again.system.trip, 0), true);
272
+ assert.equal(again.system.trip.target.content.sha256, 'town-v2');
273
+ } finally {
274
+ delete globalThis.__tripMatchesOwn;
275
+ }
276
+ });
277
+
278
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
279
+
280
+ await test('a way back refused as unavailable is asked once more under the cover', async () => {
281
+ const { system, scene } = setup();
282
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
283
+ scene.persistent.hubTickets.push(ticket);
284
+ answers.backs = [{ status: 'unavailable' }, handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' })];
285
+ system.back(scene);
286
+ await flush();
287
+ const trip = system.trip;
288
+ // Refused: the cover stays down and the door does not shut yet.
289
+ assert.equal(system.contentReady(scene, trip, 0), false);
290
+ assert.equal(trip.shut, undefined);
291
+ assert.deepEqual(calls, [['back']]);
292
+ await sleep(1600);
293
+ await flush();
294
+ assert.equal(system.contentReady(scene, trip, 0), true);
295
+ assert.deepEqual(calls, [['back'], ['back'], ['loadPack', 'town', 'https://cdn/town.json']]);
296
+ assert.equal(trip.target.join.mode, 'hub');
297
+ assert.equal(trip.shut, undefined);
298
+ });
299
+
300
+ await test('a way back refused late is still asked again, on a deadline of its own', async () => {
301
+ const { system, scene } = setup();
302
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
303
+ scene.persistent.hubTickets.push(ticket);
304
+ answers.backs = [{ status: 'unavailable' }, handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' })];
305
+ system.back(scene);
306
+ await flush();
307
+ const trip = system.trip;
308
+ // The refusal is seen 14.9 s into the trip, the next frame at 15.1 s.
309
+ assert.equal(system.contentReady(scene, trip, trip.startedAt + 14900), false);
310
+ assert.equal(system.contentReady(scene, trip, trip.startedAt + 15100), false);
311
+ assert.equal(trip.shut, undefined);
312
+ await sleep(1600);
313
+ await flush();
314
+ assert.equal(system.contentReady(scene, trip, trip.startedAt + 16800), true);
315
+ assert.equal(trip.shut, undefined);
316
+ });
317
+
318
+ await test('a way back refused twice stays shut and keeps its ticket; not_found is not asked again', async () => {
319
+ const { system, scene } = setup();
320
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
321
+ scene.persistent.hubTickets.push(ticket);
322
+ answers.back = { status: 'unavailable' };
323
+ system.back(scene);
324
+ await flush();
325
+ const trip = system.trip;
326
+ assert.equal(system.contentReady(scene, trip, 0), false);
327
+ await sleep(1600);
328
+ await flush();
329
+ assert.equal(system.contentReady(scene, trip, 0), false);
330
+ assert.equal(trip.shut, 'unavailable');
331
+ assert.deepEqual(calls, [['back'], ['back']]);
332
+ assert.deepEqual(scene.persistent.hubTickets, [ticket]);
333
+
334
+ const again = setup();
335
+ again.scene.persistent.hubTickets.push({ ...ticket });
336
+ answers.back = { status: 'not_found' };
337
+ again.system.back(again.scene);
338
+ await flush();
339
+ assert.equal(again.system.contentReady(again.scene, again.system.trip, 0), false);
340
+ assert.equal(again.system.trip.shut, 'not_found');
341
+ assert.deepEqual(calls, [['back']]);
342
+ });
343
+
344
+ await test('a way back the host refuses keeps its ticket', async () => {
345
+ const { system, scene } = setup();
346
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
347
+ scene.persistent.hubTickets.push(ticket);
348
+ answers.back = { status: 'not_found' };
349
+ system.back(scene);
350
+ await flush();
351
+ assert.equal(system.contentReady(scene, system.trip, 0), false);
352
+ assert.deepEqual(scene.persistent.hubTickets, [ticket]);
353
+ });
354
+
355
+ await test("a game's end stays armed until the player is in another session", async () => {
356
+ const { system, scene } = setup();
357
+ scene.multiplayer.target = { mode: 'hub', handoff: { sessionId: 'game-1' } };
358
+ scene.currentScenePath = 'scenes/main.scene';
359
+ system.runCommands(scene, 1000);
360
+ HubState.hub.commands.push({ k: 'hub-end', why: 'end' });
361
+ system.runCommands(scene, 1000);
362
+ assert.ok(system.pendingEnd > 1000);
363
+ // A way back refused while still in the ended session: still armed.
364
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
365
+ scene.persistent.hubTickets.push(ticket);
366
+ answers.back = { status: 'unavailable' };
367
+ system.back(scene);
368
+ await flush();
369
+ assert.equal(system.contentReady(scene, system.trip, 100), false);
370
+ system.settleEnd(scene);
371
+ assert.notEqual(system.pendingEnd, null);
372
+ // A rejoin of the same visit: still armed. A fresh instance of the same
373
+ // game, or the town: settled.
374
+ system.settleEnd(scene);
375
+ assert.notEqual(system.pendingEnd, null);
376
+ scene.multiplayer.target = { mode: 'hub', handoff: { sessionId: 'game-2' } };
377
+ system.settleEnd(scene);
378
+ assert.equal(system.pendingEnd, null);
379
+ });
380
+
381
+ await test("the town's server replaced under a player who came back through a door: the join the runtime began with is made again", async () => {
382
+ const closedNet = (error = null) => ({ closed: () => true, closeError: () => error });
383
+ const run = ({ target, depth = 0, place = { title: 'Town' }, tickets = [], error = null, root }) => {
384
+ const { system, scene } = setup();
385
+ const switched = [];
386
+ scene.multiplayer = { target, net: null, switchSession: (sc, join, opts) => switched.push([join, opts ?? null]) };
387
+ scene.persistent.hubDepth = depth;
388
+ scene.persistent.hubTickets = tickets;
389
+ if (root) {
390
+ scene.persistent.hubRootJoin = root;
391
+ }
392
+ HubState.hub.place = place;
393
+ system.lastNet = closedNet(error);
394
+ system.watchSession(scene, scene.multiplayer);
395
+ return { system, switched };
396
+ };
397
+ // Back in the town by a hub join (its handoff spent), and the session ends.
398
+ let r = run({ target: { mode: 'hub', handoff: { sessionId: 'town-1' } } });
399
+ assert.deepEqual(r.switched, [[{ mode: 'public' }, null]]);
400
+ assert.equal(r.system.toast.text, 'Reconnecting…');
401
+ assert.equal(r.system.lastNet, null, 'once');
402
+ r = run({ target: { mode: 'hub', handoff: {} }, root: { mode: 'named', key: 'k' } });
403
+ assert.deepEqual(r.switched, [[{ mode: 'named', key: 'k' }, null]]);
404
+ // A join the multiplayer kit makes again by itself: left to it.
405
+ assert.deepEqual(run({ target: { mode: 'public' } }).switched, []);
406
+ // In a game, or barred from the hub: not this.
407
+ assert.deepEqual(run({ target: { mode: 'hub', handoff: {} }, place: { title: 'Foot Race', result: {} } }).switched, []);
408
+ assert.deepEqual(run({ target: { mode: 'hub', handoff: {} }, error: 'CAULDRON_HUB_BANNED' }).switched, []);
409
+ assert.deepEqual(calls.filter((c) => c[0] === 'back'), []);
410
+ // A trip of ours closes the old connection: not an ending.
411
+ const { system, scene } = setup();
412
+ const switched = [];
413
+ scene.multiplayer = { target: { mode: 'hub', handoff: {} }, net: null, switchSession: (...a) => switched.push(a) };
414
+ system.lastNet = closedNet();
415
+ system.trip = { phase: 'covering' };
416
+ system.watchSession(scene, scene.multiplayer);
417
+ assert.deepEqual(switched, []);
418
+ HubState.hub.place = null;
419
+ });
420
+
421
+ await test('a place behind a door whose server is replaced under a player: left by its way back, once', async () => {
422
+ const closedNet = (error = null) => ({ closed: () => true, closeError: () => error });
423
+ const run = ({ depth = 1, place = { title: 'Pebble Cove' }, tickets, error = null }) => {
424
+ const { system, scene } = setup();
425
+ const switched = [];
426
+ scene.multiplayer = { target: { mode: 'hub', handoff: { sessionId: 'cove-1' } }, net: null, switchSession: (sc, join, opts) => switched.push([join, opts ?? null]) };
427
+ scene.persistent.hubDepth = depth;
428
+ scene.persistent.hubTickets = tickets ?? [{ scene: 'scenes/main.scene', title: 'Town', door: 'cove', hub: true, join: { mode: 'public' } }];
429
+ HubState.hub.place = place;
430
+ answers.back = handoff();
431
+ system.lastNet = closedNet(error);
432
+ system.watchSession(scene, scene.multiplayer);
433
+ return { system, scene, switched };
434
+ };
435
+ const backs = () => calls.filter((c) => c[0] === 'back').length;
436
+
437
+ // A place that is not a game, reached through a door: Hub.back, no rejoin.
438
+ let r = run({});
439
+ await flush();
440
+ assert.equal(backs(), 1);
441
+ assert.deepEqual(r.switched, []);
442
+ assert.equal(r.system.toast.text, 'This place was updated. Back to Town…');
443
+ assert.equal(r.system.trip?.target.hubStep, -1, 'the way back is a hub trip');
444
+ assert.equal(HubState.hub.place, null, 'what the dead session drew is gone');
445
+ // While that way back is pending the close is not acted on again, even with
446
+ // the dead connection still there.
447
+ r.system.lastNet = closedNet();
448
+ r.system.watchSession(r.scene, r.scene.multiplayer);
449
+ r.system.watchSession(r.scene, r.scene.multiplayer);
450
+ await flush();
451
+ assert.equal(backs(), 1);
452
+ assert.deepEqual(r.switched, []);
453
+ // After a way back that was refused, the close is not seen again: the
454
+ // multiplayer kit holds no connection, and Back is left to the host's control.
455
+ r.system.trip = null;
456
+ r.system.watchSession(r.scene, r.scene.multiplayer);
457
+ await flush();
458
+ assert.equal(backs(), 1);
459
+
460
+ // No ticket in this runtime (the host's trail holds the trip): still Hub.back.
461
+ r = run({ tickets: [] });
462
+ await flush();
463
+ assert.equal(backs(), 1);
464
+ assert.equal(r.system.toast.text, 'This place was updated. Going back…');
465
+
466
+ // A game behind a door is left as it always was.
467
+ r = run({ place: { title: 'Foot Race', result: {} } });
468
+ await flush();
469
+ assert.equal(backs(), 1);
470
+ assert.equal(r.system.toast.text, 'The game ended');
471
+ assert.deepEqual(r.switched, []);
472
+
473
+ // Barred from the hub: nothing is asked.
474
+ r = run({ error: 'CAULDRON_HUB_BANNED' });
475
+ await flush();
476
+ assert.equal(backs(), 0);
477
+ assert.deepEqual(r.switched, []);
478
+ assert.equal(r.system.trip, null);
479
+ HubState.hub.place = null;
480
+ });
481
+
482
+ await test("a game's end waits out a pending join, settles once it resolves elsewhere, and retries after a failed one", async () => {
483
+ const { system, scene } = setup();
484
+ const backs = [];
485
+ system.back = () => backs.push('back');
486
+ let state = 'joined';
487
+ Object.assign(scene.multiplayer, { joinState: () => state, net: null, placed: () => false, target: { mode: 'named', key: 'game' } });
488
+ system.runCommands(scene, 0);
489
+ HubState.hub.commands.push({ k: 'hub-end', why: 'end' });
490
+ system.runCommands(scene, 0);
491
+ const due = system.pendingEnd + 1;
492
+ // The way back's join is pending toward the town: neither settled nor fired.
493
+ scene.multiplayer.target = { mode: 'named', key: 'town' };
494
+ state = 'joining';
495
+ system.stepEnd(scene, scene.multiplayer, due);
496
+ assert.equal(backs.length, 0);
497
+ assert.notEqual(system.pendingEnd, null);
498
+ // It resolves (the camera still settling): settled, never fired.
499
+ state = 'joined';
500
+ system.stepEnd(scene, scene.multiplayer, due);
501
+ assert.equal(system.pendingEnd, null);
502
+ assert.equal(backs.length, 0);
503
+ // Ended again, and the way back's join fails: still in the game, so the
504
+ // end fires, and re-arms a pause later rather than every frame.
505
+ scene.multiplayer.target = { mode: 'named', key: 'game' };
506
+ HubState.hub.commands.push({ k: 'hub-end', why: 'end' });
507
+ system.runCommands(scene, 0);
508
+ state = 'failed';
509
+ system.stepEnd(scene, scene.multiplayer, system.pendingEnd + 1);
510
+ assert.equal(backs.length, 1);
511
+ system.stepEnd(scene, scene.multiplayer, system.pendingEnd - 1);
512
+ assert.equal(backs.length, 1);
513
+ assert.notEqual(system.pendingEnd, null);
514
+ });
515
+
516
+ await test('with no ticket in a place joined by a hub trip, Hub.back leads wherever ghost says', async () => {
517
+ const { system, scene } = setup();
518
+ scene.multiplayer.target = { mode: 'hub', handoff: handoff() };
519
+ scene.persistent.hubDepth = 1;
520
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
521
+ system.back(scene);
522
+ await flush();
523
+ assert.equal(system.contentReady(scene, system.trip, 0), true);
524
+ assert.deepEqual(calls, [['back'], ['loadPack', 'town', 'https://cdn/town.json']]);
525
+ });
526
+
527
+ // The multiplayer system as the trip sees it: switchSession and joinState.
528
+ function mpStub(scene, state = 'joining') {
529
+ const mp = {
530
+ state,
531
+ switched: [],
532
+ target: scene.multiplayer.target,
533
+ switchSession(_scene, join, opts = {}) {
534
+ mp.switched.push(join);
535
+ mp.held = Boolean(opts.hold);
536
+ mp.target = join;
537
+ scene.multiplayer.target = join;
538
+ },
539
+ joinState: () => mp.state,
540
+ release: () => {
541
+ mp.held = false;
542
+ },
543
+ placed: () => false,
544
+ };
545
+ return mp;
546
+ }
547
+
548
+ // Through the opening, to the moment the trip switches sessions.
549
+ async function toSwitch(system, scene, mp) {
550
+ await flush();
551
+ system.trip.startedAt -= 10000;
552
+ system.stepTrip(scene, mp, null, 0);
553
+ }
554
+
555
+ await test('nothing of the destination is mounted until the host commits the hub join', async () => {
556
+ const { system, scene } = setup();
557
+ const mp = mpStub(scene);
558
+ answers.go = handoff();
559
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
560
+ await toSwitch(system, scene, mp);
561
+ assert.equal(system.trip.phase, 'committing');
562
+ assert.equal(mp.switched[0].mode, 'hub');
563
+ assert.equal(scene.requestedScene, undefined);
564
+ assert.equal(scene.requestedContent, undefined);
565
+ system.stepTrip(scene, mp, null, 1);
566
+ assert.equal(system.trip.phase, 'committing');
567
+ mp.state = 'joined';
568
+ system.stepTrip(scene, mp, null, 2);
569
+ assert.equal(system.trip.phase, 'loading');
570
+ assert.equal(scene.requestedScene, 'scenes/race.scene');
571
+ assert.equal(scene.requestedContent.sha256, 'abc');
572
+ // The host's trail now holds a trip: the kit's Back steps aside.
573
+ assert.equal(scene.persistent.hubDepth, 1);
574
+ });
575
+
576
+ await test('a refused door from an ordinary session rejoins that session, nothing mounted', async () => {
577
+ const { system, scene } = setup();
578
+ const mp = mpStub(scene);
579
+ answers.go = handoff();
580
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
581
+ await toSwitch(system, scene, mp);
582
+ mp.state = 'failed';
583
+ system.stepTrip(scene, mp, null, 1);
584
+ assert.equal(scene.persistent.hubTickets.length, 0);
585
+ assert.deepEqual(system.trip.target.join, { mode: 'public' });
586
+ assert.equal(system.trip.target.scene, 'scenes/main.scene');
587
+ assert.equal(system.trip.target.content, undefined);
588
+ assert.equal(scene.requestedContent, undefined);
589
+ assert.equal(scene.persistent.hubDepth ?? 0, 0);
590
+ });
591
+
592
+ await test('a refused door from a place reached by a hub trip leaves by that place\'s way back', async () => {
593
+ const { system, scene } = setup();
594
+ const mp = mpStub(scene);
595
+ scene.persistent.hubTickets.push({ hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'arena', title: 'Town' });
596
+ scene.multiplayer.target = { mode: 'hub', handoff: handoff({ nonce: 'spent' }) };
597
+ answers.go = handoff();
598
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
599
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
600
+ await toSwitch(system, scene, mp);
601
+ mp.state = 'failed';
602
+ system.stepTrip(scene, mp, null, 1);
603
+ await flush();
604
+ // The spent handoff is never rejoined; Hub.back runs from where the player is.
605
+ assert.deepEqual(calls.map((c) => c[0]), ['go', 'loadPack', 'back', 'loadPack']);
606
+ assert.equal(system.trip.target.ticket.door, 'arena');
607
+ assert.equal(scene.persistent.hubTickets.length, 0);
608
+ });
609
+
610
+ await test('a way back the host does not commit keeps its ticket, and is tried once more', async () => {
611
+ const { system, scene } = setup();
612
+ const mp = mpStub(scene);
613
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
614
+ scene.persistent.hubTickets.push(ticket);
615
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
616
+ system.back(scene);
617
+ await toSwitch(system, scene, mp);
618
+ mp.state = 'failed';
619
+ system.stepTrip(scene, mp, null, 1);
620
+ assert.equal(system.trip.target.attempt, 1);
621
+ await toSwitch(system, scene, mp);
622
+ mp.state = 'failed';
623
+ system.stepTrip(scene, mp, null, 2);
624
+ assert.equal(system.trip, null);
625
+ assert.deepEqual(scene.persistent.hubTickets, [ticket]);
626
+ assert.equal(calls.filter((c) => c[0] === 'back').length, 2);
627
+ });
628
+
629
+ await test("a way back reuses held content only when it is the handoff's release", async () => {
630
+ const { system, scene } = setup();
631
+ const held = { sha256: 'town-v1', url: 'https://cdn/town-v1.json', sessionKeyPrefix: '' };
632
+ scene.persistent.hubTickets.push({
633
+ hub: true,
634
+ scene: 'scenes/plaza.scene',
635
+ content: held,
636
+ join: { mode: 'public' },
637
+ door: 'race',
638
+ title: 'Town',
639
+ });
640
+ answers.back = handoff({ bundleHash: 'town-v1', contentUrl: 'https://cdn/town-v1.json' });
641
+ system.back(scene);
642
+ await flush();
643
+ assert.equal(system.contentReady(scene, system.trip, 0), true);
644
+ // Out of the pack cache by hash; the ticket's own place, not the pack's main.
645
+ assert.deepEqual(calls, [['back'], ['loadPack', 'town-v1', 'https://cdn/town-v1.json']]);
646
+ assert.equal(system.trip.target.content.sha256, 'town-v1');
647
+ assert.equal(system.trip.target.content.id, 'pack-town-v1');
648
+ assert.equal(system.trip.target.scene, 'scenes/plaza.scene');
649
+
650
+ const again = setup();
651
+ again.scene.persistent.hubTickets.push({
652
+ hub: true,
653
+ scene: 'scenes/main.scene',
654
+ content: held,
655
+ join: { mode: 'public' },
656
+ door: 'race',
657
+ title: 'Town',
658
+ });
659
+ answers.back = handoff({ bundleHash: 'town-v2', contentUrl: 'https://cdn/town-v2.json' });
660
+ again.system.back(again.scene);
661
+ await flush();
662
+ assert.equal(again.system.contentReady(again.scene, again.system.trip, 0), true);
663
+ assert.deepEqual(calls, [['back'], ['loadPack', 'town-v2', 'https://cdn/town-v2.json']]);
664
+ assert.equal(again.system.trip.target.content.sha256, 'town-v2');
665
+ });
666
+
667
+ // scene.persistent rides in every move report, which the multiplayer kit
668
+ // caps at 16 KB: a ticket names its pack, never holds it. The dev serve never
669
+ // shows this (a dev town is its own deck: scene.content is null).
670
+ await test('a ticket names the mounted pack and never carries it', async () => {
671
+ const { system, scene } = setup();
672
+ const big = 'x'.repeat(60000);
673
+ scene.content = {
674
+ id: 'town',
675
+ sha256: 'f'.repeat(64),
676
+ url: `https://hosted.castle.games/content-packs/${'f'.repeat(64)}.json`,
677
+ files: { 'scenes/main.scene': big },
678
+ sprites: {},
679
+ ownMedia: [],
680
+ places: { main: { scene: 'scenes/main.scene' } },
681
+ sessionKeyPrefix: '',
682
+ };
683
+ scene.multiplayer.target = {
684
+ mode: 'hub',
685
+ handoff: handoff({
686
+ url: `wss://${'g'.repeat(80)}`,
687
+ nonce: 'n'.repeat(64),
688
+ sessionId: 's'.repeat(36),
689
+ bundleHash: 'f'.repeat(64),
690
+ contentUrl: scene.content.url,
691
+ }),
692
+ };
693
+ answers.go = handoff();
694
+ system.goHub(scene, { k: 'hub-go', door: 'summit', key: null, title: 'Summit', party: 1 });
695
+ system.trip = null;
696
+ system.go(scene, {
697
+ k: 'hub-go',
698
+ door: 'race',
699
+ key: 'hub.race.abc',
700
+ place: 'race',
701
+ scene: 'scenes/race.scene',
702
+ title: 'Foot Race',
703
+ party: 1,
704
+ });
705
+ const tickets = scene.persistent.hubTickets;
706
+ assert.equal(tickets.length, 2);
707
+ for (const ticket of tickets) {
708
+ assert.deepEqual(ticket.content, {
709
+ sha256: 'f'.repeat(64),
710
+ url: scene.content.url,
711
+ sessionKeyPrefix: '',
712
+ });
713
+ const bytes = JSON.stringify(ticket).length;
714
+ assert.ok(bytes < 1024, `a ticket is ${bytes} bytes`);
715
+ }
716
+ // A full stack of tickets and everything else the kit keeps there still
717
+ // leaves the move report most of its 16 KB.
718
+ for (let i = 0; i < 6; i += 1) {
719
+ system.trip = null;
720
+ system.goHub(scene, {
721
+ k: 'hub-go',
722
+ door: `door-${i}`,
723
+ key: null,
724
+ title: 'A door with a forty-character long title',
725
+ party: 1,
726
+ });
727
+ }
728
+ scene.persistent.hubResult = {
729
+ outcome: 'win',
730
+ score: 1,
731
+ stats: Object.fromEntries([...Array(8)].map((_, i) => [`stat${i}`, 'y'.repeat(40)])),
732
+ at: Date.now(),
733
+ };
734
+ const persistent = JSON.stringify(scene.persistent).length;
735
+ assert.ok(persistent < 6000, `scene.persistent is ${persistent} bytes`);
736
+ await flush();
737
+ });
738
+
739
+ await test("a dev way back to another deck's place loads its pack again by reference", async () => {
740
+ const { system, scene } = setup();
741
+ const ref = { sha256: 'race', url: 'https://cdn/race.json', sessionKeyPrefix: 'race.' };
742
+ const ticket = {
743
+ scene: 'scenes/lobby.scene',
744
+ content: ref,
745
+ join: { mode: 'named', key: 'race.hub.lobby.x' },
746
+ door: 'arena',
747
+ title: 'Lobby',
748
+ };
749
+ scene.persistent.hubTickets.push(ticket);
750
+ system.back(scene);
751
+ await flush();
752
+ assert.equal(system.contentReady(scene, system.trip, 0), true);
753
+ assert.deepEqual(calls, [['loadPack', 'race', 'https://cdn/race.json']]);
754
+ assert.equal(system.trip.target.content.sha256, 'race');
755
+ assert.equal(system.trip.target.content.sessionKeyPrefix, 'race.');
756
+ assert.equal(system.trip.target.scene, 'scenes/lobby.scene');
757
+ assert.deepEqual(system.trip.target.join, ticket.join);
758
+
759
+ // A pack that no longer loads keeps the ticket: it is still the way back.
760
+ const again = setup();
761
+ again.scene.persistent.hubTickets.push(ticket);
762
+ answers.loadPackError = 'HTTP 404';
763
+ again.system.back(again.scene);
764
+ await flush();
765
+ assert.equal(again.system.contentReady(again.scene, again.system.trip, 0), false);
766
+ assert.deepEqual(again.scene.persistent.hubTickets, [ticket]);
767
+ });
768
+
769
+ await test('a loaded acquisition the engine cannot mount is given back', async () => {
770
+ const { system, scene } = setup();
771
+ scene.mountsContent = false;
772
+ answers.go = handoff();
773
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
774
+ await flush();
775
+ assert.equal(system.contentReady(scene, system.trip, 0), false);
776
+ await flush();
777
+ assert.deepEqual(calls.at(-1), ['cancel']);
778
+ });
779
+
780
+ await test('a late cancel runs before the next acquisition, never after it', async () => {
781
+ const { system, scene } = setup();
782
+ let release;
783
+ const go = globalThis.__tripHub.go;
784
+ globalThis.__tripHub.go = (doorId) => {
785
+ calls.push(['go', doorId]);
786
+ if (!release) {
787
+ return new Promise((resolve) => (release = resolve));
788
+ }
789
+ return Promise.resolve(handoff());
790
+ };
791
+ try {
792
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
793
+ await flush();
794
+ system.trip.startedAt = -1e9;
795
+ assert.equal(system.contentReady(scene, system.trip, 0), false);
796
+ system.trip = null;
797
+ system.goHub(scene, { door: 'arena', key: null, title: 'Hill' });
798
+ await flush();
799
+ release(handoff());
800
+ for (let i = 0; i < 5; i += 1) {
801
+ await flush();
802
+ }
803
+ assert.deepEqual(calls.map((c) => c.slice(0, 2)), [['go', 'race'], ['loadPack', 'abc'], ['cancel'], ['go', 'arena'], ['loadPack', 'abc']]);
804
+ } finally {
805
+ globalThis.__tripHub.go = go;
806
+ }
807
+ });
808
+
809
+ await test('returning home through the host clears its trail, and the kit Back comes back', async () => {
810
+ const { system, scene } = setup();
811
+ const mp = mpStub(scene);
812
+ scene.persistent.hubDepth = 1;
813
+ scene.persistent.hubTickets.push({ hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' });
814
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
815
+ system.back(scene);
816
+ await toSwitch(system, scene, mp);
817
+ mp.state = 'joined';
818
+ system.stepTrip(scene, mp, null, 1);
819
+ assert.equal(scene.persistent.hubDepth, 0);
820
+ });
821
+
822
+ await test('a hub join is held until the destination scene is in, then released', async () => {
823
+ const { system, scene } = setup();
824
+ const mp = mpStub(scene);
825
+ answers.go = handoff();
826
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
827
+ await toSwitch(system, scene, mp);
828
+ assert.equal(mp.held, true);
829
+ mp.state = 'joined';
830
+ system.stepTrip(scene, mp, null, 1);
831
+ assert.equal(mp.held, true);
832
+ // The engine applies the swap.
833
+ scene.content = scene.requestedContent;
834
+ scene.currentScenePath = scene.requestedScene;
835
+ scene.requestedScene = null;
836
+ system.stepTrip(scene, mp, null, 2);
837
+ assert.equal(mp.held, false);
838
+ });
839
+
840
+ // The engine applies a requested scene swap.
841
+ function applySwap(scene) {
842
+ scene.content = scene.requestedContent === undefined ? scene.content : scene.requestedContent;
843
+ scene.currentScenePath = scene.requestedScene;
844
+ scene.requestedScene = null;
845
+ }
846
+
847
+ await test('a way home whose session closes before placing the player rejoins at once, not timed out', async () => {
848
+ const { system, scene } = setup();
849
+ const mp = mpStub(scene);
850
+ globalThis.__tripMatchesOwn = () => true;
851
+ try {
852
+ scene.persistent.hubDepth = 1;
853
+ scene.persistent.hubRootJoin = { mode: 'public' };
854
+ scene.persistent.hubTickets.push({ hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town', party: 1 });
855
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
856
+ system.back(scene);
857
+ await toSwitch(system, scene, mp);
858
+ mp.state = 'joined';
859
+ system.stepTrip(scene, mp, null, 100);
860
+ assert.equal(system.trip.phase, 'loading');
861
+ assert.equal(scene.persistent.hubDepth, 0);
862
+ // A full layer closes the socket after the hello: committed, never placed.
863
+ mp.state = 'lost';
864
+ system.stepTrip(scene, mp, null, 200);
865
+ const trip = system.trip;
866
+ assert.equal(trip.phase, 'loading');
867
+ assert.deepEqual(mp.switched.at(-1), { mode: 'public' });
868
+ assert.equal(mp.held, true, 'held until the town scene is in');
869
+ assert.equal(trip.target.lost, 1);
870
+ assert.equal(trip.loadingFrom, 200);
871
+ // Ghost spent the return ticket on the commit: no second Hub.back.
872
+ assert.equal(calls.filter((c) => c[0] === 'back').length, 1);
873
+ // The public layer places the player well inside the loading limit.
874
+ mp.state = 'joined';
875
+ applySwap(scene);
876
+ system.stepTrip(scene, mp, null, 300);
877
+ assert.equal(mp.held, false);
878
+ mp.placed = () => true;
879
+ HubState.hub.arrivedFacing = 0;
880
+ system.stepTrip(scene, mp, null, 400);
881
+ assert.equal(trip.phase, 'revealing');
882
+ assert.equal(trip.timedOut, false);
883
+ system.stepTrip(scene, mp, null, 400 + 5000);
884
+ assert.equal(system.trip, null);
885
+ const record = globalThis.__hubTrips.at(-1);
886
+ assert.equal(record.timedOut, false);
887
+ assert.equal(record.lostJoins, 1);
888
+ assert.equal(record.lost, null);
889
+ } finally {
890
+ delete globalThis.__tripMatchesOwn;
891
+ HubState.clear();
892
+ }
893
+ });
894
+
895
+ await test('a root rejoin after a lost join that is itself refused for good uncovers at once, not timed out', async () => {
896
+ const { system, scene } = setup();
897
+ const mp = mpStub(scene);
898
+ globalThis.__tripMatchesOwn = () => true;
899
+ try {
900
+ scene.persistent.hubDepth = 1;
901
+ scene.persistent.hubRootJoin = { mode: 'public' };
902
+ scene.persistent.hubTickets.push({ hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' });
903
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
904
+ system.back(scene);
905
+ await toSwitch(system, scene, mp);
906
+ mp.state = 'joined';
907
+ system.stepTrip(scene, mp, null, 100);
908
+ mp.state = 'lost';
909
+ system.stepTrip(scene, mp, null, 200);
910
+ assert.deepEqual(mp.switched.at(-1), { mode: 'public' });
911
+ // The public rejoin is refused as barred: multiplayer says 'failed'.
912
+ mp.state = 'failed';
913
+ mp.joinError = 'CAULDRON_HUB_BANNED';
914
+ system.stepTrip(scene, mp, null, 300);
915
+ const trip = system.trip;
916
+ assert.equal(trip.phase, 'revealing');
917
+ assert.equal(trip.timedOut, false);
918
+ assert.match(trip.lost, /CAULDRON_HUB_BANNED/);
919
+ assert.equal(mp.switched.length, 2, 'not joined again');
920
+ } finally {
921
+ delete globalThis.__tripMatchesOwn;
922
+ HubState.clear();
923
+ }
924
+ });
925
+
926
+ await test('a door whose session closes before placing the player leaves by the way back at once', async () => {
927
+ const { system, scene } = setup();
928
+ const mp = mpStub(scene);
929
+ answers.go = handoff();
930
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
931
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race', party: 2 });
932
+ await toSwitch(system, scene, mp);
933
+ mp.state = 'joined';
934
+ system.stepTrip(scene, mp, null, 100);
935
+ assert.equal(scene.persistent.hubDepth, 1);
936
+ mp.state = 'lost';
937
+ system.stepTrip(scene, mp, null, 200);
938
+ await flush();
939
+ // A new trip: Hub.back from the game, its ticket the door's, cover down.
940
+ const trip = system.trip;
941
+ assert.equal(trip.target.hubStep, -1);
942
+ assert.equal(trip.target.lost, 1);
943
+ assert.equal(trip.target.ticket.door, 'race');
944
+ assert.equal(trip.target.arrive.party, 2);
945
+ assert.equal(trip.timedOut, undefined);
946
+ assert.deepEqual(calls.map((c) => c[0]), ['go', 'loadPack', 'back', 'loadPack']);
947
+ });
948
+
949
+ await test('a way that keeps losing its join gives up after two recoveries and uncovers at once', async () => {
950
+ const { system, scene } = setup();
951
+ const mp = mpStub(scene);
952
+ scene.persistent.hubDepth = 2;
953
+ const ticket = (door) => ({ hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door, title: 'Town' });
954
+ scene.persistent.hubTickets.push(ticket('a'), ticket('b'), ticket('c'));
955
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
956
+ const lose = async (now) => {
957
+ await toSwitch(system, scene, mp);
958
+ mp.state = 'joined';
959
+ system.stepTrip(scene, mp, null, now);
960
+ mp.state = 'lost';
961
+ system.stepTrip(scene, mp, null, now + 1);
962
+ };
963
+ system.back(scene);
964
+ await lose(100);
965
+ assert.equal(system.trip.target.lost, 1);
966
+ // hubDepth stays above the root here, so each recovery is another Hub.back.
967
+ scene.persistent.hubDepth = 2;
968
+ await lose(200);
969
+ assert.equal(system.trip.target.lost, 2);
970
+ scene.persistent.hubDepth = 2;
971
+ await lose(300);
972
+ // Out of recoveries: revealed now, not after the loading limit, not timed out.
973
+ assert.equal(system.trip.phase, 'revealing');
974
+ assert.equal(system.trip.timedOut, false);
975
+ assert.match(system.trip.lost, /closed before placing/);
976
+ assert.equal(calls.filter((c) => c[0] === 'back').length, 3);
977
+ });
978
+
979
+ await test('a return hello carries the party that went out together', async () => {
980
+ const { system, scene } = setup();
981
+ answers.go = handoff();
982
+ system.goHub(scene, { k: 'hub-go', door: 'race', pad: 'race.1', key: null, title: 'Foot Race', party: 3 });
983
+ assert.equal(scene.persistent.hubTickets[0].party, 3);
984
+ assert.equal(scene.persistent.hubArriving.party, 3);
985
+ await flush();
986
+ system.trip = null;
987
+ system.back(scene);
988
+ assert.deepEqual(scene.persistent.hubArriving, { door: 'race', party: 3, returning: true });
989
+ // What the hello says on the next connection.
990
+ const sent = [];
991
+ system.sayHello(scene, { net: { send: (msg) => sent.push(msg) } });
992
+ assert.equal(sent[0].k, 'hub-hello');
993
+ assert.equal(sent[0].party, 3);
994
+ await flush();
995
+
996
+ // A dev keyed door's ticket too; a ticket from before party is one.
997
+ const dev = setup();
998
+ dev.system.go(dev.scene, { k: 'hub-go', door: 'race', key: 'hub.race.abc', place: 'race', scene: 'scenes/race.scene', title: 'Foot Race', party: 2 });
999
+ dev.system.trip = null;
1000
+ dev.system.back(dev.scene);
1001
+ assert.equal(dev.scene.persistent.hubArriving.party, 2);
1002
+ const old = setup();
1003
+ old.scene.persistent.hubTickets.push({ hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' });
1004
+ answers.back = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
1005
+ old.system.back(old.scene);
1006
+ assert.equal(old.scene.persistent.hubArriving.party, 1);
1007
+ await flush();
1008
+ });
1009
+
1010
+ await test('a failed door at the root of the trail rejoins the join the runtime began with', async () => {
1011
+ const { system, scene } = setup();
1012
+ const mp = mpStub(scene);
1013
+ // A -> B -> A: home again, joined by a spent hub handoff.
1014
+ scene.persistent.hubRootJoin = { mode: 'public' };
1015
+ scene.persistent.hubDepth = 0;
1016
+ scene.multiplayer.target = { mode: 'hub', handoff: handoff({ nonce: 'spent' }) };
1017
+ answers.go = handoff();
1018
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
1019
+ await toSwitch(system, scene, mp);
1020
+ mp.state = 'failed';
1021
+ system.stepTrip(scene, mp, null, 1);
1022
+ assert.deepEqual(system.trip.target.join, { mode: 'public' });
1023
+ assert.deepEqual(calls.map((c) => c[0]), ['go', 'loadPack']);
1024
+ });
1025
+
1026
+ await test('Back at the root of the trail is not Hub.back', async () => {
1027
+ const { system, scene } = setup();
1028
+ scene.multiplayer.target = { mode: 'hub', handoff: handoff({ nonce: 'spent' }) };
1029
+ scene.persistent.hubDepth = 0;
1030
+ system.back(scene);
1031
+ assert.equal(system.trip.target.join.mode, 'named');
1032
+ assert.deepEqual(calls, []);
1033
+ });
1034
+
1035
+ await test('the root join is remembered at the first hub trip only', async () => {
1036
+ const { system, scene } = setup();
1037
+ scene.multiplayer.target = { mode: 'named', key: 'hub.main.x' };
1038
+ answers.go = handoff();
1039
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
1040
+ assert.deepEqual(scene.persistent.hubRootJoin, { mode: 'named', key: 'hub.main.x' });
1041
+ });
1042
+
1043
+ await test('the dev serve keyed door is the named join it was', async () => {
1044
+ const { system, scene } = setup();
1045
+ system.go(scene, { k: 'hub-go', door: 'race', key: 'hub.race.abc', place: 'race', scene: 'scenes/race.scene', title: 'Foot Race', party: 1 });
1046
+ assert.equal(system.contentReady(scene, system.trip, 0), true);
1047
+ assert.deepEqual(system.trip.target.join, { mode: 'named', key: 'hub.race.abc' });
1048
+ assert.equal(scene.persistent.hubTickets[0].hub, undefined);
1049
+ assert.deepEqual(calls, []);
1050
+ // A dev back is the remembered join, not Hub.back.
1051
+ system.trip = null;
1052
+ system.back(scene);
1053
+ assert.deepEqual(system.trip.target.join, { mode: 'public' });
1054
+ assert.deepEqual(calls, []);
1055
+ });
1056
+
1057
+ await test('Join on a friend is Hub.jump: a hub join into this place, no ticket, no step on the trail', async () => {
1058
+ const { system, scene } = setup();
1059
+ const mp = mpStub(scene);
1060
+ scene.persistent.hubDepth = 1;
1061
+ scene.persistent.hubTickets.push({ hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' });
1062
+ answers.jump = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
1063
+ system.jump(scene, { userId: '9', username: 'amy' });
1064
+ assert.equal(scene.persistent.hubTickets.length, 1);
1065
+ await toSwitch(system, scene, mp);
1066
+ // This runtime's own content is no release ghost names: drawn from the pack.
1067
+ assert.deepEqual(calls, [['jump', '9'], ['loadPack', 'town', 'https://cdn/town.json']]);
1068
+ assert.equal(system.trip.phase, 'committing');
1069
+ assert.deepEqual(mp.switched[0], { mode: 'hub', handoff: answers.jump });
1070
+ assert.equal(system.trip.target.title, 'Joining amy');
1071
+ assert.equal(system.trip.target.arrive.door, null);
1072
+ // The place is told whom this player came to stand beside.
1073
+ assert.equal(system.trip.target.arrive.near, '9');
1074
+ assert.equal(scene.persistent.hubArriving.near, '9');
1075
+ mp.state = 'joined';
1076
+ system.stepTrip(scene, mp, null, 1);
1077
+ assert.equal(scene.persistent.hubDepth, 1);
1078
+ assert.equal(scene.persistent.hubTickets.length, 1);
1079
+ assert.equal(scene.requestedContent.sha256, 'town');
1080
+ });
1081
+
1082
+ await test('a jump within a place drawn from the release the handoff names keeps its scene', async () => {
1083
+ const { system, scene } = setup();
1084
+ system.scenePath = 'scenes/plaza.scene';
1085
+ scene.content = { id: 'pack-town-v1', sha256: 'town-v1', url: 'https://cdn/town-v1.json', sessionKeyPrefix: '' };
1086
+ answers.jump = handoff({ bundleHash: 'town-v1', contentUrl: 'https://cdn/town-v1.json' });
1087
+ system.jump(scene, { userId: '9', username: null });
1088
+ await flush();
1089
+ assert.equal(system.contentReady(scene, system.trip, 0), true);
1090
+ assert.equal(system.trip.target.scene, 'scenes/plaza.scene');
1091
+ assert.equal(system.trip.target.content.sha256, 'town-v1');
1092
+ assert.equal(system.trip.target.title, 'Joining a friend');
1093
+ });
1094
+
1095
+ await test('a jump the host refuses stays put and touches no ticket', async () => {
1096
+ const { system, scene } = setup();
1097
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
1098
+ scene.persistent.hubTickets.push(ticket);
1099
+ answers.jump = { status: 'not_found' };
1100
+ system.jump(scene, { userId: '9', username: 'amy' });
1101
+ await flush();
1102
+ assert.equal(system.contentReady(scene, system.trip, 0), false);
1103
+ assert.equal(system.trip.shut, 'not_found');
1104
+ assert.equal(system.toast.text, 'Could not join amy');
1105
+ assert.deepEqual(scene.persistent.hubTickets, [ticket]);
1106
+ assert.deepEqual(calls, [['jump', '9']]);
1107
+ });
1108
+
1109
+ await test('a jump the host does not commit rejoins the session it left', async () => {
1110
+ const { system, scene } = setup();
1111
+ const mp = mpStub(scene);
1112
+ answers.jump = handoff({ bundleHash: 'town', contentUrl: 'https://cdn/town.json' });
1113
+ system.jump(scene, { userId: '9', username: 'amy' });
1114
+ await toSwitch(system, scene, mp);
1115
+ mp.state = 'failed';
1116
+ system.stepTrip(scene, mp, null, 1);
1117
+ assert.deepEqual(system.trip.target.join, { mode: 'public' });
1118
+ assert.equal(system.trip.target.scene, 'scenes/main.scene');
1119
+ assert.equal(scene.persistent.hubTickets.length, 0);
1120
+ assert.ok(!calls.some((c) => c[0] === 'back'));
1121
+ });
1122
+
1123
+ await test('friends: asked from a place that is not a game; here and empty rows dropped; a host that cannot say stops it', async () => {
1124
+ const { system, scene } = setup();
1125
+ const mp = mpStub(scene, 'joined');
1126
+ HubState.hub.place = { id: 'main', title: 'Town' };
1127
+ try {
1128
+ answers.friends = { status: 'ok', friends: [{ userId: '9', username: 'amy', here: false }, { userId: '10', username: 'bo', here: true }] };
1129
+ system.askFriends(scene, mp, 0);
1130
+ await flush();
1131
+ assert.deepEqual(system.friends, [{ userId: '9', username: 'amy', here: false }]);
1132
+ // Not again until a while later.
1133
+ system.askFriends(scene, mp, 1000);
1134
+ await flush();
1135
+ assert.equal(calls.filter((c) => c[0] === 'friends').length, 1);
1136
+ answers.friends = { status: 'unavailable', friends: null };
1137
+ system.askFriends(scene, mp, 60000);
1138
+ await flush();
1139
+ assert.equal(system.friends, null);
1140
+ system.askFriends(scene, mp, 120000);
1141
+ await flush();
1142
+ assert.equal(calls.filter((c) => c[0] === 'friends').length, 2);
1143
+
1144
+ // A failed ask (a timeout, the network) is asked again, not turned off.
1145
+ const flaky = setup();
1146
+ HubState.hub.place = { id: 'main', title: 'Town' };
1147
+ answers.friendsError = 'TIMEOUT';
1148
+ flaky.system.askFriends(flaky.scene, mp, 0);
1149
+ await flush();
1150
+ assert.ok(!flaky.system.friendsOff);
1151
+ answers.friendsError = null;
1152
+ answers.friends = { status: 'ok', friends: [{ userId: '9', username: 'amy', here: false }] };
1153
+ flaky.system.askFriends(flaky.scene, mp, 60000);
1154
+ await flush();
1155
+ assert.deepEqual(flaky.system.friends, [{ userId: '9', username: 'amy', here: false }]);
1156
+
1157
+ const older = setup();
1158
+ HubState.hub.place = { id: 'main', title: 'Town' };
1159
+ answers.friendsError = 'UNKNOWN_COMMAND';
1160
+ older.system.askFriends(older.scene, mp, 0);
1161
+ await flush();
1162
+ assert.equal(older.system.friendsOff, true);
1163
+
1164
+ const game = setup();
1165
+ HubState.hub.place = { id: 'main', title: 'Race', result: { outcomes: ['win'] } };
1166
+ game.system.askFriends(game.scene, mp, 0);
1167
+ await flush();
1168
+ assert.deepEqual(calls, []);
1169
+ } finally {
1170
+ HubState.hub.place = null;
1171
+ }
1172
+ });
1173
+
1174
+ await test('a door refused because the player is barred from the hub says so, and is not asked again', async () => {
1175
+ const { system, scene } = setup();
1176
+ answers.goError = 'CAULDRON_HUB_BANNED';
1177
+ system.goHub(scene, { door: 'race', key: null, title: 'Foot Race' });
1178
+ await flush();
1179
+ const trip = system.trip;
1180
+ assert.equal(system.contentReady(scene, trip, 0), false);
1181
+ assert.ok(trip.shut);
1182
+ assert.equal(system.toast.text, BANNED_TEXT);
1183
+ assert.equal(scene.persistent.hubTickets.length, 0);
1184
+ await sleep(1600);
1185
+ await flush();
1186
+ assert.deepEqual(calls.filter(([op]) => op !== 'back'), [['go', 'race']], 'no retry, nothing to give back');
1187
+ });
1188
+
1189
+ await test('a way back refused because the player is barred is not asked again', async () => {
1190
+ const { system, scene } = setup();
1191
+ const ticket = { hub: true, scene: 'scenes/main.scene', content: null, join: { mode: 'public' }, door: 'race', title: 'Town' };
1192
+ scene.persistent.hubTickets.push(ticket);
1193
+ answers.backError = 'CAULDRON_HUB_BANNED';
1194
+ system.back(scene);
1195
+ await flush();
1196
+ const trip = system.trip;
1197
+ assert.equal(system.contentReady(scene, trip, 0), false);
1198
+ assert.ok(trip.shut);
1199
+ assert.equal(system.toast.text, BANNED_TEXT);
1200
+ await sleep(1600);
1201
+ await flush();
1202
+ assert.deepEqual(calls, [['back']], 'the unavailable retry does not fire');
1203
+ assert.equal(scene.persistent.hubTickets.length, 1, 'the way back is still the way back');
1204
+ });
1205
+
1206
+ await test("the town's own join refused as barred shows the message", async () => {
1207
+ const { system, scene } = setup();
1208
+ let toast = null;
1209
+ system.chrome = {
1210
+ showBack() {}, showPicker() {}, setFriends() {}, setDoors() {}, setPeople() {}, setHud() {}, setLabels() {}, setRibbon() {},
1211
+ setToast(text) { toast = text; },
1212
+ };
1213
+ system.talk = { talking: () => false };
1214
+ scene.multiplayer.joinError = 'CAULDRON_HUB_BANNED';
1215
+ globalThis.window ??= { innerWidth: 400, innerHeight: 800 };
1216
+ system.drawChrome(scene, null, 1000);
1217
+ assert.equal(toast, BANNED_TEXT);
1218
+ scene.multiplayer.joinError = null;
1219
+ system.toast = { text: '', until: 0 };
1220
+ system.drawChrome(scene, null, 1000);
1221
+ assert.equal(toast, '');
1222
+ });
1223
+
1224
+ await test('Report goes to Hub.report for that user and thanks the player', async () => {
1225
+ const { system } = setup();
1226
+ answers.report = { status: 'ok' };
1227
+ system.report({ playerId: 'p2', userId: '102', username: 'bo' });
1228
+ await flush();
1229
+ assert.deepEqual(calls, [['report', '102', undefined]]);
1230
+ assert.equal(system.toast.text, 'Reported. Thanks.');
1231
+ answers.report = { status: 'unavailable' };
1232
+ system.report({ playerId: 'p2', userId: '102', username: 'bo' });
1233
+ await flush();
1234
+ assert.equal(system.reportOff, true, 'a host that takes no reports hides Report');
1235
+ });
1236
+
1237
+ await test('People: a game lists who may be reported, only where the host runs the hub; a town lists everyone, friends first with where they are and Join', async () => {
1238
+ const { system, scene } = setup();
1239
+ const sent = [];
1240
+ scene.multiplayer.net = {
1241
+ others: () => new Map([['p2', 'bo'], ['p3', 'guest'], ['p4', 'amy']]),
1242
+ userId: (id) => ({ p2: '102', p4: '104' })[id] ?? null,
1243
+ send: (msg) => sent.push(msg),
1244
+ };
1245
+ assert.equal(system.people(scene, true), null, 'a game on no hub host: no People');
1246
+ system.hubHost = true;
1247
+ assert.deepEqual(system.people(scene, true).map((row) => row.playerId), ['p2', 'p4'], 'a game: the signed-in players, to report');
1248
+ assert.ok(system.people(scene, true).every((row) => row.where === null && row.actions.map((a) => a.id).join() === 'report'));
1249
+
1250
+ // A town: amy is a friend here, cy a friend in another layer, dee inside a game.
1251
+ HubState.hub.place = { title: 'Town', result: null };
1252
+ HubState.hub.doors = [{ id: 'race', title: 'Foot Race' }];
1253
+ const friends = [
1254
+ { userId: '104', username: 'amy', here: true },
1255
+ { userId: '9', username: 'cy', here: false },
1256
+ { userId: '11', username: 'dee', here: false, door: 'race' },
1257
+ { userId: '12', username: 'eve', here: false, door: 'gone' },
1258
+ ];
1259
+ system.together.frame(scene.multiplayer, friends, true, 5000);
1260
+ assert.deepEqual(sent, [{ k: 'hub-where', ids: ['p4'] }], 'asks where the friend here is');
1261
+ system.together.frame(scene.multiplayer, friends, true, 5500);
1262
+ assert.equal(sent.length, 1, 'at most once a second');
1263
+ HubState.hub.whereAt = { p4: { x: 3, z: 4, where: 'on the Foot Race pad', door: 'race', on: true } };
1264
+ system.friendsAll = friends;
1265
+ const ids = (row) => row.actions.map((a) => a.id).join();
1266
+ let rows = system.people(scene, false);
1267
+ assert.deepEqual(
1268
+ rows.map((row) => [row.username, row.mark, row.where, ids(row)]),
1269
+ [
1270
+ ['amy', '★ ', 'on the Foot Race pad', 'goto,invite,report'],
1271
+ ['cy', '★ ', 'in another part of town', 'jump'],
1272
+ ['dee', '★ ', 'inside Foot Race', 'door'],
1273
+ ['bo', '', 'in town', 'invite,find,report'],
1274
+ ['guest', '', 'in town', 'invite,find'],
1275
+ ],
1276
+ );
1277
+
1278
+ // Anyone standing at a door can be gone to, as Doors > Go would.
1279
+ HubState.hub.whereAt.p2 = { x: 1, z: 1, where: 'on the Foot Race pad', door: 'race', on: true };
1280
+ assert.deepEqual(ids(system.people(scene, false).find((row) => row.playerId === 'p2')), 'door,invite,find,report');
1281
+ delete HubState.hub.whereAt.p2;
1282
+
1283
+ // Join: beside a friend here, to the door of a friend's game. Invite asks.
1284
+ sent.length = 0;
1285
+ system.join(scene, rows[0], 'goto');
1286
+ system.jumpSentAt = -Infinity;
1287
+ system.join(scene, rows[2], 'door');
1288
+ assert.equal(system.toast.text, 'dee is inside Foot Race');
1289
+ system.join(scene, rows[3], 'invite');
1290
+ assert.deepEqual(sent, [{ k: 'hub-goto', player: 'p4' }, { k: 'hub-door-jump', door: 'race' }, { k: 'hub-party', op: 'invite', player: 'p2' }]);
1291
+
1292
+ // Find puts the pointer on anyone here: they are asked about too, and Find
1293
+ // again takes it off. With the list open the other rows are asked about.
1294
+ system.join(scene, rows[3], 'find');
1295
+ sent.length = 0;
1296
+ system.together.frame(scene.multiplayer, friends, true, 7000);
1297
+ assert.deepEqual(sent, [{ k: 'hub-where', ids: ['p2', 'p4'] }]);
1298
+ assert.equal(system.people(scene, false).find((row) => row.playerId === 'p2').actions[1].text, 'Stop');
1299
+ system.join(scene, rows[3], 'find');
1300
+ sent.length = 0;
1301
+ system.together.frame(scene.multiplayer, friends, true, 9000, true);
1302
+ assert.deepEqual(sent, [{ k: 'hub-where', ids: ['p4', 'p2', 'p3'] }]);
1303
+
1304
+ // In a party with bo: bo is first, Join goes to them, and Doors > Go asks the party.
1305
+ HubState.hub.party = { members: ['p2'], away: 1, invite: null, proposal: null };
1306
+ system.together.frame(scene.multiplayer, friends, true, 11000);
1307
+ rows = system.people(scene, false);
1308
+ assert.deepEqual([rows[0].username, rows[0].mark, ids(rows[0])], ['bo', '● ', 'goto,report']);
1309
+ assert.equal(system.together.partyLine(), 'Your party · 2 here · 1 in a game');
1310
+ sent.length = 0;
1311
+ system.goDoor(scene, { id: 'race', title: 'Foot Race' });
1312
+ assert.deepEqual(sent, [{ k: 'hub-party', op: 'go', door: 'race' }]);
1313
+ HubState.hub.party = { members: null, away: 0, invite: { from: 'p2', username: 'bo' }, proposal: null };
1314
+ system.jumpSentAt = -Infinity;
1315
+ system.goDoor(scene, { id: 'race', title: 'Foot Race' });
1316
+ assert.deepEqual(sent[1], { k: 'hub-door-jump', door: 'race' }, 'alone: a jump to the door');
1317
+ assert.equal(system.ask().text, 'bo invited you to their party');
1318
+ system.answer(scene, system.ask(), true);
1319
+ assert.deepEqual(sent[2], { k: 'hub-party', op: 'accept', from: 'p2' });
1320
+ HubState.hub.party = { members: ['p2'], away: 0, invite: null, proposal: { id: 7, door: 'race', by: 'p2', username: 'bo', asked: true } };
1321
+ assert.equal(system.ask().text, "bo: let's play Foot Race");
1322
+ system.answer(scene, system.ask(), false);
1323
+ assert.deepEqual(sent[3], { k: 'hub-party', op: 'go-no', id: 7 });
1324
+ // "Let's play Foot Race" from someone here is offered as a Go to that door.
1325
+ HubState.hub.party = { members: null, away: 0, invite: null, proposal: null };
1326
+ system.playAsk = { from: 'p2', name: 'bo', door: 'race', until: performance.now() + 20000 };
1327
+ assert.equal(system.ask().text, "bo: let's play Foot Race");
1328
+ system.jumpSentAt = -Infinity;
1329
+ system.answer(scene, system.ask(), true);
1330
+ assert.deepEqual(sent.at(-1), { k: 'hub-door-jump', door: 'race' });
1331
+ assert.equal(system.ask(), null);
1332
+ HubState.hub.party = { members: null, away: 0, invite: null, proposal: null };
1333
+ HubState.hub.place = null;
1334
+ HubState.hub.doors = [];
1335
+ HubState.hub.whereAt = {};
1336
+ });
1337
+
1338
+ await test('a sent chat line says whom it reached', async () => {
1339
+ const { system } = setup();
1340
+ let status = 'sent';
1341
+ system.chat.send = (text, done) => done(status);
1342
+ system.chatReach = ['bo', 'amy'];
1343
+ system.saying({ text: 'hello' });
1344
+ assert.equal(system.toast.text, 'Sent to bo, amy');
1345
+ system.chatReach = ['a', 'b', 'c', 'd', 'e'];
1346
+ system.saying({ text: 'hello' });
1347
+ assert.equal(system.toast.text, 'Sent to a, b, c +2');
1348
+ status = 'none';
1349
+ system.saying({ text: 'hello' });
1350
+ assert.equal(system.toast.text, 'Nobody here can read that. Follow each other to chat.');
1351
+ });
1352
+
1353
+ await test('the invite button beside someone: steady at the edge of range, kept for a friend, says it was sent', async () => {
1354
+ const together = makeTogether();
1355
+ const at = { p2: { x: 2, z: 0 }, p4: { x: 30, z: 0 } };
1356
+ const scene = {
1357
+ multiplayer: {
1358
+ net: { others: () => new Map([['p2', 'bo'], ['p4', 'amy']]), userId: (id) => ({ p2: '102', p4: '104' })[id] ?? null, send() {} },
1359
+ avatars: { actors: new Map([['p2', 'a2'], ['p4', 'a4']]) },
1360
+ },
1361
+ getActor: (id) => ({ components: { Transform: at[id === 'a2' ? 'p2' : 'p4'] } }),
1362
+ };
1363
+ HubState.hub.place = { title: 'Town', result: null };
1364
+ HubState.hub.party = { members: null, away: 0, invite: null, proposal: null };
1365
+ const local = { components: { Transform: { x: 0, y: 1, z: 0 } } };
1366
+ const friends = [{ userId: '104', username: 'amy', here: true }];
1367
+ together.frame(scene.multiplayer, friends, true, 0);
1368
+ const who = (now) => together.beside(scene, local, now)?.playerId ?? null;
1369
+ assert.equal(who(1000), null, 'not at once');
1370
+ assert.equal(who(2600), 'p2', 'after standing by them a moment');
1371
+ // A step back past the near distance and in again: the button does not blink.
1372
+ at.p2.x = 2.6;
1373
+ assert.equal(who(2700), 'p2', 'still about');
1374
+ at.p2.x = 2.1;
1375
+ assert.equal(who(2800), 'p2');
1376
+ assert.equal(who(11000), null, 'not a friend: the offer ends');
1377
+ // Walked right off and back: offered again after a moment.
1378
+ at.p2.x = 9;
1379
+ assert.equal(who(11100), null);
1380
+ at.p2.x = 2;
1381
+ assert.equal(who(11200), null);
1382
+ assert.equal(who(12800), 'p2');
1383
+ // A press: the button says so, then leaves that player alone for a while.
1384
+ together.invited('p2', 12900);
1385
+ assert.deepEqual(together.beside(scene, local, 13000), { d: 2, playerId: 'p2', username: 'bo', sent: true });
1386
+ assert.equal(who(18000), null, 'not offered again straight away');
1387
+ assert.equal(who(44000), null, 'the old offer has run out by then');
1388
+ // A friend's stays for as long as they stand there.
1389
+ at.p2.x = 30;
1390
+ at.p4.x = 1.5;
1391
+ assert.equal(who(50000), null);
1392
+ assert.equal(who(52000), 'p4');
1393
+ assert.equal(who(90000), 'p4', 'a friend beside the player is always offered');
1394
+ // Someone whose offer ran out is not in the way of the next one along.
1395
+ at.p4.x = 30;
1396
+ at.p2.x = 3;
1397
+ assert.equal(who(100000), null);
1398
+ at.p2.x = 2;
1399
+ assert.equal(who(100100), null);
1400
+ assert.equal(who(101700), 'p2');
1401
+ assert.equal(who(111000), null, "bo's offer ends");
1402
+ at.p2.x = 3;
1403
+ at.p4.x = 1;
1404
+ assert.equal(who(111100), null);
1405
+ assert.equal(who(112700), 'p4', 'amy, though bo still stands near');
1406
+ together.invited('p4', 112800);
1407
+ assert.equal(together.beside(scene, local, 113000).sent, true);
1408
+ at.p2.x = 9;
1409
+ assert.equal(who(117000), null);
1410
+ at.p2.x = 2;
1411
+ assert.equal(who(117100), null);
1412
+ assert.equal(who(118700), 'p2', 'bo walked off and back: offered, though amy was just invited');
1413
+ at.p2.x = 30;
1414
+ at.p4.x = 1.5;
1415
+ assert.equal(who(150000), null);
1416
+ assert.equal(who(152000), 'p4');
1417
+ // In the party: nothing to offer.
1418
+ HubState.hub.party = { members: ['p4'], away: 0, invite: null, proposal: null };
1419
+ assert.equal(who(153000), null);
1420
+ HubState.hub.party = { members: null, away: 0, invite: null, proposal: null };
1421
+ HubState.hub.place = null;
1422
+ });
1423
+
1424
+ await test('start next to a friend: once, on walking in, when a friend is here and the player has not walked off', async () => {
1425
+ const { system, scene } = setup();
1426
+ const sent = [];
1427
+ scene.multiplayer.net = {
1428
+ others: () => new Map([['p2', 'bo'], ['p4', 'amy']]),
1429
+ userId: (id) => ({ p2: '102', p4: '104' })[id] ?? null,
1430
+ send: (msg) => sent.push(msg),
1431
+ };
1432
+ HubState.hub.place = { title: 'Town', result: null };
1433
+ HubState.hub.arrivedFacing = 0;
1434
+ const local = { components: { Transform: { x: 0, y: 1, z: 9 } } };
1435
+ const friends = [{ userId: '104', username: 'amy', here: true }];
1436
+ system.startNear = { until: 12000, at: null, asked: false };
1437
+ system.together.frame(scene.multiplayer, null, true, 1000);
1438
+ system.startBeside(scene, local, true, 1000);
1439
+ assert.deepEqual(sent, [], 'no friend known yet: nothing');
1440
+ system.together.frame(scene.multiplayer, friends, true, 3000);
1441
+ system.startBeside(scene, local, true, 3000);
1442
+ system.startBeside(scene, local, true, 3100);
1443
+ assert.deepEqual(sent.filter((m) => m.k === 'hub-goto'), [{ k: 'hub-goto', player: 'p4' }], 'asked once');
1444
+ // A jump beside someone else is not this request's answer.
1445
+ HubState.hub.jumpedTo = 'p2';
1446
+ system.startBeside(scene, local, true, 3200);
1447
+ assert.notEqual(system.toast.text, "You're next to bo");
1448
+ // The place moved the player: they are told whom they are beside.
1449
+ local.components.Transform.x = 30;
1450
+ HubState.hub.jumpedTo = 'p4';
1451
+ system.startBeside(scene, local, true, 3300);
1452
+ assert.equal(system.toast.text, "You're next to amy");
1453
+ assert.equal(system.startNear, null);
1454
+
1455
+ // A player who walked off before a friend turned up is left where they went.
1456
+ sent.length = 0;
1457
+ system.startNear = { until: 12000, at: null, asked: false };
1458
+ system.together.frame(scene.multiplayer, null, true, 5000);
1459
+ local.components.Transform.x = 0;
1460
+ system.startBeside(scene, local, true, 5000);
1461
+ local.components.Transform.x = 8;
1462
+ system.together.frame(scene.multiplayer, friends, true, 7000);
1463
+ system.startBeside(scene, local, true, 7000);
1464
+ assert.deepEqual(sent.filter((m) => m.k === 'hub-goto'), []);
1465
+
1466
+ // A friend who comes in later is announced; one here all along is not, however
1467
+ // late the host says who the friends are.
1468
+ const roster = new Map([['p2', 'bo'], ['p4', 'amy']]);
1469
+ scene.multiplayer.net.others = () => roster;
1470
+ system.together.clear();
1471
+ system.together.frame(scene.multiplayer, null, true, 20000);
1472
+ system.together.frame(scene.multiplayer, friends, true, 45000);
1473
+ assert.deepEqual(system.together.arrivals(), [], 'amy was here before the friends answer');
1474
+ roster.set('p9', 'zed');
1475
+ scene.multiplayer.net.userId = (id) => ({ p2: '102', p4: '104', p9: '109' })[id] ?? null;
1476
+ system.together.frame(scene.multiplayer, [...friends, { userId: '109', username: 'zed', here: true }], true, 47000);
1477
+ assert.deepEqual(system.together.arrivals(), [{ playerId: 'p9', username: 'zed' }]);
1478
+ assert.deepEqual(system.together.arrivals(), []);
1479
+
1480
+ // A friend who goes through a door is named to those left behind, with Follow.
1481
+ HubState.hub.doors = [{ id: 'race', title: 'Foot Race', late: true }];
1482
+ system.together.frame(scene.multiplayer, [...friends, { userId: '109', username: 'zed', here: true }], true, 48000);
1483
+ HubState.hub.wentEvents.push({ k: 'hub-went', players: ['p9'], door: 'race' }, { k: 'hub-went', players: ['p2'], door: 'race' });
1484
+ for (const went of HubState.hub.wentEvents.splice(0)) {
1485
+ const name = went.players.map((id) => system.together.lately(id)).find(Boolean);
1486
+ if (name) {
1487
+ system.playAsk = { from: went.players[0], name, door: went.door, until: performance.now() + 45000, went: true };
1488
+ }
1489
+ }
1490
+ assert.deepEqual([system.ask().text, system.ask().yes], ['zed went into Foot Race', 'Follow'], 'bo is no friend: only zed is named');
1491
+ // A door that takes no late joiners: told where they are, with no Follow.
1492
+ HubState.hub.doors = [{ id: 'race', title: 'Foot Race' }];
1493
+ assert.deepEqual([system.ask().text, system.ask().yes, system.ask().no], ['zed went into Foot Race · back soon', 'OK', 'Go to the door']);
1494
+ sent.length = 0;
1495
+ system.jumpSentAt = -Infinity;
1496
+ system.answer(scene, system.ask(), true);
1497
+ assert.deepEqual(sent, [], 'OK stays put');
1498
+ system.playAsk = null;
1499
+ HubState.hub.doors = [];
1500
+
1501
+ // A jump the player asks for ends a pending start, and one sent on the
1502
+ // heels of another waits its turn rather than being dropped by the place.
1503
+ system.startNear = { until: 99999, at: null, asked: false };
1504
+ system.jumpSentAt = performance.now();
1505
+ sent.length = 0;
1506
+ system.goDoor(scene, { id: 'race', title: 'Foot Race' });
1507
+ assert.equal(system.startNear, null);
1508
+ assert.deepEqual(sent, [], 'held back inside the place\'s one-a-second limit');
1509
+ await sleep(1250);
1510
+ assert.deepEqual(sent, [{ k: 'hub-door-jump', door: 'race' }]);
1511
+ HubState.hub.place = null;
1512
+ HubState.hub.arrivedFacing = null;
1513
+ HubState.hub.jumpedTo = null;
1514
+ });
1515
+
1516
+ console.warn = warn;
1517
+ console.log = log;
1518
+ console.log(`hub trips: ${n} tests passed`);