|
| 1 | +import test, { after } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { lockCredentials } from "./utils/credentials.ts"; |
| 4 | + |
| 5 | +type MessageHandler = (message: any, sender: any, sendResponse: (r?: any) => void) => boolean; |
| 6 | + |
| 7 | +let onMessageListeners: MessageHandler[] = []; |
| 8 | +let alarmListeners: Array<(alarm: { name: string }) => void> = []; |
| 9 | +let createdAlarms: Record<string, { when: number }> = {}; |
| 10 | +let clearedAlarms: string[] = []; |
| 11 | +let fetchCalls: any[] = []; |
| 12 | + |
| 13 | +const mockStorage: Record<string, any> = { |
| 14 | + settings: { summarizationInterval: 30 }, |
| 15 | + activeMeetingState: { |
| 16 | + isActive: true, |
| 17 | + audioActive: true, |
| 18 | + transcript: [ |
| 19 | + { id: "c1", text: "hello", speaker: "John", timestamp: 1, timestampLabel: "00:01" }, |
| 20 | + ], |
| 21 | + lastSummarizedAt: Date.now() - 5000, // 5 seconds ago |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +function installChromeMock() { |
| 26 | + onMessageListeners = []; |
| 27 | + alarmListeners = []; |
| 28 | + createdAlarms = {}; |
| 29 | + clearedAlarms = []; |
| 30 | + fetchCalls = []; |
| 31 | + |
| 32 | + (globalThis as any).fetch = async (url: string, options: any) => { |
| 33 | + fetchCalls.push({ url, options }); |
| 34 | + return { |
| 35 | + ok: true, |
| 36 | + text: async () => |
| 37 | + JSON.stringify({ |
| 38 | + text: "Mock transcript", |
| 39 | + choices: [{ message: { content: '{"text": "Mock transcript"}' } }], |
| 40 | + }), |
| 41 | + json: async () => ({ |
| 42 | + text: "Mock transcript", |
| 43 | + choices: [{ message: { content: '{"text": "Mock transcript"}' } }], |
| 44 | + }), |
| 45 | + }; |
| 46 | + }; |
| 47 | + |
| 48 | + if (typeof (globalThis as any).addEventListener !== "function") { |
| 49 | + (globalThis as any).addEventListener = () => {}; |
| 50 | + } |
| 51 | + (globalThis as any).self = globalThis; |
| 52 | + |
| 53 | + (globalThis as any).chrome = { |
| 54 | + runtime: { |
| 55 | + getURL: (p: string) => `chrome-extension://ext/${p}`, |
| 56 | + sendMessage: async () => {}, |
| 57 | + getContexts: async () => [], |
| 58 | + onMessage: { |
| 59 | + addListener: (cb: MessageHandler) => { |
| 60 | + onMessageListeners.push(cb); |
| 61 | + }, |
| 62 | + }, |
| 63 | + onInstalled: { addListener: () => {} }, |
| 64 | + onStartup: { addListener: () => {} }, |
| 65 | + }, |
| 66 | + alarms: { |
| 67 | + onAlarm: { |
| 68 | + addListener: (cb: any) => { |
| 69 | + alarmListeners.push(cb); |
| 70 | + }, |
| 71 | + }, |
| 72 | + create: (name: string, info: any) => { |
| 73 | + createdAlarms[name] = info; |
| 74 | + }, |
| 75 | + clear: (name: string) => { |
| 76 | + clearedAlarms.push(name); |
| 77 | + delete createdAlarms[name]; |
| 78 | + }, |
| 79 | + }, |
| 80 | + tabs: { |
| 81 | + onUpdated: { addListener: () => {} }, |
| 82 | + onActivated: { addListener: () => {} }, |
| 83 | + onRemoved: { addListener: () => {} }, |
| 84 | + query: async () => [], |
| 85 | + sendMessage: async () => {}, |
| 86 | + }, |
| 87 | + commands: { onCommand: { addListener: () => {} } }, |
| 88 | + contextMenus: { |
| 89 | + onClicked: { addListener: () => {} }, |
| 90 | + removeAll: (cb: any) => cb?.(), |
| 91 | + create: () => {}, |
| 92 | + }, |
| 93 | + sidePanel: { open: async () => {} }, |
| 94 | + storage: { |
| 95 | + local: { |
| 96 | + get: async (key: string) => { |
| 97 | + if (key === "settings") return mockStorage; |
| 98 | + if (key === "credentials") |
| 99 | + return { credentials: { OPENAI_API_KEY: { key: "foo", isEncrypted: false } } }; |
| 100 | + return { [key]: mockStorage[key] }; |
| 101 | + }, |
| 102 | + set: async (items: any) => { |
| 103 | + Object.assign(mockStorage, items); |
| 104 | + }, |
| 105 | + remove: async (key: string) => { |
| 106 | + delete mockStorage[key]; |
| 107 | + }, |
| 108 | + }, |
| 109 | + session: { |
| 110 | + get: async (key: string) => ({ openai_api_key: "foo" }), |
| 111 | + set: async (items: any) => {}, |
| 112 | + remove: async (key: string) => {}, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +installChromeMock(); |
| 119 | + |
| 120 | +// Load the module after mocks |
| 121 | +await import("./background.ts"); |
| 122 | + |
| 123 | +async function sendMessage(msg: any) { |
| 124 | + for (const listener of onMessageListeners) { |
| 125 | + listener(msg, {}, () => {}); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); |
| 130 | + |
| 131 | +test("Interval guard schedules an alarm if elapsed time is less than interval", async () => { |
| 132 | + // Reset test tracking variables |
| 133 | + createdAlarms = {}; |
| 134 | + clearedAlarms = []; |
| 135 | + fetchCalls = []; |
| 136 | + |
| 137 | + // Note: activeMeetingState was already hydrated at module load because we set it |
| 138 | + // in mockStorage before `await import("./background.ts")`. Thus `state.isActive` is true, |
| 139 | + // and `state.lastSummarizedAt` is very recent. |
| 140 | + |
| 141 | + // Send first chunk to trigger initial summarization and set lastSummarizedAt > 0 |
| 142 | + await sendMessage({ |
| 143 | + type: "OFFSCREEN_AUDIO_CHUNK", |
| 144 | + audioBase64: "A".repeat(10000), |
| 145 | + timestamp: Date.now(), |
| 146 | + }); |
| 147 | + |
| 148 | + // Wait for it to process |
| 149 | + await sleep(200); |
| 150 | + |
| 151 | + // Now state.lastSummarizedAt is recent. Send second chunk to hit the interval guard. |
| 152 | + await sendMessage({ |
| 153 | + type: "OFFSCREEN_AUDIO_CHUNK", |
| 154 | + audioBase64: "A".repeat(10000), |
| 155 | + timestamp: Date.now(), |
| 156 | + }); |
| 157 | + |
| 158 | + // Wait for the queue to process it |
| 159 | + await sleep(100); |
| 160 | + |
| 161 | + // We expect SUMMARY_RETRY_ALARM to be created because it's been less than 30s since the first summary |
| 162 | + assert.ok(createdAlarms["summarize-retry"], "Alarm should be scheduled"); |
| 163 | + const alarmWhen = createdAlarms["summarize-retry"].when; |
| 164 | + assert.ok(alarmWhen > Date.now(), "Alarm should be in the future"); |
| 165 | +}); |
| 166 | + |
| 167 | +test("Manual stop audio forces final summary flush", async () => { |
| 168 | + createdAlarms = {}; |
| 169 | + clearedAlarms = []; |
| 170 | + fetchCalls = []; |
| 171 | + |
| 172 | + // Trigger manual stop |
| 173 | + await sendMessage({ type: "MANUAL_STOP_AUDIO" }); |
| 174 | + await sleep(500); |
| 175 | + |
| 176 | + // The final flush clears the retry alarm |
| 177 | + assert.ok( |
| 178 | + clearedAlarms.includes("summarize-retry"), |
| 179 | + "Retry alarm should be cleared before final flush", |
| 180 | + ); |
| 181 | + |
| 182 | + // Check that fetch was called for the summary endpoint (OPENAI_CHAT_URL) |
| 183 | + const chatCalls = fetchCalls.filter((f) => f.url.includes("api.openai.com/v1/chat/completions")); |
| 184 | + assert.ok(chatCalls.length > 0, "A final summary request should be dispatched"); |
| 185 | +}); |
| 186 | + |
| 187 | +after(() => { |
| 188 | + // Clear the 30-minute auto-lock timer created in credentials.ts when getApiKey() unlocks credentials. |
| 189 | + lockCredentials(); |
| 190 | +}); |
0 commit comments