Files
canina/backend/src/orchestration/orchestration-read.http.spec.ts
T

158 lines
4.8 KiB
TypeScript

import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { OrchestrationController } from './orchestration.controller';
import { OrchestrationService } from './orchestration.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
describe('integration read boundary', () => {
let app: INestApplication;
const task = {
id: 't1',
key: 'k',
title: 'Review',
department: 'SEO',
state: 'GATE2_APPROVED',
version: 1,
operationMode: 'PREPARE',
budgetLimitCents: 0,
metadata: { token: 'secret' },
runs: [
{
id: 'r',
status: 'VERIFIED',
commitSha: 'abc',
environment: 'stage',
artifacts: [
{
id: 'a',
sha256: 'f'.repeat(64),
sizeBytes: 1n,
uri: 'secret-uri',
metadata: { token: 'secret' },
},
],
},
],
events: [
{
id: 'e',
eventType: 'CREATED',
sequence: 1,
payload: { token: 'secret' },
},
],
};
const service = {
findAll: jest.fn(() => Promise.resolve([task])),
findOne: jest.fn(() => Promise.resolve(task)),
createTask: jest.fn(),
transition: jest.fn(),
decideGate: jest.fn(),
recordBudget: jest.fn(),
setKillSwitch: jest.fn(),
command: jest.fn(),
reconcileBudget: jest.fn(),
};
beforeAll(async () => {
const m = await Test.createTestingModule({
controllers: [OrchestrationController],
providers: [
{ provide: OrchestrationService, useValue: service },
RolesGuard,
],
})
.overrideGuard(JwtAuthGuard)
.useValue({
canActivate(c: any) {
const req = c.switchToHttp().getRequest();
if (!req.headers.authorization) return false;
req.user = { id: 'verified-user', role: req.headers.authorization };
return true;
},
})
.compile();
app = m.createNestApplication();
await app.init();
});
afterAll(async () => app.close());
it('denies every mutation before legacy core is invoked', async () => {
for (const [method, path] of [
['post', 'tasks'],
['put', 'tasks/t1/transition'],
['post', 'tasks/t1/gates/2/decide'],
['post', 'tasks/t1/budget'],
['post', 'tasks/t1/commands/RUN_CREATE'],
['post', 'tasks/t1/budget/reconcile'],
['put', 'kill-switch'],
['post', 'chat/messages'],
] as const) {
await request(app.getHttpServer())
[method]('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.send({})
.expect(403);
}
for (const name of [
'createTask',
'transition',
'decideGate',
'recordBudget',
'setKillSwitch',
'command',
'reconcileBudget',
] as const)
expect(service[name]).not.toHaveBeenCalled();
});
it('projects list and detail with no actions, arbitrary secrets or BigInt transport failures', async () => {
for (const path of ['tasks', 'tasks/t1']) {
const r = await request(app.getHttpServer())
.get('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.expect(200);
const t = Array.isArray(r.body) ? r.body[0] : r.body;
expect(t.availableActions).toEqual([]);
expect(t.budgetLimitCents).toBe(0);
expect(JSON.stringify(t)).not.toMatch(/secret|sizeBytes|payload|token/);
}
});
it('returns actual inventory overview and explicit unavailable runtime resources', async () => {
const r = await request(app.getHttpServer())
.get('/admin/orchestration/overview')
.set('Authorization', 'Admin')
.expect(200);
expect(r.body.items).toEqual([
expect.objectContaining({ id: 'tasks', count: 1 }),
]);
expect(r.body.capabilities).toMatchObject({
mutations: false,
chat: false,
runtime: false,
});
for (const path of ['agents', 'schedules', 'model-policy'])
await request(app.getHttpServer())
.get('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.expect(503);
const releases = await request(app.getHttpServer())
.get('/admin/orchestration/releases')
.set('Authorization', 'Admin')
.expect(200);
expect(releases.body.items[0]).toMatchObject({
id: 'r',
commitSha: 'abc',
environment: 'stage',
});
});
it('protects read surfaces from anonymous and customer access', async () => {
await request(app.getHttpServer())
.get('/admin/orchestration/overview')
.expect(403);
await request(app.getHttpServer())
.get('/admin/orchestration/tasks')
.set('Authorization', 'CUSTOMER')
.expect(403);
});
});