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 { 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();
|
||||||
|
if (awareness?.type === "awareness") {
|
||||||
expect(awareness.data.cursor).toEqual({ line: 10, col: 5 });
|
expect(awareness.data.cursor).toEqual({ line: 10, col: 5 });
|
||||||
|
}
|
||||||
|
|
||||||
ws1.close();
|
ws1.close();
|
||||||
ws2.close();
|
ws2.close();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
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:";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,11 @@ export function saveUpdate(room: string, data: Uint8Array): void {
|
||||||
|
|
||||||
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(
|
||||||
|
"SELECT data FROM updates WHERE room = ? ORDER BY id ASC",
|
||||||
|
);
|
||||||
const rows = stmt.all(room) as Array<{ data: Uint8Array }>;
|
const rows = stmt.all(room) as Array<{ data: Uint8Array }>;
|
||||||
return rows.map(row => row.data);
|
return rows.map((row) => row.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function close(): void {
|
export function close(): void {
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue