-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22325 from storybookjs/shilman/persistent-session-id
Telemetry: Persist sessionId across runs
- Loading branch information
Showing
4 changed files
with
180 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { nanoid } from 'nanoid'; | ||
import { cache } from '@storybook/core-common'; | ||
import { resetSessionIdForTest, getSessionId, SESSION_TIMEOUT } from './session-id'; | ||
|
||
jest.mock('@storybook/core-common', () => { | ||
const actual = jest.requireActual('@storybook/core-common'); | ||
return { | ||
...actual, | ||
cache: { | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
jest.mock('nanoid'); | ||
|
||
const spy = (x: any) => x as jest.SpyInstance; | ||
|
||
describe('getSessionId', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
resetSessionIdForTest(); | ||
}); | ||
|
||
test('returns existing sessionId when cached in memory and does not fetch from disk', async () => { | ||
const existingSessionId = 'memory-session-id'; | ||
resetSessionIdForTest(existingSessionId); | ||
|
||
const sessionId = await getSessionId(); | ||
|
||
expect(cache.get).not.toHaveBeenCalled(); | ||
expect(cache.set).toHaveBeenCalledTimes(1); | ||
expect(cache.set).toHaveBeenCalledWith( | ||
'session', | ||
expect.objectContaining({ id: existingSessionId }) | ||
); | ||
expect(sessionId).toBe(existingSessionId); | ||
}); | ||
|
||
test('returns existing sessionId when cached on disk and not expired', async () => { | ||
const existingSessionId = 'existing-session-id'; | ||
const existingSession = { | ||
id: existingSessionId, | ||
lastUsed: Date.now() - SESSION_TIMEOUT + 1000, | ||
}; | ||
|
||
spy(cache.get).mockResolvedValueOnce(existingSession); | ||
|
||
const sessionId = await getSessionId(); | ||
|
||
expect(cache.get).toHaveBeenCalledTimes(1); | ||
expect(cache.get).toHaveBeenCalledWith('session'); | ||
expect(cache.set).toHaveBeenCalledTimes(1); | ||
expect(cache.set).toHaveBeenCalledWith( | ||
'session', | ||
expect.objectContaining({ id: existingSessionId }) | ||
); | ||
expect(sessionId).toBe(existingSessionId); | ||
}); | ||
|
||
test('generates new sessionId when none exists', async () => { | ||
const newSessionId = 'new-session-id'; | ||
(nanoid as any as jest.SpyInstance).mockReturnValueOnce(newSessionId); | ||
|
||
spy(cache.get).mockResolvedValueOnce(undefined); | ||
|
||
const sessionId = await getSessionId(); | ||
|
||
expect(cache.get).toHaveBeenCalledTimes(1); | ||
expect(cache.get).toHaveBeenCalledWith('session'); | ||
expect(nanoid).toHaveBeenCalledTimes(1); | ||
expect(cache.set).toHaveBeenCalledTimes(1); | ||
expect(cache.set).toHaveBeenCalledWith( | ||
'session', | ||
expect.objectContaining({ id: newSessionId }) | ||
); | ||
expect(sessionId).toBe(newSessionId); | ||
}); | ||
|
||
test('generates new sessionId when existing one is expired', async () => { | ||
const expiredSessionId = 'expired-session-id'; | ||
const expiredSession = { id: expiredSessionId, lastUsed: Date.now() - SESSION_TIMEOUT - 1000 }; | ||
const newSessionId = 'new-session-id'; | ||
spy(nanoid).mockReturnValueOnce(newSessionId); | ||
|
||
spy(cache.get).mockResolvedValueOnce(expiredSession); | ||
|
||
const sessionId = await getSessionId(); | ||
|
||
expect(cache.get).toHaveBeenCalledTimes(1); | ||
expect(cache.get).toHaveBeenCalledWith('session'); | ||
expect(nanoid).toHaveBeenCalledTimes(1); | ||
expect(cache.set).toHaveBeenCalledTimes(1); | ||
expect(cache.set).toHaveBeenCalledWith( | ||
'session', | ||
expect.objectContaining({ id: newSessionId }) | ||
); | ||
expect(sessionId).toBe(newSessionId); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { nanoid } from 'nanoid'; | ||
import { cache } from '@storybook/core-common'; | ||
|
||
export const SESSION_TIMEOUT = 1000 * 60 * 60 * 2; // 2h | ||
|
||
interface Session { | ||
id: string; | ||
lastUsed: number; | ||
} | ||
|
||
let sessionId: string | undefined; | ||
|
||
export const resetSessionIdForTest = (val: string | undefined = undefined) => { | ||
sessionId = val; | ||
}; | ||
|
||
export const getSessionId = async () => { | ||
const now = Date.now(); | ||
if (!sessionId) { | ||
const session: Session | undefined = await cache.get('session'); | ||
if (session && session.lastUsed >= now - SESSION_TIMEOUT) { | ||
sessionId = session.id; | ||
} else { | ||
sessionId = nanoid(); | ||
} | ||
} | ||
await cache.set('session', { id: sessionId, lastUsed: now }); | ||
return sessionId; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters