|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockEnv, mockEnqueue } = vi.hoisted(() => ({ |
| 7 | + mockEnv: { COPILOT_API_KEY: undefined as string | undefined } as Record< |
| 8 | + string, |
| 9 | + string | undefined |
| 10 | + >, |
| 11 | + mockEnqueue: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/core/config/env', () => ({ env: mockEnv })) |
| 15 | +vi.mock('@/lib/core/outbox/service', () => ({ enqueueOutboxEvent: mockEnqueue })) |
| 16 | +vi.mock('@/lib/mothership/constants', () => ({ SIM_AGENT_API_URL: 'https://agent.example.test' })) |
| 17 | +vi.mock('@/lib/mothership/request/go/fetch', () => ({ fetchGo: vi.fn() })) |
| 18 | +vi.mock('@/lib/mothership/request/headers', () => ({ mothershipRequestHeaders: () => ({}) })) |
| 19 | +vi.mock('@/lib/mothership/server/agent-url', () => ({ getMothershipSourceEnvHeaders: () => ({}) })) |
| 20 | + |
| 21 | +import { enqueueMemoryCleanup } from '@/lib/mothership/memories/cleanup-outbox' |
| 22 | + |
| 23 | +const executor = { insert: vi.fn() } |
| 24 | +const targets = [ |
| 25 | + { kind: 'private_owner' as const, userId: 'user-1' }, |
| 26 | + { kind: 'workspace' as const, workspaceId: '00000000-0000-4000-8000-000000000001' }, |
| 27 | +] |
| 28 | + |
| 29 | +describe('enqueueMemoryCleanup', () => { |
| 30 | + beforeEach(() => { |
| 31 | + vi.clearAllMocks() |
| 32 | + mockEnv.COPILOT_API_KEY = undefined |
| 33 | + }) |
| 34 | + |
| 35 | + it('queues nothing when this Sim has no worker credential', async () => { |
| 36 | + await enqueueMemoryCleanup(executor, targets) |
| 37 | + expect(mockEnqueue).not.toHaveBeenCalled() |
| 38 | + }) |
| 39 | + |
| 40 | + it('queues one credential-bound event per destination and target', async () => { |
| 41 | + mockEnv.COPILOT_API_KEY = 'configured-key' |
| 42 | + await enqueueMemoryCleanup(executor, targets) |
| 43 | + expect(mockEnqueue).toHaveBeenCalledTimes(targets.length) |
| 44 | + for (const [, , payload] of mockEnqueue.mock.calls) |
| 45 | + expect(payload).toMatchObject({ |
| 46 | + baseUrl: 'https://agent.example.test', |
| 47 | + credentialFingerprint: expect.any(String), |
| 48 | + }) |
| 49 | + }) |
| 50 | +}) |
0 commit comments