Add lib/api/routes/resources.js: CRUD scoped to space; dependency add/list/remove (cross-space attempts mapped to 409 conflict via the composite FK); source-docs index per resource; change history via audit.listForEntity. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
74 lines
3.2 KiB
JavaScript
74 lines
3.2 KiB
JavaScript
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
|
|
import request from 'supertest';
|
|
import { setup } from './helpers.js';
|
|
import * as spaces from '../../lib/db/repos/spaces.js';
|
|
|
|
let app, ownerHeaders, space, otherSpace;
|
|
const owner = { kind: 'user', id: null };
|
|
|
|
async function makeResource(spaceId, slug) {
|
|
const res = await request(app).post(`/api/spaces/${spaceId}/resources`).set(ownerHeaders)
|
|
.send({ slug, name: slug, runtime_type: 'lxc' });
|
|
return res.body;
|
|
}
|
|
|
|
beforeAll(async () => { ({ app, ownerHeaders } = await setup()); });
|
|
beforeEach(async () => {
|
|
space = await spaces.create({ slug: `s-${Date.now()}-${Math.random().toString(36).slice(2,5)}`, name: 'S' }, owner);
|
|
otherSpace = await spaces.create({ slug: `o-${Date.now()}-${Math.random().toString(36).slice(2,5)}`, name: 'O' }, owner);
|
|
});
|
|
|
|
describe('resources routes', () => {
|
|
it('POST creates and lists by space', async () => {
|
|
const r = await makeResource(space.id, 'svc');
|
|
expect(r.runtime_type).toBe('lxc');
|
|
const list = await request(app).get(`/api/spaces/${space.id}/resources`).set(ownerHeaders);
|
|
expect(list.body.length).toBe(1);
|
|
});
|
|
|
|
it('PATCH updates status', async () => {
|
|
const r = await makeResource(space.id, 'svc2');
|
|
const res = await request(app).patch(`/api/resources/${r.id}`).set(ownerHeaders)
|
|
.send({ status: 'running' });
|
|
expect(res.body.status).toBe('running');
|
|
});
|
|
|
|
it('POST dependencies rejects self-dependency', async () => {
|
|
const r = await makeResource(space.id, 'self');
|
|
const res = await request(app).post(`/api/resources/${r.id}/dependencies`).set(ownerHeaders)
|
|
.send({ depends_on: r.id });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('POST dependencies rejects cross-space → 409', async () => {
|
|
const a = await makeResource(space.id, 'a');
|
|
const b = await makeResource(otherSpace.id, 'b');
|
|
const res = await request(app).post(`/api/resources/${a.id}/dependencies`).set(ownerHeaders)
|
|
.send({ depends_on: b.id });
|
|
expect(res.status).toBe(409);
|
|
expect(res.body.error.code).toBe('conflict');
|
|
});
|
|
|
|
it('list + remove dependency', async () => {
|
|
const a = await makeResource(space.id, 'svc-a');
|
|
const b = await makeResource(space.id, 'svc-b');
|
|
await request(app).post(`/api/resources/${a.id}/dependencies`).set(ownerHeaders)
|
|
.send({ depends_on: b.id, kind: 'hard' });
|
|
const list = await request(app).get(`/api/resources/${a.id}/dependencies`).set(ownerHeaders);
|
|
expect(list.body.length).toBe(1);
|
|
expect(list.body[0].depends_on).toBe(b.id);
|
|
const del = await request(app).delete(`/api/resources/${a.id}/dependencies/${b.id}`).set(ownerHeaders);
|
|
expect(del.status).toBe(204);
|
|
const after = await request(app).get(`/api/resources/${a.id}/dependencies`).set(ownerHeaders);
|
|
expect(after.body.length).toBe(0);
|
|
});
|
|
|
|
it('GET /:id/changes returns audit history', async () => {
|
|
const r = await makeResource(space.id, 'audited');
|
|
await request(app).patch(`/api/resources/${r.id}`).set(ownerHeaders).send({ status: 'running' });
|
|
const res = await request(app).get(`/api/resources/${r.id}/changes`).set(ownerHeaders);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.length).toBe(2); // create + update
|
|
});
|
|
});
|