Skip to content

gitflarePublicPartly agent-ready40

neon-drift

Real-time multiplayer arcade racer on Cloudflare Workers and Durable Objects.

main
Add the Apache-2.0 licence3efe3da
JavaScript38 lines1.7 KB
import test from 'node:test';import assert from 'node:assert/strict';import { readFileSync } from 'node:fs';import { topScores, writeScores } from '../src/leaderboard.ts'; function fakeDb() {  const log = [];  const db = {    log,    prepare(sql) { return { sql, bind(...a) { return { sql, a, all: async () => ({ results: [] }) }; } }; },    async batch(stmts) { log.push(...stmts.map((s) => s.sql)); },  };  return db;}test('leaderboard creates schema once per db', async () => {  const db = fakeDb();  db.prepare = (sql) => ({ sql, bind: (...a) => ({ sql, a, all: async () => ({ results: [{ name: 'a', score: 1, created_at: 1 }] }) }) });  const r = await topScores(db, 'arena');  assert.equal(r.available, true);  assert.equal(r.entries.length, 1);  await topScores(db, 'arena');  assert.equal(db.log.filter((s) => s.startsWith('CREATE TABLE')).length, 1);  await writeScores(db, 'arena', [{ name: 'x', score: 2 }]);  assert.ok(db.log.some((s) => s.startsWith('INSERT')));});test('leaderboard degrades without DB or on error', async () => {  assert.deepEqual(await topScores(undefined, 'arena'), { entries: [], available: false });  const bad = { prepare() { throw new Error('x'); }, batch: async () => { throw new Error('x'); } };  assert.equal((await topScores(bad, 'arena')).available, false);});test('html has no inline scripts and CSP header exists', () => {  const html = readFileSync(new URL('../public/index.html', import.meta.url), 'utf8');  assert.ok(!/<script(?![^>]*src=)/.test(html));  assert.ok(!/\son\w+=/.test(html));  const h = readFileSync(new URL('../public/_headers', import.meta.url), 'utf8');  assert.match(h, /script-src 'self'/);});