fix(recovery): restore source removed by production documentation merge

This commit is contained in:
2026-11-10 03:42:10 +01:00
parent 585bdd4b44
commit 3d2fc6f913
957 changed files with 115754 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
import { describe, expect, it } from "vitest";
import {
operationFingerprint,
operationIdentity,
runIdempotentOperation,
type IdempotencyStore,
} from "../../packages/siax-access/src/idempotency";
describe("@siax/access universal idempotency", () => {
it("binds every result-affecting input into the fingerprint", () => {
const a = operationFingerprint(["t", "p", "u", "instruction-extrahera"]);
const b = operationFingerprint(["t", "p", "u", "instruction-extrahera-belopp"]);
expect(a).not.toBe(b);
});
it("rejects malformed identity (same shape 0pt/DOC0 both enforce)", () => {
expect(() => operationIdentity({ tenantId: "t", projectId: "p", actorId: "a", idempotencyKey: "../bad", payload: [] })).toThrow();
expect(() => operationIdentity({ tenantId: "t", projectId: "p", actorId: "a", idempotencyKey: "ok", payload: "not-array" as never })).toThrow();
});
it("conflicts when a reused key carries a changed payload", () => {
const fp1 = operationFingerprint(["t", "p", "u", "A"]);
const fp2 = operationFingerprint(["t", "p", "u", "B"]);
expect(fp1).not.toBe(fp2);
});
it("replays a stored receipt verbatim on the exact fingerprint", async () => {
const stored = { x: 1 };
const store: IdempotencyStore<unknown, { x: number }> = {
start: async () => false,
save: async () => {},
receipt: async () => stored,
};
const outcome = await runIdempotentOperation({
store, identity: {}, fingerprint: "fp",
dispatch: async () => { throw new Error("must not rerun"); },
});
expect(outcome.kind).toBe("replayed");
if (outcome.kind === "replayed") expect(outcome.receipt).toEqual(stored);
});
it("claims once and saves only after the dispatch", async () => {
const order: string[] = [];
const store: IdempotencyStore<unknown, { x: number }> = {
start: async () => { order.push("start"); return true; },
save: async () => { order.push("save"); },
receipt: async () => { order.push("receipt"); return undefined; },
};
let dispatched = 0;
const outcome = await runIdempotentOperation({
store, identity: {}, fingerprint: "fp",
dispatch: async () => { order.push("dispatch"); return { x: ++dispatched }; },
});
expect(outcome.kind).toBe("claimed");
expect(dispatched).toBe(1);
expect(order).toEqual(["start", "dispatch", "save"]);
});
it("reports a store error as conflict, never as a fresh start", async () => {
const store: IdempotencyStore<unknown, { x: number }> = {
start: async () => { throw new Error("db down"); },
save: async () => {},
receipt: async () => { throw new Error("db down"); },
};
let dispatched = 0;
const outcome = await runIdempotentOperation({
store, identity: {}, fingerprint: "fp",
dispatch: async () => { dispatched++; return { x: 1 }; },
});
expect(outcome.kind).toBe("conflict");
expect(dispatched).toBe(0);
});
it("does not rerun the dispatch when start succeeds but save later fails", async () => {
const store: IdempotencyStore<unknown, { x: number }> = { start: async () => true, save: async () => { throw new Error("journal write failed"); }, receipt: async () => undefined };
let dispatched = 0;
const outcome = await runIdempotentOperation({
store, identity: {}, fingerprint: "fp",
dispatch: async () => { dispatched++; return { x: 1 }; },
});
expect(outcome.kind).toBe("claimed");
expect(dispatched).toBe(1);
});
});
+114
View File
@@ -0,0 +1,114 @@
import { describe, expect, it } from 'vitest';
import {
OpenFgaRelationships,
isProjectAccessAllowed,
openFgaFromEnv,
type ProjectRelationshipScope,
} from '../../packages/siax-access/src/index';
const STORE = '01J8ZQ6K3M9V4T7R2X5C8B1N0A';
const MODEL = '01J8ZQ6K3M9V4T7R2X5C8B1N0B';
const TOKEN = 'test-service-token';
const scope: ProjectRelationshipScope = {
tenantId: 't_0pt',
projectId: 'prj_alpha',
actorId: 'user_123',
};
function client(handler: (url: string, init?: RequestInit) => Response) {
return new OpenFgaRelationships({
baseUrl: 'https://fga.siax.io',
storeId: STORE,
modelId: MODEL,
serviceToken: TOKEN,
fetchImpl: (async (url: string | URL | Request, init?: RequestInit) =>
handler(String(url), init)) as typeof fetch,
});
}
function tupleResponse(allowed: boolean): Response {
return new Response(JSON.stringify({ allowed }), { status: 200 });
}
describe('@siax/access OpenFgaRelationships', () => {
it('refuses non-https configuration and non-ULID ids', () => {
expect(
() =>
new OpenFgaRelationships({
baseUrl: 'http://fga.siax.io',
storeId: STORE,
modelId: MODEL,
serviceToken: TOKEN,
}),
).toThrow();
expect(
() =>
new OpenFgaRelationships({
baseUrl: 'https://fga.siax.io',
storeId: 'not-a-ulid',
modelId: MODEL,
serviceToken: TOKEN,
}),
).toThrow();
});
it('requires both the tenant membership and the project parent tuple', async () => {
const seen: string[] = [];
const c = client((_url, init) => {
const body = JSON.parse(String(init?.body)) as {
tuple_key: { relation: string };
};
seen.push(body.tuple_key.relation);
return tupleResponse(true);
});
await expect(c.checkProject(scope)).resolves.toBe('relationships_match');
expect(seen).toEqual(['member', 'tenant']);
});
it('denies the project when membership is denied even if the parent tuple allows', async () => {
let call = 0;
const c = client(() => tupleResponse(call++ === 1));
await expect(c.checkProject(scope)).resolves.toBe('denied');
});
it('reports unavailable (never allowed) when OpenFGA fails', async () => {
const c = client(() => new Response('boom', { status: 503 }));
await expect(c.checkProject(scope)).resolves.toBe('unavailable');
await expect(isProjectAccessAllowed(scope, { relationships: c })).resolves.toBe(false);
});
it('rejects malformed scope values before any request', async () => {
let called = false;
const c = client(() => {
called = true;
return tupleResponse(true);
});
await expect(c.checkProject({ tenantId: 't/../x', projectId: 'p', actorId: 'a' })).resolves.toBe(
'denied',
);
expect(called).toBe(false);
});
it('fails closed when OpenFGA is not configured', async () => {
await expect(isProjectAccessAllowed(scope, { relationships: undefined })).resolves.toBe(false);
expect(openFgaFromEnv({} as NodeJS.ProcessEnv)).toBeUndefined();
});
it('applies the additional policy only after a relationship match', async () => {
const c = client(() => tupleResponse(true));
await expect(
isProjectAccessAllowed(scope, {
relationships: c,
additionalPolicy: () => false,
}),
).resolves.toBe(false);
await expect(
isProjectAccessAllowed(scope, {
relationships: c,
additionalPolicy: () => {
throw new Error('policy store down');
},
}),
).resolves.toBe(false);
});
});