Skip to content
1        10        20        30        40        50        60        70        80        901// Run against a live server: BASE=http://127.0.0.1:8080 bun test tests/multiplayer.test.js2import { test, expect } from 'bun:test';34const BASE = process.env.BASE ?? 'http://127.0.0.1:8080';5const look = { skin: 0, hairStyle: 0, hairColor: 0, shirt: 1, pants: 0, hat: 0 };67const post = (path, body) =>8  fetch(BASE + path, {9    method: 'POST',10    headers: { 'content-type': 'application/json' },11    body: JSON.stringify(body),12  });1314// Collect SSE frames for a while and return them grouped by event name.15async function listen(ms) {16  const controller = new AbortController();17  const frames = { state: [], log: [] };18  const res = await fetch(BASE + '/api/events', { signal: controller.signal });19  const reader = res.body.getReader();20  const decoder = new TextDecoder();21  let buffer = '';22  const timer = setTimeout(() => controller.abort(), ms);23  try {24    for (;;) {25      const { value, done } = await reader.read();26      if (done) break;27      buffer += decoder.decode(value, { stream: true });28      let idx;29      while ((idx = buffer.indexOf('\n\n')) >= 0) {30        const block = buffer.slice(0, idx);31        buffer = buffer.slice(idx + 2);32        const event = /^event: (\w+)/m.exec(block)?.[1];33        const data = /^data: (.*)$/m.exec(block)?.[1];34        if (event && data) frames[event].push(JSON.parse(data));35      }36    }37  } catch (e) {38    if (e.name !== 'AbortError') throw e;39  } finally {40    clearTimeout(timer);41  }42  return frames;43}4445test('two players join and share one state', async () => {46  const a = await (await post('/api/join', { name: 'Ana', look })).json();47  const b = await (await post('/api/join', { name: 'Ben', look })).json();4849  // Keep both alive while we listen.50  const heartbeat = setInterval(() => {51    post('/api/input', { id: a.id, dx: 0, dy: 0 });52    post('/api/input', { id: b.id, dx: 0, dy: 0 });53  }, 500);5455  const before = await listen(300);56  const snap = before.state.at(-1);57  const ids = snap.players.map((p) => p.id);58  expect(ids).toContain(a.id);59  expect(ids).toContain(b.id);60  expect(snap.guests.length).toBe(4);6162  // Ana walks left; the server moves her, and the change shows in the stream.63  const startX = snap.players.find((p) => p.id === a.id).x;64  await post('/api/input', { id: a.id, dx: -1, dy: 0 });65  const moving = await listen(600);66  await post('/api/input', { id: a.id, dx: 0, dy: 0 });67  const endX = moving.state.at(-1).players.find((p) => p.id === a.id).x;68  expect(endX).toBeLessThan(startX);6970  // Ben leaves while we are listening, so the log line arrives in the stream.71  const leaving = listen(600);72  await post('/api/leave', { id: b.id });73  const after = await leaving;74  clearInterval(heartbeat);75  expect(after.state.at(-1).players.map((p) => p.id)).not.toContain(b.id);76  expect(after.log.some((l) => l.text === 'Ben left the flat.')).toBe(true);77});7879test('unknown players get 404 and a full flat refuses', async () => {80  const res = await post('/api/input', { id: 'nope', dx: 1, dy: 0 });81  expect(res.status).toBe(404);82  const act = await post('/api/action', { id: 'nope' });83  expect(act.status).toBe(404);84});