feat(KB-063): add realtime GitHub badge updates
- Add a focused GitHub badge poller with shared rate-limit, freshness, and cleanup handling - Wire a dedicated badge websocket server and routes to stream PR and issue badge snapshots - Add a shared useBadgeWebSocket hook and update TaskCard to subscribe only when visible while preserving partial badge state - Cover polling, websocket, and task card flows with tests and document the realtime badge channel
This commit is contained in:
272
packages/dashboard/src/__tests__/websocket.test.ts
Normal file
272
packages/dashboard/src/__tests__/websocket.test.ts
Normal file
@@ -0,0 +1,272 @@
|
||||
import { EventEmitter, once } from "node:events";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { WebSocket } from "ws";
|
||||
import type { Task } from "@kb/core";
|
||||
import { createServer } from "../server.js";
|
||||
import { githubPoller } from "../github-poll.js";
|
||||
import { WebSocketManager } from "../websocket.js";
|
||||
|
||||
class MockSocket extends EventEmitter {
|
||||
readyState: number = WebSocket.OPEN;
|
||||
sent: string[] = [];
|
||||
ping = vi.fn();
|
||||
terminate = vi.fn(() => {
|
||||
this.readyState = WebSocket.CLOSED;
|
||||
});
|
||||
send = vi.fn((payload: string) => {
|
||||
this.sent.push(payload);
|
||||
});
|
||||
close = vi.fn(() => {
|
||||
this.readyState = WebSocket.CLOSED;
|
||||
this.emit("close");
|
||||
});
|
||||
}
|
||||
|
||||
class MockStore extends EventEmitter {
|
||||
task: Task;
|
||||
|
||||
constructor(task: Task) {
|
||||
super();
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
getRootDir(): string {
|
||||
return process.cwd();
|
||||
}
|
||||
|
||||
async listTasks(): Promise<Task[]> {
|
||||
return [this.task];
|
||||
}
|
||||
|
||||
async getTask(id: string): Promise<Task> {
|
||||
if (id !== this.task.id) {
|
||||
const error = Object.assign(new Error("Task not found"), { code: "ENOENT" });
|
||||
throw error;
|
||||
}
|
||||
|
||||
return this.task;
|
||||
}
|
||||
}
|
||||
|
||||
function createTask(overrides: Partial<Task> = {}): Task {
|
||||
return {
|
||||
id: "KB-063",
|
||||
title: "Realtime badge updates",
|
||||
description: "Test task",
|
||||
column: "in-review",
|
||||
dependencies: [],
|
||||
steps: [],
|
||||
currentStep: 0,
|
||||
log: [],
|
||||
createdAt: "2026-03-30T00:00:00.000Z",
|
||||
updatedAt: "2026-03-30T00:00:00.000Z",
|
||||
columnMovedAt: "2026-03-30T00:00:00.000Z",
|
||||
prInfo: {
|
||||
url: "https://github.com/owner/repo/pull/42",
|
||||
number: 42,
|
||||
status: "open",
|
||||
title: "Tracked PR",
|
||||
headBranch: "feature/test",
|
||||
baseBranch: "main",
|
||||
commentCount: 0,
|
||||
lastCheckedAt: "2026-03-30T00:00:00.000Z",
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
async function waitForExpectation(assertion: () => void, timeoutMs: number = 1_000): Promise<void> {
|
||||
const start = Date.now();
|
||||
while (true) {
|
||||
try {
|
||||
assertion();
|
||||
return;
|
||||
} catch (error) {
|
||||
if (Date.now() - start >= timeoutMs) {
|
||||
throw error;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe("WebSocketManager", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("routes subscribe and unsubscribe messages to task channels", () => {
|
||||
const manager = new WebSocketManager();
|
||||
const socket = new MockSocket();
|
||||
|
||||
manager.addClient(socket as unknown as WebSocket, "client-1");
|
||||
socket.emit("message", Buffer.from(JSON.stringify({ type: "subscribe", taskId: "KB-063" })));
|
||||
expect(manager.getSubscriptionCount("KB-063")).toBe(1);
|
||||
|
||||
socket.emit("message", Buffer.from(JSON.stringify({ type: "unsubscribe", taskId: "KB-063" })));
|
||||
expect(manager.getSubscriptionCount("KB-063")).toBe(0);
|
||||
});
|
||||
|
||||
it("broadcasts badge updates only to subscribed clients", () => {
|
||||
const manager = new WebSocketManager();
|
||||
const first = new MockSocket();
|
||||
const second = new MockSocket();
|
||||
|
||||
manager.addClient(first as unknown as WebSocket, "client-1");
|
||||
manager.addClient(second as unknown as WebSocket, "client-2");
|
||||
|
||||
first.emit("message", Buffer.from(JSON.stringify({ type: "subscribe", taskId: "KB-063" })));
|
||||
second.emit("message", Buffer.from(JSON.stringify({ type: "subscribe", taskId: "KB-064" })));
|
||||
|
||||
manager.broadcastBadgeUpdate("KB-063", {
|
||||
prInfo: null,
|
||||
issueInfo: {
|
||||
url: "https://github.com/owner/repo/issues/2",
|
||||
number: 2,
|
||||
state: "closed",
|
||||
title: "Issue",
|
||||
stateReason: "completed",
|
||||
},
|
||||
timestamp: "2026-03-30T12:00:00.000Z",
|
||||
});
|
||||
|
||||
expect(first.send).toHaveBeenCalledTimes(1);
|
||||
expect(second.send).not.toHaveBeenCalled();
|
||||
expect(JSON.parse(first.sent[0])).toMatchObject({
|
||||
type: "badge:updated",
|
||||
taskId: "KB-063",
|
||||
prInfo: null,
|
||||
issueInfo: { number: 2 },
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps connections alive when pong responses arrive", () => {
|
||||
const manager = new WebSocketManager({ heartbeatIntervalMs: 100 });
|
||||
const socket = new MockSocket();
|
||||
|
||||
manager.addClient(socket as unknown as WebSocket, "client-1");
|
||||
|
||||
vi.advanceTimersByTime(100);
|
||||
expect(socket.ping).toHaveBeenCalledTimes(1);
|
||||
|
||||
socket.emit("pong");
|
||||
vi.advanceTimersByTime(100);
|
||||
|
||||
expect(socket.terminate).not.toHaveBeenCalled();
|
||||
expect(manager.getClientCount()).toBe(1);
|
||||
});
|
||||
|
||||
it("terminates dead connections and cleans up subscriptions", () => {
|
||||
const manager = new WebSocketManager({ heartbeatIntervalMs: 100 });
|
||||
const socket = new MockSocket();
|
||||
|
||||
manager.addClient(socket as unknown as WebSocket, "client-1");
|
||||
socket.emit("message", Buffer.from(JSON.stringify({ type: "subscribe", taskId: "KB-063" })));
|
||||
|
||||
vi.advanceTimersByTime(200);
|
||||
|
||||
expect(socket.terminate).toHaveBeenCalled();
|
||||
expect(manager.getClientCount()).toBe(0);
|
||||
expect(manager.getSubscriptionCount("KB-063")).toBe(0);
|
||||
});
|
||||
|
||||
it("disposes sockets without leaking tracked clients", () => {
|
||||
const manager = new WebSocketManager();
|
||||
const socket = new MockSocket();
|
||||
|
||||
manager.addClient(socket as unknown as WebSocket, "client-1");
|
||||
manager.dispose();
|
||||
|
||||
expect(socket.terminate).toHaveBeenCalled();
|
||||
expect(manager.getClientCount()).toBe(0);
|
||||
expect(manager.getSubscribedTaskIds()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("/api/ws integration", () => {
|
||||
let startSpy: ReturnType<typeof vi.spyOn>;
|
||||
let stopSpy: ReturnType<typeof vi.spyOn>;
|
||||
let replaceSpy: ReturnType<typeof vi.spyOn>;
|
||||
let unwatchSpy: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
startSpy = vi.spyOn(githubPoller, "start").mockImplementation(() => {});
|
||||
stopSpy = vi.spyOn(githubPoller, "stop").mockImplementation(() => {});
|
||||
replaceSpy = vi.spyOn(githubPoller, "replaceTaskWatches").mockImplementation(() => {});
|
||||
unwatchSpy = vi.spyOn(githubPoller, "unwatchTask").mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("delivers badge updates to subscribed websocket clients and manages poller lifecycle", async () => {
|
||||
const initialTask = createTask();
|
||||
const store = new MockStore(initialTask);
|
||||
const app = createServer(store as any, { githubToken: "test-token" });
|
||||
const server = app.listen(0);
|
||||
await once(server, "listening");
|
||||
|
||||
const port = (server.address() as import("node:net").AddressInfo).port;
|
||||
const client = new WebSocket(`ws://127.0.0.1:${port}/api/ws`);
|
||||
const messages: any[] = [];
|
||||
|
||||
client.on("message", (payload) => {
|
||||
messages.push(JSON.parse(payload.toString()));
|
||||
});
|
||||
|
||||
await once(client, "open");
|
||||
await waitForExpectation(() => {
|
||||
expect(startSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
client.send(JSON.stringify({ type: "subscribe", taskId: initialTask.id }));
|
||||
await waitForExpectation(() => {
|
||||
expect(replaceSpy).toHaveBeenCalledWith(initialTask.id, [
|
||||
{
|
||||
taskId: initialTask.id,
|
||||
type: "pr",
|
||||
owner: "owner",
|
||||
repo: "repo",
|
||||
number: 42,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
const updatedTask = createTask({
|
||||
prInfo: {
|
||||
...initialTask.prInfo!,
|
||||
status: "merged",
|
||||
title: "Merged PR",
|
||||
lastCheckedAt: "2026-03-30T12:00:00.000Z",
|
||||
},
|
||||
updatedAt: "2026-03-30T12:00:00.000Z",
|
||||
});
|
||||
(store as unknown as MockStore).task = updatedTask;
|
||||
(store as unknown as MockStore).emit("task:updated", updatedTask);
|
||||
|
||||
await waitForExpectation(() => {
|
||||
expect(messages).toContainEqual(expect.objectContaining({
|
||||
type: "badge:updated",
|
||||
taskId: updatedTask.id,
|
||||
prInfo: expect.objectContaining({ status: "merged", title: "Merged PR" }),
|
||||
issueInfo: null,
|
||||
}));
|
||||
});
|
||||
|
||||
client.close();
|
||||
await once(client, "close");
|
||||
|
||||
await waitForExpectation(() => {
|
||||
expect(unwatchSpy).toHaveBeenCalledWith(initialTask.id);
|
||||
expect(stopSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user