Fix lint and type errors

This commit is contained in:
Jared Miller 2026-01-27 21:20:33 -05:00
parent 415e49327e
commit d904b9d2b6
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
5 changed files with 72 additions and 59 deletions

View file

@ -1,8 +1,9 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Server } from "bun"; import type { Server } from "bun";
import type { ServerMessage } from "./protocol";
describe("awareness routing", () => { describe("awareness routing", () => {
let server: Server; let server: Server<unknown>;
const PORT = 4042; const PORT = 4042;
beforeAll(async () => { beforeAll(async () => {
@ -20,7 +21,7 @@ describe("awareness routing", () => {
const ws1 = new WebSocket(`ws://localhost:${PORT}/ws`); const ws1 = new WebSocket(`ws://localhost:${PORT}/ws`);
const ws2 = new WebSocket(`ws://localhost:${PORT}/ws`); const ws2 = new WebSocket(`ws://localhost:${PORT}/ws`);
const received: unknown[] = []; const received: ServerMessage[] = [];
await Promise.all([ await Promise.all([
new Promise((r) => { new Promise((r) => {
@ -53,7 +54,9 @@ describe("awareness routing", () => {
const awareness = received.find((m) => m.type === "awareness"); const awareness = received.find((m) => m.type === "awareness");
expect(awareness).toBeDefined(); expect(awareness).toBeDefined();
expect(awareness.data.cursor).toEqual({ line: 10, col: 5 }); if (awareness?.type === "awareness") {
expect(awareness.data.cursor).toEqual({ line: 10, col: 5 });
}
ws1.close(); ws1.close();
ws2.close(); ws2.close();

View file

@ -1,52 +1,51 @@
import { describe, it, expect, beforeEach, afterEach } from "bun:test"; import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { initDb, saveUpdate, getUpdates, close } from "./db"; import { close, getUpdates, initDb, saveUpdate } from "./db";
import { unlinkSync } from "fs";
const TEST_DB = ":memory:"; const TEST_DB = ":memory:";
describe("db persistence", () => { describe("db persistence", () => {
beforeEach(() => { beforeEach(() => {
initDb(TEST_DB); initDb(TEST_DB);
}); });
afterEach(() => { afterEach(() => {
close(); close();
}); });
it("saves and retrieves updates for a room", () => { it("saves and retrieves updates for a room", () => {
const room = "test-room"; const room = "test-room";
const update1 = new Uint8Array([1, 2, 3]); const update1 = new Uint8Array([1, 2, 3]);
const update2 = new Uint8Array([4, 5, 6]); const update2 = new Uint8Array([4, 5, 6]);
saveUpdate(room, update1); saveUpdate(room, update1);
saveUpdate(room, update2); saveUpdate(room, update2);
const updates = getUpdates(room); const updates = getUpdates(room);
expect(updates).toHaveLength(2); expect(updates).toHaveLength(2);
expect(updates[0]).toEqual(update1); expect(updates[0]).toEqual(update1);
expect(updates[1]).toEqual(update2); expect(updates[1]).toEqual(update2);
}); });
it("returns empty array for unknown room", () => { it("returns empty array for unknown room", () => {
const updates = getUpdates("nonexistent"); const updates = getUpdates("nonexistent");
expect(updates).toEqual([]); expect(updates).toEqual([]);
}); });
it("rooms are isolated", () => { it("rooms are isolated", () => {
const room1 = "room-one"; const room1 = "room-one";
const room2 = "room-two"; const room2 = "room-two";
const update1 = new Uint8Array([1, 1, 1]); const update1 = new Uint8Array([1, 1, 1]);
const update2 = new Uint8Array([2, 2, 2]); const update2 = new Uint8Array([2, 2, 2]);
saveUpdate(room1, update1); saveUpdate(room1, update1);
saveUpdate(room2, update2); saveUpdate(room2, update2);
const room1Updates = getUpdates(room1); const room1Updates = getUpdates(room1);
const room2Updates = getUpdates(room2); const room2Updates = getUpdates(room2);
expect(room1Updates).toHaveLength(1); expect(room1Updates).toHaveLength(1);
expect(room2Updates).toHaveLength(1); expect(room2Updates).toHaveLength(1);
expect(room1Updates[0]).toEqual(update1); expect(room1Updates[0]).toEqual(update1);
expect(room2Updates[0]).toEqual(update2); expect(room2Updates[0]).toEqual(update2);
}); });
}); });

View file

@ -3,8 +3,8 @@ import { Database } from "bun:sqlite";
let db: Database | null = null; let db: Database | null = null;
export function initDb(path: string): void { export function initDb(path: string): void {
db = new Database(path); db = new Database(path);
db.exec(` db.exec(`
CREATE TABLE IF NOT EXISTS updates ( CREATE TABLE IF NOT EXISTS updates (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
room TEXT NOT NULL, room TEXT NOT NULL,
@ -16,21 +16,23 @@ export function initDb(path: string): void {
} }
export function saveUpdate(room: string, data: Uint8Array): void { export function saveUpdate(room: string, data: Uint8Array): void {
if (!db) throw new Error("Database not initialized"); if (!db) throw new Error("Database not initialized");
const stmt = db.prepare("INSERT INTO updates (room, data) VALUES (?, ?)"); const stmt = db.prepare("INSERT INTO updates (room, data) VALUES (?, ?)");
stmt.run(room, data); stmt.run(room, data);
} }
export function getUpdates(room: string): Uint8Array[] { export function getUpdates(room: string): Uint8Array[] {
if (!db) throw new Error("Database not initialized"); if (!db) throw new Error("Database not initialized");
const stmt = db.prepare("SELECT data FROM updates WHERE room = ? ORDER BY id ASC"); const stmt = db.prepare(
const rows = stmt.all(room) as Array<{ data: Uint8Array }>; "SELECT data FROM updates WHERE room = ? ORDER BY id ASC",
return rows.map(row => row.data); );
const rows = stmt.all(room) as Array<{ data: Uint8Array }>;
return rows.map((row) => row.data);
} }
export function close(): void { export function close(): void {
if (db) { if (db) {
db.close(); db.close();
db = null; db = null;
} }
} }

View file

@ -4,7 +4,7 @@ export type ClientMessage =
| { type: "join"; room: string } | { type: "join"; room: string }
| { type: "leave" } | { type: "leave" }
| { type: "update"; data: number[] } // yjs update as byte array | { type: "update"; data: number[] } // yjs update as byte array
| { type: "awareness"; data: number[] }; | { type: "awareness"; data: AwarenessState };
export type AwarenessState = { export type AwarenessState = {
clientId: number; clientId: number;

View file

@ -1,6 +1,12 @@
import { describe, expect, test } from "bun:test"; import { describe, expect, test } from "bun:test";
import * as Y from "yjs"; import * as Y from "yjs";
import { type Client, type WsData, getOrCreateSession, Session } from "./session"; import type { ServerMessage } from "./protocol";
import {
type Client,
getOrCreateSession,
Session,
type WsData,
} from "./session";
describe("Session", () => { describe("Session", () => {
test("creates yjs doc on init", () => { test("creates yjs doc on init", () => {
@ -61,8 +67,8 @@ describe("getOrCreateSession", () => {
describe("awareness", () => { describe("awareness", () => {
test("broadcasts awareness to other clients", () => { test("broadcasts awareness to other clients", () => {
const session = new Session("test-room"); const session = new Session("test-room");
const sent1: unknown[] = []; const sent1: ServerMessage[] = [];
const sent2: unknown[] = []; const sent2: ServerMessage[] = [];
const client1 = { const client1 = {
ws: { send: (m: string) => sent1.push(JSON.parse(m)) }, ws: { send: (m: string) => sent1.push(JSON.parse(m)) },
@ -81,6 +87,9 @@ describe("awareness", () => {
expect(sent1.filter((m) => m.type === "awareness")).toHaveLength(0); expect(sent1.filter((m) => m.type === "awareness")).toHaveLength(0);
// client2 should receive it // client2 should receive it
expect(sent2.filter((m) => m.type === "awareness")).toHaveLength(1); expect(sent2.filter((m) => m.type === "awareness")).toHaveLength(1);
expect(sent2.find((m) => m.type === "awareness")?.data).toEqual(awareness); const awarenessMsg = sent2.find((m) => m.type === "awareness");
if (awarenessMsg?.type === "awareness") {
expect(awarenessMsg.data).toEqual(awareness);
}
}); });
}); });