| 1 | import { writeFileSync, rmSync } from 'node:fs'; |
| 2 | import { createServer } from 'node:http'; |
| 3 | import { tmpdir } from 'node:os'; |
| 4 | import { join as joinPath } from 'node:path'; |
| 5 | import { afterAll, beforeAll, expect, test } from 'vitest'; |
| 6 | import { joinRoom } from '../src/net/room.ts'; |
| 7 | import { attachRooms } from './rooms.ts'; |
| 8 | |
| 9 | // The real client against the real relay, in one process. Plain .mjs on |
| 10 | // purpose: the client is typed for the browser and the relay for node, and |
| 11 | // neither tsconfig wants to swallow the other — vitest transforms both. |
| 12 | |
| 13 | // A fake rsgrok, so the relay's own tunnel-spawning runs for real without |
| 14 | // leaving the machine: prints the tunnel-up line and then holds, like the |
| 15 | // real one. Installed before any server exists — the relay spawns on listen. |
| 16 | const stub = joinPath(tmpdir(), `fake-rsgrok-${process.pid}.sh`); |
| 17 | writeFileSync( |
| 18 | stub, |
| 19 | '#!/bin/sh\necho "rsgrok: https://ours.vibe.richardscollin.com -> $2"\nexec sleep 60\n', |
| 20 | { mode: 0o755 }, |
| 21 | ); |
| 22 | process.env.RSGROK_BIN = stub; |
| 23 | |
| 24 | let server; |
| 25 | let url; |
| 26 | const opened = []; |
| 27 | |
| 28 | beforeAll(async () => { |
| 29 | server = createServer(); |
| 30 | attachRooms(server); |
| 31 | await new Promise((r) => server.listen(0, '127.0.0.1', r)); |
| 32 | url = `ws://127.0.0.1:${server.address().port}/ws`; |
| 33 | }); |
| 34 | afterAll(async () => { |
| 35 | for (const r of opened) r.leave(); |
| 36 | await new Promise((r) => server.close(r)); |
| 37 | delete process.env.RSGROK_BIN; |
| 38 | rmSync(stub, { force: true }); |
| 39 | }); |
| 40 | |
| 41 | const join = async (roomCode) => { |
| 42 | const r = await joinRoom({ room: roomCode, server: url }); |
| 43 | opened.push(r); |
| 44 | return r; |
| 45 | }; |
| 46 | const next = (sub) => new Promise((r) => { |
| 47 | const un = sub((...args) => { |
| 48 | un(); |
| 49 | r(args); |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | test('a full little room: join, state, events, migration', async () => { |
| 54 | const a = await join(); |
| 55 | expect(a.code).toMatch(/^[A-Z2-9]{6}$/); |
| 56 | expect(a.isHost).toBe(true); |
| 57 | expect(a.me.name).toBe('Guest 1'); |
| 58 | |
| 59 | const joinSeen = next((cb) => a.onJoin(cb)); |
| 60 | const b = await join(a.code); |
| 61 | expect(b.code).toBe(a.code); |
| 62 | expect(b.isHost).toBe(false); |
| 63 | expect(b.players.length).toBe(2); |
| 64 | expect((await joinSeen)[0].id).toBe(b.me.id); |
| 65 | |
| 66 | // Host writes, mirror reads. |
| 67 | const stateSeen = next((cb) => b.onState(cb)); |
| 68 | a.setState({ mode: 'party', game: { turn: 2 } }); |
| 69 | await stateSeen; |
| 70 | expect(b.state.mode).toBe('party'); |
| 71 | expect(b.state.game).toEqual({ turn: 2 }); |
| 72 | |
| 73 | // Only the host writes. |
| 74 | expect(() => b.setState({ evil: true })).toThrow(); |
| 75 | |
| 76 | // Events go the other way, stamped with the sender. |
| 77 | const actSeen = next((cb) => a.on('act', (payload, from) => cb(payload, from))); |
| 78 | b.send('act', { seat: 1 }); |
| 79 | const [payload, from] = await actSeen; |
| 80 | expect(payload).toEqual({ seat: 1 }); |
| 81 | expect(from).toBe(b.me.id); |
| 82 | |
| 83 | // The host leaves; the room passes to the next in line, state intact. |
| 84 | const leaveSeen = next((cb) => b.onLeave(cb)); |
| 85 | const hostSeen = next((cb) => b.onHostChange(cb)); |
| 86 | a.leave(); |
| 87 | expect((await leaveSeen)[0].id).toBe(a.me.id); |
| 88 | await hostSeen; |
| 89 | expect(b.isHost).toBe(true); |
| 90 | expect(b.state.mode).toBe('party'); |
| 91 | b.setState({ mode: 'party', n: 1 }); // and the crown is real |
| 92 | }); |
| 93 | |
| 94 | test('a late joiner walks in on the state as it stands', async () => { |
| 95 | const a = await join(); |
| 96 | a.setState({ mode: 'online', seats: [1, 2, 3, 4] }); |
| 97 | // The relay applies the (microtask-coalesced) write before answering a |
| 98 | // later hello, both being one client's messages in order — but ours is a |
| 99 | // second connection, so give the flush a beat. |
| 100 | await new Promise((r) => setTimeout(r, 50)); |
| 101 | const b = await join(a.code); |
| 102 | expect(b.state.mode).toBe('online'); |
| 103 | expect(b.state.seats).toEqual([1, 2, 3, 4]); |
| 104 | }); |
| 105 | |
| 106 | test('an unknown code becomes a fresh room, not an error', async () => { |
| 107 | const a = await join('REBORN'); |
| 108 | expect(a.code).toBe('REBORN'); |
| 109 | expect(a.isHost).toBe(true); |
| 110 | expect(a.state).toEqual({}); |
| 111 | }); |
| 112 | |
| 113 | test('the join carries the url of the tunnel the relay opened itself', async () => { |
| 114 | const r = await join(); |
| 115 | expect(r.publicBase).toBe('https://ours.vibe.richardscollin.com'); |
| 116 | }); |
| 117 | |
| 118 | test('bound to loopback, no rsgrok — a join still lands, and offers no link', async () => { |
| 119 | // A "binary" that dies at once: the relay must shrug, not stall the hello. |
| 120 | const dead = joinPath(tmpdir(), `dead-rsgrok-${process.pid}.sh`); |
| 121 | writeFileSync(dead, '#!/bin/sh\nexit 1\n', { mode: 0o755 }); |
| 122 | const was = process.env.RSGROK_BIN; |
| 123 | process.env.RSGROK_BIN = dead; |
| 124 | const relay = createServer(); |
| 125 | attachRooms(relay); |
| 126 | await new Promise((r) => relay.listen(0, '127.0.0.1', r)); |
| 127 | let joined = null; |
| 128 | try { |
| 129 | joined = await joinRoom({ server: `ws://127.0.0.1:${relay.address().port}/ws` }); |
| 130 | // Listening on loopback alone: this machine's wifi address would be a link |
| 131 | // nothing is answering on, so the relay offers none. |
| 132 | expect(joined.publicBase).toBe(''); |
| 133 | } finally { |
| 134 | // Left open, the relay's own close never resolves and the failure above |
| 135 | // shows up as a timeout instead of as itself. |
| 136 | joined?.leave(); |
| 137 | process.env.RSGROK_BIN = was; |
| 138 | rmSync(dead, { force: true }); |
| 139 | await new Promise((r) => relay.close(r)); |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | test('on every interface, no rsgrok — the join carries this machine on the wifi', async () => { |
| 144 | const dead = joinPath(tmpdir(), `dead-rsgrok2-${process.pid}.sh`); |
| 145 | writeFileSync(dead, '#!/bin/sh\nexit 1\n', { mode: 0o755 }); |
| 146 | const was = process.env.RSGROK_BIN; |
| 147 | process.env.RSGROK_BIN = dead; |
| 148 | const relay = createServer(); |
| 149 | attachRooms(relay); |
| 150 | await new Promise((r) => relay.listen(0, '0.0.0.0', r)); |
| 151 | const port = relay.address().port; |
| 152 | let joined = null; |
| 153 | try { |
| 154 | joined = await joinRoom({ server: `ws://127.0.0.1:${port}/ws` }); |
| 155 | // A machine with no network at all — a locked-down CI box — has nothing to |
| 156 | // offer and says so; anything else names itself and its port. |
| 157 | if (joined.publicBase !== '') { |
| 158 | expect(joined.publicBase).toMatch(new RegExp(`^http://\\d+\\.\\d+\\.\\d+\\.\\d+:${port}$`)); |
| 159 | } |
| 160 | } finally { |
| 161 | joined?.leave(); |
| 162 | process.env.RSGROK_BIN = was; |
| 163 | rmSync(dead, { force: true }); |
| 164 | await new Promise((r) => relay.close(r)); |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | test('PUBLIC_URL outranks any tunnel', async () => { |
| 169 | process.env.PUBLIC_URL = 'https://mahjong.example.com'; |
| 170 | try { |
| 171 | const a = await join(); |
| 172 | expect(a.publicBase).toBe('https://mahjong.example.com'); |
| 173 | } finally { |
| 174 | delete process.env.PUBLIC_URL; |
| 175 | } |
| 176 | }); |