88914f50ae
Les 25 modules étaient à plat, nommés d'après ce qu'ils font mécaniquement (`store-registry`, `read-model`, `reach`, `caps`). Rien dans l'arborescence ne disait lesquels DEVIENDRONT le vrai SDK, lesquels tiennent lieu du travail que le verifier fera nativement, et lesquels n'existent que parce qu'un wallet est partagé — trois destins sans rapport. Quatre dossiers, les deux fichiers d'entrée restant à la racine pour que l'`exports` du paquet et le code du consommateur ne bougent pas : - `model/` — le modèle d'adressage de la cible, transcrit : vocabulaire pur, pas d'I/O. Survit comme connaissance. - `surface/` — ce que l'app touche, chaque symbole ayant un pendant cible documenté. Supprimé quand l'alias bascule ; le code de l'app est inchangé. - `emulated-verifier/` — les doublures de ce que le verifier fait nativement : possession, dépôt des caps, frontière, non-livraison, traitement des inbox, registres de branche, ouverture de repo. **C'est le dossier où diverger du modèle est possible.** Le préfixe `emulated-` porte le sens : tient lieu de, jamais est — cette bibliothèque ne réside dans aucune couche de la cible, elle les référence. - `shared-wallet/` — n'existe que parce qu'un wallet héberge toutes les identités. Aucun pendant, rien sur quoi s'aligner ; sa seule loi est de rester invisible depuis `surface/`. S'évapore, remplacé par rien. `store-registry-api.ts` devient `surface/placement.ts` : il faisait déjà à la main ce que la frontière de dossier fait structurellement — c'est la meilleure preuve interne du bien-fondé de ce rangement. Ce commit ne fait que déplacer et recâbler les imports (src, test, e2e). Les scissions des modules à cheval suivent. 157 tests unitaires, typecheck src/test/e2e vert.
91 lines
3.1 KiB
TypeScript
91 lines
3.1 KiB
TypeScript
import { test, expect, mock, afterEach } from "bun:test";
|
|
import { makeNg } from "../src/surface/ng-proxy";
|
|
import {
|
|
configure,
|
|
resetConfig,
|
|
getCaps,
|
|
resetCaps,
|
|
setCurrentUser,
|
|
} from "../src/polyfill";
|
|
|
|
// This suite injects a fake `ng` via configure() and declares WRITE caps —
|
|
// which stay an authorization list on purpose: only READING is key possession
|
|
// (P1a). The write axis is decorative until P1b (every internal writer bypasses
|
|
// this proxy). Reset after each test so the docs.test.ts "not configured" guard
|
|
// still holds and no cap leaks into another suite.
|
|
afterEach(() => {
|
|
resetConfig();
|
|
resetCaps();
|
|
setCurrentUser(null);
|
|
});
|
|
|
|
function fakeNg() {
|
|
return { sparql_update: mock(async (..._a: unknown[]) => undefined) };
|
|
}
|
|
|
|
function inject() {
|
|
const ng = fakeNg();
|
|
configure({ ng: ng as any, useShape: (() => {}) as any });
|
|
return ng;
|
|
}
|
|
|
|
const DOC = "did:ng:o:doc";
|
|
const UPDATE = `INSERT DATA { GRAPH <${DOC}> { <s> <p> <o> } }`;
|
|
|
|
test("write guard: passthrough when NO write policy is declared (no regression)", async () => {
|
|
const ng = inject();
|
|
setCurrentUser("bob"); // not a writer, but there's no policy at all
|
|
const proxy = makeNg();
|
|
await proxy.sparql_update("sid", UPDATE, DOC);
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("write guard: passthrough for an UNGOVERNED doc even when a policy exists elsewhere", async () => {
|
|
const ng = inject();
|
|
getCaps().grantWrite("did:ng:o:other", "alice"); // policy on another doc
|
|
setCurrentUser("bob");
|
|
const proxy = makeNg();
|
|
await proxy.sparql_update("sid", UPDATE, DOC); // DOC itself is ungoverned
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("write guard: REJECTS when the doc is governed and the user lacks the write cap", async () => {
|
|
const ng = inject();
|
|
getCaps().grantWrite(DOC, "alice"); // alice holds the write cap
|
|
setCurrentUser("bob"); // bob does not
|
|
const proxy = makeNg();
|
|
await expect(proxy.sparql_update("sid", UPDATE, DOC)).rejects.toThrow(
|
|
/write denied/,
|
|
);
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(0); // never reached the real ng
|
|
});
|
|
|
|
test("write guard: REJECTS an anonymous (null) user on a governed doc", async () => {
|
|
const ng = inject();
|
|
getCaps().grantWrite(DOC, "alice");
|
|
setCurrentUser(null);
|
|
const proxy = makeNg();
|
|
await expect(proxy.sparql_update("sid", UPDATE, DOC)).rejects.toThrow(
|
|
/write denied/,
|
|
);
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(0);
|
|
});
|
|
|
|
test("write guard: ALLOWS the write-cap holder", async () => {
|
|
const ng = inject();
|
|
getCaps().grantWrite(DOC, "alice");
|
|
setCurrentUser("alice"); // owner always holds the write cap
|
|
const proxy = makeNg();
|
|
await proxy.sparql_update("sid", UPDATE, DOC);
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("write guard: passthrough when anchor is omitted (cannot scope the guard)", async () => {
|
|
const ng = inject();
|
|
getCaps().grantWrite(DOC, "alice");
|
|
setCurrentUser("bob");
|
|
const proxy = makeNg();
|
|
await proxy.sparql_update("sid", "INSERT DATA {}"); // no anchor → passthrough
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(1);
|
|
});
|