Fix lint and type errors
This commit is contained in:
parent
415e49327e
commit
d904b9d2b6
5 changed files with 72 additions and 59 deletions
|
|
@ -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<unknown>;
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
28
src/db.ts
28
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue