fix(云智能体): 避免轻微时差导致连接失败

This commit is contained in:
2026-09-14 20:57:02 +08:00
parent 1d00ba39bd
commit 8a67e8bd47
3 changed files with 80 additions and 3 deletions

View File

@@ -44,6 +44,35 @@ beforeEach(() => { vi.clearAllMocks(); journalRecords.clear(); clearWorksSquareS
afterEach(() => { instances.splice(0).forEach((module) => module.dispose()); clearWorksSquareSession(); vi.useRealTimers(); });
describe('Main cloud Agents boundary', () => {
it('accepts a five-minute session from a clock one second ahead and caps local caching', async () => {
vi.useFakeTimers();
const now = Math.floor(Date.now() / 1000) * 1000;
vi.setSystemTime(now);
const transport = vi.fn(async (url: string) => url.endsWith('/session')
? json({ access_token: 'yuxi-secret', token_type: 'bearer', scope: 'makelore-agents',
expires_at: Math.floor(Date.now() / 1000) + 301, api_base_url: 'https://agents.example.test' })
: json({ agents: [], next_cursor: null }));
const module = moduleFor(transport);
expect(await module.list()).toEqual({ agents: [], next_cursor: null });
expect(transport.mock.calls.map(([url]) => new URL(url).pathname))
.toEqual(['/api/cloud-agents/session', '/api/makelore/agents']);
vi.setSystemTime(now + 294000);
await module.list();
expect(transport.mock.calls.filter(([url]) => url.endsWith('/session'))).toHaveLength(1);
vi.setSystemTime(now + 295000);
await module.list();
expect(transport.mock.calls.filter(([url]) => url.endsWith('/session'))).toHaveLength(2);
});
it.each([0, -1, 1e308])('rejects an expired or invalid session timestamp %s before calling Yuxi', async (expiresAt) => {
const transport = vi.fn().mockResolvedValue(json({
access_token: 'yuxi-secret', token_type: 'bearer', scope: 'makelore-agents',
expires_at: expiresAt, api_base_url: 'https://agents.example.test',
}));
await expect(moduleFor(transport).list()).rejects.toMatchObject({ code: 'cloud_service_unavailable' });
expect(transport).toHaveBeenCalledTimes(1);
});
it('persists recovery in the real electron-store file and reloads it after replacing Main', async () => {
const directory = await mkdtemp(join(tmpdir(), 'makelore-cloud-journal-'));
try {