feat(appdata): 冻结兼容写入入口
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
loadServerData,
|
||||
saveServerData,
|
||||
ServerDataConflictError,
|
||||
ServerDataWriteFrozenError,
|
||||
SERVER_DATA_SAVE_ERROR_EVENT,
|
||||
type ServerDataSaveErrorDetail,
|
||||
} from './server-data';
|
||||
@@ -312,3 +313,64 @@ test('server data save failures dispatch a browser-visible failure event', async
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('server data frozen writes surface the replacement domain API path', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const originalWindowDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'window');
|
||||
const events: Array<CustomEvent<ServerDataSaveErrorDetail>> = [];
|
||||
__resetApiAvailabilityForTests();
|
||||
|
||||
Object.defineProperty(globalThis, 'window', {
|
||||
configurable: true,
|
||||
value: {
|
||||
dispatchEvent: (event: Event) => {
|
||||
events.push(event as CustomEvent<ServerDataSaveErrorDetail>);
|
||||
return true;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith('/config/ai')) {
|
||||
return new Response(JSON.stringify({}), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
if (url.endsWith('/data/dev-tasks') && init?.method === 'PUT') {
|
||||
return new Response(JSON.stringify({
|
||||
code: 'APP_DATA_WRITE_FROZEN',
|
||||
key: 'dev-tasks',
|
||||
state: 'write_frozen',
|
||||
replacement: '/api/v1/versions/:versionId/dev-tasks',
|
||||
}), {
|
||||
status: 409,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
throw new Error(`Unexpected fetch ${init?.method ?? 'GET'} ${url}`);
|
||||
}) as typeof fetch;
|
||||
|
||||
try {
|
||||
await assert.rejects(
|
||||
() => saveServerData('dev-tasks', []),
|
||||
(error: unknown) => error instanceof ServerDataWriteFrozenError &&
|
||||
error.key === 'dev-tasks' &&
|
||||
error.replacement === '/api/v1/versions/:versionId/dev-tasks',
|
||||
);
|
||||
assert.equal(events.length, 1);
|
||||
assert.deepEqual(events[0].detail, {
|
||||
key: 'dev-tasks',
|
||||
message: 'AppData 写入已冻结,请改用 /api/v1/versions/:versionId/dev-tasks',
|
||||
});
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
__resetApiAvailabilityForTests();
|
||||
if (originalWindowDescriptor) {
|
||||
Object.defineProperty(globalThis, 'window', originalWindowDescriptor);
|
||||
} else {
|
||||
Reflect.deleteProperty(globalThis, 'window');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user