| 1 | import { describe, expect, it } from 'vitest';
|
| 2 | import { buildApi } from '../apps/api/src/app.js';
|
| 3 |
|
| 4 | describe('HTTP API', () => {
|
| 5 | it('serves health, readiness, and OpenAPI', async () => {
|
| 6 | const app = buildApi();
|
| 7 | await app.ready();
|
| 8 | expect((await app.inject({ method: 'GET', url: '/health' })).statusCode).toBe(200);
|
| 9 | expect((await app.inject({ method: 'GET', url: '/ready' })).json()).toEqual({
|
| 10 | status: 'ready',
|
| 11 | });
|
| 12 | const specification = (await app.inject({ method: 'GET', url: '/openapi.json' })).json();
|
| 13 | expect(specification.info.title).toBe('OpenClasp API');
|
| 14 | expect(specification.paths).toHaveProperty('/v0.1/risk/assess');
|
| 15 | await app.close();
|
| 16 | });
|
| 17 |
|
| 18 | it('isolates hosted dashboard and settings by authenticated operator', async () => {
|
| 19 | const calls: string[] = [];
|
| 20 | const repository = {
|
| 21 | dashboard: async (operatorId: string) => {
|
| 22 | calls.push(`dashboard:${operatorId}`);
|
| 23 | return {
|
| 24 | agents: [],
|
| 25 | interactions: [],
|
| 26 | events: [],
|
| 27 | conflicts: [],
|
| 28 | receipts: [],
|
| 29 | profiles: [],
|
| 30 | };
|
| 31 | },
|
| 32 | getSettings: async (operatorId: string) => {
|
| 33 | calls.push(`settings:${operatorId}`);
|
| 34 | return {
|
| 35 | displayName: '',
|
| 36 | contributionEnabled: false,
|
| 37 | retentionDays: 30,
|
| 38 | evidenceSharing: 'ask' as const,
|
| 39 | rawConversationsStored: false as const,
|
| 40 | };
|
| 41 | },
|
| 42 | saveSettings: async (operatorId: string, value: any) => {
|
| 43 | calls.push(`save:${operatorId}`);
|
| 44 | return { ...value, rawConversationsStored: false as const };
|
| 45 | },
|
| 46 | upsert: async () => undefined,
|
| 47 | list: async () => [],
|
| 48 | };
|
| 49 | const app = buildApi(undefined, undefined, repository);
|
| 50 | await app.ready();
|
| 51 | expect((await app.inject({ method: 'GET', url: '/v0.1/dashboard' })).statusCode).toBe(401);
|
| 52 | expect(
|
| 53 | (
|
| 54 | await app.inject({
|
| 55 | method: 'GET',
|
| 56 | url: '/v0.1/dashboard',
|
| 57 | headers: { 'x-openclasp-operator': 'user-a' },
|
| 58 | })
|
| 59 | ).statusCode,
|
| 60 | ).toBe(200);
|
| 61 | expect(
|
| 62 | (
|
| 63 | await app.inject({
|
| 64 | method: 'GET',
|
| 65 | url: '/v0.1/settings',
|
| 66 | headers: { 'x-openclasp-operator': 'user-b' },
|
| 67 | })
|
| 68 | ).statusCode,
|
| 69 | ).toBe(200);
|
| 70 | expect(calls).toEqual(['dashboard:user-a', 'settings:user-b']);
|
| 71 | await app.close();
|
| 72 | });
|
| 73 | });
|