Files
TREK/server/tests/unit/nest/oauth-token-retention.job.test.ts
T
Maurice b64da26651 feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect
their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or
OpenRouter key) or use the one the admin set up, including a custom
OpenAI-compatible server. It is an addon and only shows while a connection
is usable.

The chat runs on the existing MCP tools, pinned to the trip. Every change is
held as a change card the user applies or discards, and applied changes can
be undone. On the desktop it is a floating window that can be moved and
resized; on the phone it is fullscreen. Search results show as place cards
that can go onto a day, weather lookups as a strip, proposed places as
ghost markers on the map with a before/after switch, and an answer can be
shared into the trip chat. Booking files can be dropped in and are read
into change cards.
2026-10-22 21:30:12 +00:00

56 lines
2.8 KiB
TypeScript

import type { OauthClientsRepository } from '../../../src/db/repositories/OauthClients.repository';
import type { OauthTokensRepository } from '../../../src/db/repositories/OauthTokens.repository';
import { OauthTokenRetentionJob, OAUTH_TOKEN_RETENTION_DAYS } from '../../../src/nest/oauth/oauth-token-retention.job';
import type { CronRegistrarService } from '../../../src/nest/scheduling/cron-registrar.service';
import { describe, expect, it, vi } from 'vitest';
const logMock = vi.hoisted(() => ({ logInfo: vi.fn(), logError: vi.fn(), logWarn: vi.fn(), logDebug: vi.fn() }));
vi.mock('../../../src/nest/audit/audit-log.logger', () => logMock);
function make(
deleteExpiredBefore: (cutoff: string) => Promise<number>,
enabled = true,
deleteUnusedCimdBefore: (cutoff: string) => Promise<number> = vi.fn().mockResolvedValue(0),
) {
const registrar = { isEnabled: vi.fn(() => enabled), register: vi.fn() };
const job = new OauthTokenRetentionJob(
{ deleteExpiredBefore } as unknown as OauthTokensRepository,
{ deleteUnusedCimdBefore } as unknown as OauthClientsRepository,
registrar as unknown as CronRegistrarService,
);
return { job, registrar };
}
describe('OauthTokenRetentionJob', () => {
it('OAUTHRET-001: registers a nightly job, and nothing under the test gate', () => {
const { job, registrar } = make(vi.fn());
job.onApplicationBootstrap();
expect(registrar.register).toHaveBeenCalledWith('oauth-token-retention', '30 3 * * *', expect.any(Function));
const gated = make(vi.fn(), false);
gated.job.onApplicationBootstrap();
expect(gated.registrar.register).not.toHaveBeenCalled();
});
it('OAUTHRET-002: deletes what expired more than the retention period ago and logs the count', async () => {
const del = vi.fn().mockResolvedValue(4);
await make(del).job.tick(new Date('2026-10-07T00:00:00.000Z'));
expect(OAUTH_TOKEN_RETENTION_DAYS).toBe(30);
expect(del).toHaveBeenCalledWith('2026-09-07 00:00:00');
expect(logMock.logInfo).toHaveBeenCalledWith('OAuth token retention: removed 4 expired token(s)');
});
it('OAUTHRET-003: a failing purge is logged, not thrown', async () => {
await make(vi.fn().mockRejectedValue(new Error('database is locked'))).job.tick();
expect(logMock.logError).toHaveBeenCalledWith('OAuth token retention: database is locked');
});
it('OAUTHRET-004: drops metadata-document clients without tokens from before the same cutoff', async () => {
const dropClients = vi.fn().mockResolvedValue(2);
await make(vi.fn().mockResolvedValue(0), true, dropClients).job.tick(new Date('2026-10-07T00:00:00.000Z'));
expect(dropClients).toHaveBeenCalledWith('2026-09-07 00:00:00');
expect(logMock.logInfo).toHaveBeenCalledWith('OAuth token retention: removed 2 unused metadata-document client(s)');
});
});