import test from "node:test"; import assert from "node:assert/strict"; import { createRefreshThrottleState, markScheduledRefreshExecuted, planThrottledRefresh, shouldRefreshRealtimeEvent, } from "../src/lib/realtime-refresh"; test("shouldRefreshRealtimeEvent filters scoped conversation updates by project and note", () => { assert.equal( shouldRefreshRealtimeEvent({ eventType: "conversation.updated", eventData: JSON.stringify({ projectId: "project-a", note: "project_goals.updated" }), projectIds: ["project-a", "project-b"], conversationUpdatedNotes: ["project_goals.updated"], }), true, ); assert.equal( shouldRefreshRealtimeEvent({ eventType: "conversation.updated", eventData: JSON.stringify({ projectId: "project-z", note: "project_goals.updated" }), projectIds: ["project-a", "project-b"], conversationUpdatedNotes: ["project_goals.updated"], }), false, ); assert.equal( shouldRefreshRealtimeEvent({ eventType: "conversation.updated", eventData: JSON.stringify({ projectId: "project-a", note: "other.note" }), projectIds: ["project-a", "project-b"], conversationUpdatedNotes: ["project_goals.updated"], }), false, ); }); test("planThrottledRefresh coalesces bursts into one delayed refresh", () => { const state = createRefreshThrottleState(); assert.deepEqual(planThrottledRefresh(state, 1_000, 400), { type: "refresh_now", }); assert.equal(state.lastRefreshAt, 1_000); assert.equal(state.pending, false); assert.deepEqual(planThrottledRefresh(state, 1_180, 400), { type: "schedule_refresh", delayMs: 220, }); assert.equal(state.pending, true); assert.deepEqual(planThrottledRefresh(state, 1_220, 400), { type: "skip", }); markScheduledRefreshExecuted(state, 1_400); assert.equal(state.pending, false); assert.equal(state.lastRefreshAt, 1_400); assert.deepEqual(planThrottledRefresh(state, 1_900, 400), { type: "refresh_now", }); });