70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import type { INestApplicationContext } from '@nestjs/common';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { SessionStore } from '../src/identity/session.store';
|
|
import { runExpiryCommand } from '../src/cli/workflow-expiry';
|
|
|
|
describe('authenticated expiry command', () => {
|
|
afterEach(() => {
|
|
jest.restoreAllMocks();
|
|
});
|
|
function fixture(fail = false) {
|
|
const actor = {
|
|
userId: randomUUID(),
|
|
organizationId: randomUUID(),
|
|
sessionId: randomUUID(),
|
|
permissions: ['workflows.run', 'orders.manage'],
|
|
};
|
|
const authenticate = jest.fn().mockResolvedValue(actor);
|
|
const run = fail
|
|
? jest.fn().mockRejectedValue(new Error('Failure'))
|
|
: jest.fn().mockResolvedValue({ id: 'run-id', processedCount: 0 });
|
|
const close = jest.fn().mockResolvedValue(undefined);
|
|
const app = {
|
|
get: (type: unknown) =>
|
|
type === SessionStore ? { authenticate } : { run },
|
|
close,
|
|
} as unknown as INestApplicationContext;
|
|
const create = jest
|
|
.spyOn(NestFactory, 'createApplicationContext')
|
|
.mockResolvedValue(app);
|
|
return { actor, authenticate, run, close, create };
|
|
}
|
|
it('authenticates and runs with a stable supplied key, then closes resources', async () => {
|
|
const f = fixture();
|
|
const key = randomUUID();
|
|
const token = 'a'.repeat(43);
|
|
expect(
|
|
await runExpiryCommand({
|
|
WORKFLOW_TOKEN: token,
|
|
WORKFLOW_LIMIT: '5',
|
|
WORKFLOW_RUN_KEY: key,
|
|
}),
|
|
).toMatchObject({ processedCount: 0 });
|
|
expect(f.authenticate).toHaveBeenCalledWith(token);
|
|
expect(f.run).toHaveBeenCalledWith(f.actor, {
|
|
limit: 5,
|
|
idempotencyKey: key,
|
|
});
|
|
expect(f.close).toHaveBeenCalledTimes(1);
|
|
});
|
|
it('closes resources after a failed workflow', async () => {
|
|
const f = fixture(true);
|
|
await expect(
|
|
runExpiryCommand({ WORKFLOW_TOKEN: 'a'.repeat(43) }),
|
|
).rejects.toThrow('Failure');
|
|
expect(f.close).toHaveBeenCalledTimes(1);
|
|
});
|
|
it('rejects missing credentials and oversized limits before application startup', async () => {
|
|
const f = fixture();
|
|
await expect(runExpiryCommand({})).rejects.toThrow();
|
|
await expect(
|
|
runExpiryCommand({
|
|
WORKFLOW_TOKEN: 'a'.repeat(43),
|
|
WORKFLOW_LIMIT: '26',
|
|
}),
|
|
).rejects.toThrow();
|
|
expect(f.create).not.toHaveBeenCalled();
|
|
});
|
|
});
|