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.
99 lines
3.9 KiB
TypeScript
99 lines
3.9 KiB
TypeScript
import { test, expect, mock, beforeEach } from "bun:test";
|
|
import { docCreate, sparqlUpdate, sparqlQuery } from "../src/surface/docs";
|
|
|
|
// The reach guard is process-wide: once ANY cap exists it applies to every reader.
|
|
// This suite declares none, so it must not inherit another suite's enforcement.
|
|
beforeEach(() => {
|
|
resetCaps();
|
|
setCurrentUser(null);
|
|
});
|
|
import * as ngProxy from "../src/surface/ng-proxy";
|
|
|
|
// NOTE ORDER: the "not configured → throw" case MUST run before any configure()
|
|
// call, because configure() sets a module-level singleton with no public reset.
|
|
|
|
test("throws a clear error when configure() was not called", async () => {
|
|
await expect(docCreate("sid", "Graph", "data:graph", "store")).rejects.toThrow(
|
|
/configure\(\) must be called before use/,
|
|
);
|
|
await expect(sparqlUpdate("sid", "INSERT DATA {}")).rejects.toThrow(
|
|
/configure\(\) must be called before use/,
|
|
);
|
|
await expect(sparqlQuery("sid", "SELECT * {}")).rejects.toThrow(
|
|
/configure\(\) must be called before use/,
|
|
);
|
|
});
|
|
|
|
// From here on, a fake real `ng` is injected via configure().
|
|
import { configure, resetCaps, setCurrentUser } from "../src/polyfill";
|
|
|
|
function fakeNg() {
|
|
return {
|
|
doc_create: mock(async (..._a: unknown[]) => "did:ng:o:new-doc"),
|
|
sparql_update: mock(async (..._a: unknown[]) => undefined),
|
|
sparql_query: mock(async (..._a: unknown[]) => ({ results: { bindings: [] } })),
|
|
// A sentinel: makeNg(), if ever used, would `.bind` and call THIS through
|
|
// the JS Proxy. We assert the primitives call the raw fns above directly.
|
|
};
|
|
}
|
|
|
|
function inject() {
|
|
const ng = fakeNg();
|
|
configure({ ng: ng as any, useShape: (() => {}) as any });
|
|
return ng;
|
|
}
|
|
|
|
test("docCreate calls the real injected ng.doc_create with the exact args", async () => {
|
|
const ng = inject();
|
|
const nuri = await docCreate("sid-1", "Graph", "data:graph", "store", undefined);
|
|
expect(nuri).toBe("did:ng:o:new-doc");
|
|
expect(ng.doc_create).toHaveBeenCalledTimes(1);
|
|
expect(ng.doc_create.mock.calls[0]).toEqual(["sid-1", "Graph", "data:graph", "store", undefined]);
|
|
});
|
|
|
|
test("sparqlUpdate forwards (sessionId, query, anchor) to the real ng.sparql_update", async () => {
|
|
const ng = inject();
|
|
await sparqlUpdate("sid-2", "INSERT DATA { GRAPH <did:ng:o:a> { <s> <p> <o> } }", "did:ng:o:a");
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(1);
|
|
expect(ng.sparql_update.mock.calls[0]).toEqual([
|
|
"sid-2",
|
|
"INSERT DATA { GRAPH <did:ng:o:a> { <s> <p> <o> } }",
|
|
"did:ng:o:a",
|
|
]);
|
|
});
|
|
|
|
test("sparqlUpdate passes anchor=undefined when omitted", async () => {
|
|
const ng = inject();
|
|
await sparqlUpdate("sid-3", "INSERT DATA {}");
|
|
expect(ng.sparql_update.mock.calls[0]).toEqual(["sid-3", "INSERT DATA {}", undefined]);
|
|
});
|
|
|
|
test("sparqlQuery forwards (sessionId, query, base, anchor) and returns the raw result", async () => {
|
|
const ng = inject();
|
|
const res = await sparqlQuery("sid-4", "SELECT ?e { GRAPH <g> { ?s ?p ?e } }", undefined, "did:ng:o:g");
|
|
expect(res).toEqual({ results: { bindings: [] } });
|
|
expect(ng.sparql_query).toHaveBeenCalledTimes(1);
|
|
expect(ng.sparql_query.mock.calls[0]).toEqual([
|
|
"sid-4",
|
|
"SELECT ?e { GRAPH <g> { ?s ?p ?e } }",
|
|
undefined,
|
|
"did:ng:o:g",
|
|
]);
|
|
});
|
|
|
|
test("the primitives do NOT route through the public ng proxy (makeNg)", async () => {
|
|
// makeNg builds a JS Proxy over the injected ng. If a primitive went through
|
|
// it, calls would land on the proxy's `get` trap, not on our raw mock fns.
|
|
// Spy on makeNg: it must never be invoked by the docs primitives.
|
|
const spy = mock(ngProxy.makeNg);
|
|
const ng = inject();
|
|
await docCreate("sid", "Graph", "data:graph", "store");
|
|
await sparqlUpdate("sid", "INSERT DATA {}");
|
|
await sparqlQuery("sid", "SELECT * {}");
|
|
expect(spy).toHaveBeenCalledTimes(0);
|
|
// And the raw injected fns were reached directly:
|
|
expect(ng.doc_create).toHaveBeenCalledTimes(1);
|
|
expect(ng.sparql_update).toHaveBeenCalledTimes(1);
|
|
expect(ng.sparql_query).toHaveBeenCalledTimes(1);
|
|
});
|