From d904b9d2b67e5a31b06ed1729355d6ef3513eb07 Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Tue, 27 Jan 2026 21:20:33 -0500 Subject: [PATCH] Fix lint and type errors --- src/awareness.test.ts | 9 ++++-- src/db.test.ts | 75 +++++++++++++++++++++---------------------- src/db.ts | 28 ++++++++-------- src/protocol.ts | 2 +- src/session.test.ts | 17 +++++++--- 5 files changed, 72 insertions(+), 59 deletions(-) diff --git a/src/awareness.test.ts b/src/awareness.test.ts index 1e3615c..b51f016 100644 --- a/src/awareness.test.ts +++ b/src/awareness.test.ts @@ -1,8 +1,9 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test"; import type { Server } from "bun"; +import type { ServerMessage } from "./protocol"; describe("awareness routing", () => { - let server: Server; + let server: Server; const PORT = 4042; beforeAll(async () => { @@ -20,7 +21,7 @@ describe("awareness routing", () => { const ws1 = new WebSocket(`ws://localhost:${PORT}/ws`); const ws2 = new WebSocket(`ws://localhost:${PORT}/ws`); - const received: unknown[] = []; + const received: ServerMessage[] = []; await Promise.all([ new Promise((r) => { @@ -53,7 +54,9 @@ describe("awareness routing", () => { const awareness = received.find((m) => m.type === "awareness"); 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(); ws2.close(); diff --git a/src/db.test.ts b/src/db.test.ts index 9b5c33c..1e6a50d 100644 --- a/src/db.test.ts +++ b/src/db.test.ts @@ -1,52 +1,51 @@ -import { describe, it, expect, beforeEach, afterEach } from "bun:test"; -import { initDb, saveUpdate, getUpdates, close } from "./db"; -import { unlinkSync } from "fs"; +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; +import { close, getUpdates, initDb, saveUpdate } from "./db"; const TEST_DB = ":memory:"; describe("db persistence", () => { - beforeEach(() => { - initDb(TEST_DB); - }); + beforeEach(() => { + initDb(TEST_DB); + }); - afterEach(() => { - close(); - }); + afterEach(() => { + close(); + }); - it("saves and retrieves updates for a room", () => { - const room = "test-room"; - const update1 = new Uint8Array([1, 2, 3]); - const update2 = new Uint8Array([4, 5, 6]); + it("saves and retrieves updates for a room", () => { + const room = "test-room"; + const update1 = new Uint8Array([1, 2, 3]); + const update2 = new Uint8Array([4, 5, 6]); - saveUpdate(room, update1); - saveUpdate(room, update2); + saveUpdate(room, update1); + saveUpdate(room, update2); - const updates = getUpdates(room); - expect(updates).toHaveLength(2); - expect(updates[0]).toEqual(update1); - expect(updates[1]).toEqual(update2); - }); + const updates = getUpdates(room); + expect(updates).toHaveLength(2); + expect(updates[0]).toEqual(update1); + expect(updates[1]).toEqual(update2); + }); - it("returns empty array for unknown room", () => { - const updates = getUpdates("nonexistent"); - expect(updates).toEqual([]); - }); + it("returns empty array for unknown room", () => { + const updates = getUpdates("nonexistent"); + expect(updates).toEqual([]); + }); - it("rooms are isolated", () => { - const room1 = "room-one"; - const room2 = "room-two"; - const update1 = new Uint8Array([1, 1, 1]); - const update2 = new Uint8Array([2, 2, 2]); + it("rooms are isolated", () => { + const room1 = "room-one"; + const room2 = "room-two"; + const update1 = new Uint8Array([1, 1, 1]); + const update2 = new Uint8Array([2, 2, 2]); - saveUpdate(room1, update1); - saveUpdate(room2, update2); + saveUpdate(room1, update1); + saveUpdate(room2, update2); - const room1Updates = getUpdates(room1); - const room2Updates = getUpdates(room2); + const room1Updates = getUpdates(room1); + const room2Updates = getUpdates(room2); - expect(room1Updates).toHaveLength(1); - expect(room2Updates).toHaveLength(1); - expect(room1Updates[0]).toEqual(update1); - expect(room2Updates[0]).toEqual(update2); - }); + expect(room1Updates).toHaveLength(1); + expect(room2Updates).toHaveLength(1); + expect(room1Updates[0]).toEqual(update1); + expect(room2Updates[0]).toEqual(update2); + }); }); diff --git a/src/db.ts b/src/db.ts index 2104c7b..e04eee4 100644 --- a/src/db.ts +++ b/src/db.ts @@ -3,8 +3,8 @@ import { Database } from "bun:sqlite"; let db: Database | null = null; export function initDb(path: string): void { - db = new Database(path); - db.exec(` + db = new Database(path); + db.exec(` CREATE TABLE IF NOT EXISTS updates ( id INTEGER PRIMARY KEY AUTOINCREMENT, room TEXT NOT NULL, @@ -16,21 +16,23 @@ export function initDb(path: string): void { } export function saveUpdate(room: string, data: Uint8Array): void { - if (!db) throw new Error("Database not initialized"); - const stmt = db.prepare("INSERT INTO updates (room, data) VALUES (?, ?)"); - stmt.run(room, data); + if (!db) throw new Error("Database not initialized"); + const stmt = db.prepare("INSERT INTO updates (room, data) VALUES (?, ?)"); + stmt.run(room, data); } export function getUpdates(room: string): Uint8Array[] { - if (!db) throw new Error("Database not initialized"); - const stmt = db.prepare("SELECT data FROM updates WHERE room = ? ORDER BY id ASC"); - const rows = stmt.all(room) as Array<{ data: Uint8Array }>; - return rows.map(row => row.data); + if (!db) throw new Error("Database not initialized"); + const stmt = db.prepare( + "SELECT data FROM updates WHERE room = ? ORDER BY id ASC", + ); + const rows = stmt.all(room) as Array<{ data: Uint8Array }>; + return rows.map((row) => row.data); } export function close(): void { - if (db) { - db.close(); - db = null; - } + if (db) { + db.close(); + db = null; + } } diff --git a/src/protocol.ts b/src/protocol.ts index 8ce100f..2708c74 100644 --- a/src/protocol.ts +++ b/src/protocol.ts @@ -4,7 +4,7 @@ export type ClientMessage = | { type: "join"; room: string } | { type: "leave" } | { type: "update"; data: number[] } // yjs update as byte array - | { type: "awareness"; data: number[] }; + | { type: "awareness"; data: AwarenessState }; export type AwarenessState = { clientId: number; diff --git a/src/session.test.ts b/src/session.test.ts index f55e148..b57deac 100644 --- a/src/session.test.ts +++ b/src/session.test.ts @@ -1,6 +1,12 @@ import { describe, expect, test } from "bun:test"; 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", () => { test("creates yjs doc on init", () => { @@ -61,8 +67,8 @@ describe("getOrCreateSession", () => { describe("awareness", () => { test("broadcasts awareness to other clients", () => { const session = new Session("test-room"); - const sent1: unknown[] = []; - const sent2: unknown[] = []; + const sent1: ServerMessage[] = []; + const sent2: ServerMessage[] = []; const client1 = { ws: { send: (m: string) => sent1.push(JSON.parse(m)) }, @@ -81,6 +87,9 @@ describe("awareness", () => { expect(sent1.filter((m) => m.type === "awareness")).toHaveLength(0); // client2 should receive it 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); + } }); });