Skip to content

Commit f351600

Browse files
committed
Flush event cache when ai prepare event is expired
1 parent f802462 commit f351600

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

code/core/src/telemetry/ai-prepare-utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getAiPreparePending } from './event-cache.ts';
1+
import { flushAiPreparePending, getAiPreparePending } from './event-cache.ts';
22
import { SESSION_TIMEOUT } from './session-id.ts';
33
import { createHash } from 'node:crypto';
44
import { readFile } from 'node:fs/promises';
@@ -107,7 +107,8 @@ export async function collectAiPrepareEvidence(
107107
// Gate 4: Is it within the session window?
108108
const timeSincePrepare = Date.now() - pending.timestamp;
109109
if (timeSincePrepare > SESSION_TIMEOUT) {
110-
// TODO: purge aiPreparePending
110+
// Session expired, clean up pending record.
111+
await flushAiPreparePending();
111112
return;
112113
}
113114

code/core/src/telemetry/event-cache.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
44
import { cache } from 'storybook/internal/common';
55

66
import type { CacheEntry } from './event-cache.ts';
7-
import { getLastEvents, getPrecedingUpgrade, set } from './event-cache.ts';
7+
import {
8+
flushAiPreparePending,
9+
getAiPreparePending,
10+
getLastEvents,
11+
getPrecedingUpgrade,
12+
set,
13+
} from './event-cache.ts';
814
import type { TelemetryEvent } from './types.ts';
915

1016
vi.mock('storybook/internal/common', { spy: true });
@@ -347,4 +353,40 @@ describe('event-cache', () => {
347353
expect(result).toEqual(afterDev);
348354
});
349355
});
356+
357+
describe('ai-prepare pending cache', () => {
358+
let cacheGetMock: MockInstance;
359+
let cacheRemoveMock: MockInstance;
360+
361+
beforeEach(() => {
362+
vi.clearAllMocks();
363+
cacheGetMock = vi.mocked(cache.get);
364+
cacheRemoveMock = vi.mocked(cache.remove);
365+
});
366+
367+
it('returns cached ai-prepare pending record when present', async () => {
368+
const pending = {
369+
timestamp: 123,
370+
sessionId: 'session-1',
371+
configDir: '/tmp/.storybook',
372+
previewPath: '/tmp/.storybook/preview.ts',
373+
previewHash: 'abc123',
374+
};
375+
376+
cacheGetMock.mockResolvedValueOnce(pending);
377+
378+
await expect(getAiPreparePending()).resolves.toEqual(pending);
379+
expect(cacheGetMock).toHaveBeenCalledWith('ai-prepare-pending');
380+
});
381+
382+
it('removes the cached ai-prepare pending record and returns undefined', async () => {
383+
cacheRemoveMock.mockResolvedValueOnce(undefined);
384+
cacheGetMock.mockResolvedValueOnce(undefined);
385+
386+
await expect(flushAiPreparePending()).resolves.toBeUndefined();
387+
expect(cacheRemoveMock).toHaveBeenCalledWith('ai-prepare-pending');
388+
await expect(getAiPreparePending()).resolves.toBeUndefined();
389+
expect(cacheGetMock).toHaveBeenCalledWith('ai-prepare-pending');
390+
});
391+
});
350392
});

code/core/src/telemetry/event-cache.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,10 @@ export const getAiPreparePending = async (): Promise<AiPreparePendingRecord | un
9999
await processingPromise;
100100
return (await cache.get('ai-prepare-pending')) ?? undefined;
101101
};
102+
103+
export const flushAiPreparePending = async (): Promise<undefined> => {
104+
// Wait for any pending set operations to complete before removing
105+
await processingPromise;
106+
await cache.remove('ai-prepare-pending');
107+
return undefined;
108+
};

0 commit comments

Comments
 (0)