166 lines
4.1 KiB
TypeScript
166 lines
4.1 KiB
TypeScript
import { expect, test } from 'vitest';
|
|
|
|
import {
|
|
clearCustomWorldAgentUiState,
|
|
readCustomWorldAgentUiState,
|
|
shouldRestoreCustomWorldAgentUiState,
|
|
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',
|
|
ownerUserId: 'user-1',
|
|
});
|
|
|
|
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({});
|
|
});
|
|
|
|
test('custom world agent ui state hydrates query owner from matching stored session only', () => {
|
|
const sessionStorage = createMemoryStorage();
|
|
sessionStorage.setItem(
|
|
'genarrative.custom-world-agent-ui.v1',
|
|
JSON.stringify({
|
|
activeSessionId: 'session-1',
|
|
ownerUserId: 'user-1',
|
|
}),
|
|
);
|
|
|
|
expect(
|
|
readCustomWorldAgentUiState({
|
|
location: {
|
|
pathname: '/',
|
|
search: '?customWorldSessionId=session-1',
|
|
},
|
|
history: null,
|
|
sessionStorage,
|
|
}),
|
|
).toEqual({
|
|
activeSessionId: 'session-1',
|
|
activeOperationId: null,
|
|
customWorldGenerationSource: null,
|
|
ownerUserId: 'user-1',
|
|
});
|
|
|
|
expect(
|
|
readCustomWorldAgentUiState({
|
|
location: {
|
|
pathname: '/',
|
|
search: '?customWorldSessionId=session-2',
|
|
},
|
|
history: null,
|
|
sessionStorage,
|
|
}),
|
|
).toEqual({
|
|
activeSessionId: 'session-2',
|
|
activeOperationId: null,
|
|
customWorldGenerationSource: null,
|
|
});
|
|
});
|
|
|
|
test('custom world agent ui state only auto restores stored pointers on RPG creation paths', () => {
|
|
const sessionStorage = createMemoryStorage();
|
|
sessionStorage.setItem(
|
|
'genarrative.custom-world-agent-ui.v1',
|
|
JSON.stringify({
|
|
activeSessionId: 'session-1',
|
|
ownerUserId: 'user-1',
|
|
}),
|
|
);
|
|
|
|
expect(
|
|
shouldRestoreCustomWorldAgentUiState({
|
|
location: {
|
|
pathname: '/',
|
|
search: '',
|
|
},
|
|
history: null,
|
|
sessionStorage,
|
|
}),
|
|
).toBe(false);
|
|
|
|
expect(
|
|
shouldRestoreCustomWorldAgentUiState({
|
|
location: {
|
|
pathname: '/creation/rpg/agent',
|
|
search: '',
|
|
},
|
|
history: null,
|
|
sessionStorage,
|
|
}),
|
|
).toBe(true);
|
|
});
|
|
|
|
test('custom world agent ui state restores explicit query pointers on any main path', () => {
|
|
expect(
|
|
shouldRestoreCustomWorldAgentUiState({
|
|
location: {
|
|
pathname: '/',
|
|
search: '?customWorldSessionId=session-1',
|
|
},
|
|
history: null,
|
|
sessionStorage: createMemoryStorage(),
|
|
}),
|
|
).toBe(true);
|
|
});
|