This commit is contained in:
75
src/services/customWorldAgentUiState.test.ts
Normal file
75
src/services/customWorldAgentUiState.test.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { expect, test } from 'vitest';
|
||||
|
||||
import {
|
||||
clearCustomWorldAgentUiState,
|
||||
readCustomWorldAgentUiState,
|
||||
writeCustomWorldAgentUiState,
|
||||
} from './customWorldAgentUiState';
|
||||
|
||||
function createMemoryStorage() {
|
||||
const store = new Map<string, string>();
|
||||
|
||||
return {
|
||||
getItem(key: string) {
|
||||
return store.get(key) ?? null;
|
||||
},
|
||||
setItem(key: string, value: string) {
|
||||
store.set(key, value);
|
||||
},
|
||||
removeItem(key: string) {
|
||||
store.delete(key);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('custom world agent ui state reads from query first and persists to session storage', () => {
|
||||
const sessionStorage = createMemoryStorage();
|
||||
let currentUrl = '/play';
|
||||
const env = {
|
||||
location: {
|
||||
pathname: '/play',
|
||||
get search() {
|
||||
const [, search = ''] = currentUrl.split('?');
|
||||
return search ? `?${search}` : '';
|
||||
},
|
||||
},
|
||||
history: {
|
||||
replaceState: (_data: unknown, _unused: string, nextUrl?: string | URL | null) => {
|
||||
currentUrl = String(nextUrl ?? '/play');
|
||||
},
|
||||
},
|
||||
sessionStorage,
|
||||
};
|
||||
|
||||
writeCustomWorldAgentUiState(
|
||||
{
|
||||
activeSessionId: 'session-1',
|
||||
activeOperationId: 'operation-1',
|
||||
customWorldGenerationSource: 'agent-draft-foundation',
|
||||
ownerUserId: 'user-1',
|
||||
},
|
||||
env,
|
||||
);
|
||||
|
||||
expect(currentUrl).toContain('customWorldSessionId=session-1');
|
||||
expect(currentUrl).toContain('customWorldOperationId=operation-1');
|
||||
expect(currentUrl).toContain(
|
||||
'customWorldGenerationSource=agent-draft-foundation',
|
||||
);
|
||||
expect(readCustomWorldAgentUiState(env)).toEqual({
|
||||
activeSessionId: 'session-1',
|
||||
activeOperationId: 'operation-1',
|
||||
customWorldGenerationSource: 'agent-draft-foundation',
|
||||
});
|
||||
|
||||
currentUrl = '/play';
|
||||
expect(readCustomWorldAgentUiState(env)).toEqual({
|
||||
activeSessionId: 'session-1',
|
||||
activeOperationId: 'operation-1',
|
||||
customWorldGenerationSource: 'agent-draft-foundation',
|
||||
ownerUserId: 'user-1',
|
||||
});
|
||||
|
||||
clearCustomWorldAgentUiState(env);
|
||||
expect(readCustomWorldAgentUiState(env)).toEqual({});
|
||||
});
|
||||
Reference in New Issue
Block a user