File size: 9,993 Bytes
17c0464 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import {testFrameworkBinding} from "../../testing/framework-fixtures";
import {expect, test} from "bun:test";
import {createTestRequestsApi} from "./test-requests.api";
import {TestRequestConflict, TestRequestService, type HostAcceptance} from "../../services/test-request.service";
const token = "synthetic-controller-credential-" + "x".repeat(32);
const headers = {authorization: `Bearer ${token}`, "content-type": "application/json"};
const digest = "a".repeat(64);
test('preparation minimum is opt-in, authenticated and clears when withheld requests are withdrawn', async () => {
let minimum: number | null = 1085; const calls: string[] = [];
class Service extends TestRequestService {
override async queued() {return {requests: [], preparations: [], dispatchCompletions: [], nextCursor: null};}
override async preparationFrameworkMinimum(hostId: string) {calls.push(hostId); return minimum;}
}
const api = createTestRequestsApi(new Service(), () => JSON.stringify({mini: token}));
expect(await (await api.request('/', {headers})).json()).not.toHaveProperty('minimumFrameworkVersion'); expect(calls).toEqual([]);
expect(await (await api.request('/?preparationFrameworkMinimum=1&hostId=other', {headers})).json()).toMatchObject({minimumFrameworkVersion: 1085});
expect(calls).toEqual(['mini']); minimum = null;
expect(await (await api.request('/?preparationFrameworkMinimum=1', {headers})).json()).toMatchObject({minimumFrameworkVersion: null});
expect((await api.request('/?preparationFrameworkMinimum=2', {headers})).status).toBe(400);
expect((await api.request('/?preparationFrameworkMinimum=1')).status).toBe(401);
});
test("delivery scopes queue and acceptance to authenticated controller", async () => {
const calls: unknown[] = [];
class Service extends TestRequestService {
override async queued(host: string, cursor: string | undefined, limit: number) {
calls.push({host, cursor, limit});
return {requests: [], preparations: [], dispatchCompletions: [], nextCursor: null};
}
override async accept(receipt: HostAcceptance, host: string) {
if (receipt.hostId !== host) throw new TestRequestConflict("wrong host");
return {requestId: receipt.requestId, hostId: host, inputSha256: digest,
input: {}, state: "accepted" as const, hostReceipt: receipt};
}
}
const api = createTestRequestsApi(new Service(), () => JSON.stringify({mini: token}));
expect((await api.request("/?hostId=other&limit=2", {headers})).status).toBe(200);
expect(calls).toEqual([{host: "mini", cursor: undefined, limit: 2}]);
const receipt = {requestId: "r1", inputSha256: digest, hostId: "mini", acceptedAt: "2026-10-02T19:00:00Z"};
const response = await api.request("/r1/accept", {method: "POST", headers, body: JSON.stringify(receipt)});
expect(response.status).toBe(200);
expect(await response.json()).toEqual({receipt});
expect((await api.request("/r1/accept", {method: "POST", headers,
body: JSON.stringify({...receipt, hostId: "other"})})).status).toBe(409);
expect((await api.request("/r2/accept", {method: "POST", headers,
body: JSON.stringify(receipt)})).status).toBe(400);
});
test("malformed acceptance and storage outage cannot acknowledge execution", async () => {
class Service extends TestRequestService {
override async queued(): Promise<never> {throw new Error("database unavailable");}
}
const api = createTestRequestsApi(new Service(), () => JSON.stringify({mini: token}));
const response = await api.request("/", {headers});
expect(response.status).toBe(503);
expect(await response.json()).toEqual({error: "request_delivery_unavailable"});
expect((await api.request("/r1/accept", {method: "POST", headers, body: "{"})).status).toBe(400);
expect((await api.request("/r1/accept", {method: "POST", headers, body: "[]"})).status).toBe(400);
});
test("host rejection returns the immutable receipt and refuses foreign or malformed delivery identity", async () => {
const rejection = {requestId: "r1", hostId: "mini", inputSha256: digest, rejectedAt: "2026-10-03T11:00:00Z",
code: "missing-definition", reason: "The requested revision is not installed."};
class Service extends TestRequestService {
override async reject(input: unknown, host: string) {
const receipt = input as typeof rejection;
if (receipt.hostId !== host) throw new TestRequestConflict("wrong host");
return {requestId: receipt.requestId, hostId: host, inputSha256: digest, input: {}, state: "terminal" as const,
terminalStatus: "not-run", hostRejection: receipt};
}
}
const api = createTestRequestsApi(new Service(), () => JSON.stringify({mini: token}));
const response = await api.request("/r1/reject", {method: "POST", headers, body: JSON.stringify(rejection)});
expect(response.status).toBe(200);
expect(await response.json()).toEqual({rejection});
expect((await api.request("/r2/reject", {method: "POST", headers, body: JSON.stringify(rejection)})).status).toBe(400);
expect((await api.request("/r1/reject", {method: "POST", headers, body: JSON.stringify({...rejection, hostId: "other"})})).status).toBe(409);
expect((await api.request("/r1/reject", {method: "POST", headers, body: JSON.stringify({...rejection, status: "pass"})})).status).toBe(400);
expect((await api.request("/r1/reject", {method: "POST", headers: {"content-type": "application/json"}, body: JSON.stringify(rejection)})).status).toBe(401);
});
test("cancellation delivery scopes the host and acknowledges the exact cooperative intent", async () => {
const cancellation = {requestId: "r1", hostId: "mini", inputSha256: digest, requestedAt: "2026-10-03T14:00:00Z", reason: "Occurrence boundary"};
class Service extends TestRequestService {
override async cancellations(host: string) {
expect(host).toBe("mini");
return {requests: [{requestId: "r1", hostId: host, inputSha256: digest, input: {}, state: "terminal" as const,
terminalStatus: "cancelled", hostCancellation: cancellation}], nextCursor: null};
}
override async acknowledgeCancellation(input: unknown, host: string) {
if ((input as typeof cancellation).hostId !== host) throw new TestRequestConflict("wrong host");
return {requestId: "r1", hostId: host, inputSha256: digest, input: {}, state: "accepted" as const, hostCancellation: cancellation};
}
}
const api = createTestRequestsApi(new Service(), () => JSON.stringify({mini: token}));
expect((await api.request("/cancellations?hostId=other", {headers})).status).toBe(200);
const response = await api.request("/r1/cancel-ack", {method: "POST", headers, body: JSON.stringify(cancellation)});
expect(response.status).toBe(200);
expect(await response.json()).toEqual({cancellation});
expect((await api.request("/r2/cancel-ack", {method: "POST", headers, body: JSON.stringify(cancellation)})).status).toBe(400);
expect((await api.request("/r1/cancel-ack", {method: "POST", headers, body: JSON.stringify({...cancellation, hostId: "other"})})).status).toBe(409);
});
test('owned qualification read is host-authenticated, exact, bounded and explicitly acknowledged',async()=>{
const calls:unknown[]=[];
class Service extends TestRequestService {override async queued(){return {requests:[],preparations:[],dispatchCompletions:[],nextCursor:null};}}
const owned={jobId:'original',laneId:'mac',inputSha256:digest,ownerId:'passive-owner',generation:4,binding:testFrameworkBinding(),controller:{incarnation:'reporter',generation:1}};
const api=createTestRequestsApi(new Service(),()=>JSON.stringify({mini:token}),undefined,undefined,{async qualificationTargets(host,value){calls.push({host,value});return[];}});
const query=new URLSearchParams({qualificationTargets:'1',ownedQualification:JSON.stringify(owned)});
const result=await api.request(`/?${query}`,{headers});expect(result.status).toBe(200);expect(await result.json()).toMatchObject({ownedQualification:owned,qualificationTargets:[]});
expect(calls).toEqual([{host:'mini',value:owned}]);
expect((await api.request(`/?${query}`,{headers:{}})).status).toBe(401);
expect((await api.request('/?ownedQualification='+encodeURIComponent(JSON.stringify(owned)),{headers})).status).toBe(400);
expect((await api.request('/?qualificationTargets=1&ownedQualification='+encodeURIComponent(JSON.stringify({...owned,hostId:'other'})),{headers})).status).toBe(400);
});
test('unclaimed qualification lookup is exact, host-authenticated and cannot carry invented custody', async () => {
const calls: unknown[] = [], unclaimed = {jobId: 'original', inputSha256: digest};
class Service extends TestRequestService {override async queued(){return {requests:[],preparations:[],dispatchCompletions:[],nextCursor:null};}}
const api = createTestRequestsApi(new Service(), () => JSON.stringify({mini: token}), undefined, undefined,
{async qualificationTargets(host, owned, value){calls.push({host, owned, value}); return [];}});
const query = new URLSearchParams({qualificationTargets: '1', unclaimedQualification: JSON.stringify(unclaimed)});
const result = await api.request(`/?${query}`, {headers}); expect(result.status).toBe(200);
expect(await result.json()).toMatchObject({unclaimedQualification: unclaimed, qualificationTargets: []});
expect(calls).toEqual([{host: 'mini', owned: undefined, value: unclaimed}]);
expect((await api.request(`/?${query}`, {headers: {}})).status).toBe(401);
expect((await api.request('/?unclaimedQualification='+encodeURIComponent(JSON.stringify(unclaimed)), {headers})).status).toBe(400);
expect((await api.request('/?qualificationTargets=1&unclaimedQualification='+encodeURIComponent(JSON.stringify({...unclaimed, ownerId: 'invented'})), {headers})).status).toBe(400);
expect((await api.request(`/?${query}&ownedQualification=ignored`, {headers})).status).toBe(400);
});
|