feat(api): advance capability and secure operations slices

This commit is contained in:
chick
2026-07-17 11:57:20 +08:00
parent 8018b36adf
commit 1cc4a995ee
19 changed files with 2102 additions and 22 deletions
@@ -1,7 +1,35 @@
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { SafeUpstreamGateway } from './safe-upstream-gateway.js';
describe('SafeUpstreamGateway', () => {
it('dispatches only the audited zero-body network registration operation through pinned POST', async () => {
const calls: unknown[] = [];
const gateway = new SafeUpstreamGateway({
transport: {
get: async () => ({ status: 200, headers: {}, body: '' }),
post: async (url, headers, body) => {
calls.push({ url, headers, body });
return { status: 204, headers: {}, body: '' };
},
},
});
const response = await gateway.postNetworkRegisterAuto('http://192.168.1.20:8080');
expect(response.status).toBe(204);
expect(calls).toEqual([
{ url: 'http://192.168.1.20:8080/api/network/register-auto', headers: {}, body: '' },
]);
});
it('rejects malformed operation origins before calling transport', async () => {
const post = vi.fn(async () => ({ status: 204, headers: {}, body: '' }));
const gateway = new SafeUpstreamGateway({
transport: { get: async () => ({ status: 200, headers: {}, body: '' }), post },
});
await expect(
gateway.postNetworkRegisterAuto('http://192.168.1.20/base?next=x'),
).rejects.toMatchObject({ code: 'UPSTREAM_REQUEST_INVALID', dispatched: false });
expect(post).not.toHaveBeenCalled();
});
it('sends login password only as JSON through the pinned POST transport', async () => {
const calls: unknown[] = [];
const gateway = new SafeUpstreamGateway({