| 1 | import { createServer } from 'node:http'; |
| 2 | import { afterAll, beforeAll, expect, test } from 'vitest'; |
| 3 | import { joinRoom } from '../src/net/room.ts'; |
| 4 | import { attachRooms, pickPublicUrl } from './rooms.ts'; |
| 5 | |
| 6 | // The real client against the real relay, in one process. Plain .mjs on |
| 7 | // purpose: the client is typed for the browser and the relay for node, and |
| 8 | // neither tsconfig wants to swallow the other — vitest transforms both. |
| 9 | |
| 10 | let server; |
| 11 | let url; |
| 12 | const opened = []; |
| 13 | |
| 14 | beforeAll(async () => { |
| 15 | server = createServer(); |
| 16 | attachRooms(server); |
| 17 | await new Promise((r) => server.listen(0, '127.0.0.1', r)); |
| 18 | url = `ws://127.0.0.1:${server.address().port}/ws`; |
| 19 | }); |
| 20 | afterAll(async () => { |
| 21 | for (const r of opened) r.leave(); |
| 22 | await new Promise((r) => server.close(r)); |
| 23 | }); |
| 24 | |
| 25 | const join = async (roomCode) => { |
| 26 | const r = await joinRoom({ room: roomCode, server: url }); |
| 27 | opened.push(r); |
| 28 | return r; |
| 29 | }; |
| 30 | const next = (sub) => new Promise((r) => { |
| 31 | const un = sub((...args) => { |
| 32 | un(); |
| 33 | r(args); |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | test('a full little room: join, state, events, migration', async () => { |
| 38 | const a = await join(); |
| 39 | expect(a.code).toMatch(/^[A-Z2-9]{6}$/); |
| 40 | expect(a.isHost).toBe(true); |
| 41 | expect(a.me.name).toBe('Guest 1'); |
| 42 | |
| 43 | const joinSeen = next((cb) => a.onJoin(cb)); |
| 44 | const b = await join(a.code); |
| 45 | expect(b.code).toBe(a.code); |
| 46 | expect(b.isHost).toBe(false); |
| 47 | expect(b.players.length).toBe(2); |
| 48 | expect((await joinSeen)[0].id).toBe(b.me.id); |
| 49 | |
| 50 | // Host writes, mirror reads. |
| 51 | const stateSeen = next((cb) => b.onState(cb)); |
| 52 | a.setState({ mode: 'party', game: { turn: 2 } }); |
| 53 | await stateSeen; |
| 54 | expect(b.state.mode).toBe('party'); |
| 55 | expect(b.state.game).toEqual({ turn: 2 }); |
| 56 | |
| 57 | // Only the host writes. |
| 58 | expect(() => b.setState({ evil: true })).toThrow(); |
| 59 | |
| 60 | // Events go the other way, stamped with the sender. |
| 61 | const actSeen = next((cb) => a.on('act', (payload, from) => cb(payload, from))); |
| 62 | b.send('act', { seat: 1 }); |
| 63 | const [payload, from] = await actSeen; |
| 64 | expect(payload).toEqual({ seat: 1 }); |
| 65 | expect(from).toBe(b.me.id); |
| 66 | |
| 67 | // The host leaves; the room passes to the next in line, state intact. |
| 68 | const leaveSeen = next((cb) => b.onLeave(cb)); |
| 69 | const hostSeen = next((cb) => b.onHostChange(cb)); |
| 70 | a.leave(); |
| 71 | expect((await leaveSeen)[0].id).toBe(a.me.id); |
| 72 | await hostSeen; |
| 73 | expect(b.isHost).toBe(true); |
| 74 | expect(b.state.mode).toBe('party'); |
| 75 | b.setState({ mode: 'party', n: 1 }); // and the crown is real |
| 76 | }); |
| 77 | |
| 78 | test('a late joiner walks in on the state as it stands', async () => { |
| 79 | const a = await join(); |
| 80 | a.setState({ mode: 'online', seats: [1, 2, 3, 4] }); |
| 81 | // The relay applies the (microtask-coalesced) write before answering a |
| 82 | // later hello, both being one client's messages in order — but ours is a |
| 83 | // second connection, so give the flush a beat. |
| 84 | await new Promise((r) => setTimeout(r, 50)); |
| 85 | const b = await join(a.code); |
| 86 | expect(b.state.mode).toBe('online'); |
| 87 | expect(b.state.seats).toEqual([1, 2, 3, 4]); |
| 88 | }); |
| 89 | |
| 90 | test('an unknown code becomes a fresh room, not an error', async () => { |
| 91 | const a = await join('REBORN'); |
| 92 | expect(a.code).toBe('REBORN'); |
| 93 | expect(a.isHost).toBe(true); |
| 94 | expect(a.state).toEqual({}); |
| 95 | }); |
| 96 | |
| 97 | test('pickPublicUrl takes only an https tunnel aimed at our port', () => { |
| 98 | const tunnels = { |
| 99 | tunnels: [ |
| 100 | { public_url: 'https://other.ngrok-free.dev', config: { addr: 'http://127.0.0.1:4061' } }, |
| 101 | { public_url: 'http://ours.ngrok-free.dev', config: { addr: 'http://127.0.0.1:4510' } }, |
| 102 | { public_url: 'https://ours.ngrok-free.dev', config: { addr: 'http://127.0.0.1:4510' } }, |
| 103 | ], |
| 104 | }; |
| 105 | expect(pickPublicUrl(tunnels, 4510)).toBe('https://ours.ngrok-free.dev'); |
| 106 | expect(pickPublicUrl(tunnels, 8080)).toBe(''); // nobody tunnels us — no link |
| 107 | expect(pickPublicUrl(tunnels, null)).toBe(''); |
| 108 | expect(pickPublicUrl({}, 4510)).toBe(''); |
| 109 | expect(pickPublicUrl('nonsense', 4510)).toBe(''); |
| 110 | }); |
| 111 | |
| 112 | test('the join carries the public url of a tunnel aimed at us', async () => { |
| 113 | // A fake ngrok agent: one tunnel for somebody else, one for us. |
| 114 | let relayPort = 0; |
| 115 | const api = createServer((_req, res) => { |
| 116 | res.setHeader('content-type', 'application/json'); |
| 117 | res.end( |
| 118 | JSON.stringify({ |
| 119 | tunnels: [ |
| 120 | { public_url: 'https://other.ngrok-free.dev', config: { addr: 'http://127.0.0.1:59999' } }, |
| 121 | { public_url: 'https://ours.ngrok-free.dev', config: { addr: `http://127.0.0.1:${relayPort}` } }, |
| 122 | ], |
| 123 | }), |
| 124 | ); |
| 125 | }); |
| 126 | await new Promise((r) => api.listen(0, '127.0.0.1', r)); |
| 127 | const relay = createServer(); |
| 128 | attachRooms(relay); |
| 129 | await new Promise((r) => relay.listen(0, '127.0.0.1', r)); |
| 130 | relayPort = relay.address().port; |
| 131 | process.env.NGROK_API = `http://127.0.0.1:${api.address().port}/api/tunnels`; |
| 132 | try { |
| 133 | const r = await joinRoom({ server: `ws://127.0.0.1:${relayPort}/ws` }); |
| 134 | expect(r.publicBase).toBe('https://ours.ngrok-free.dev'); |
| 135 | r.leave(); |
| 136 | } finally { |
| 137 | delete process.env.NGROK_API; |
| 138 | await new Promise((r) => relay.close(r)); |
| 139 | await new Promise((r) => api.close(r)); |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | test('PUBLIC_URL outranks any tunnel', async () => { |
| 144 | process.env.PUBLIC_URL = 'https://mahjong.example.com'; |
| 145 | try { |
| 146 | const a = await join(); |
| 147 | expect(a.publicBase).toBe('https://mahjong.example.com'); |
| 148 | } finally { |
| 149 | delete process.env.PUBLIC_URL; |
| 150 | } |
| 151 | }); |