clone

a deno framework for local-first web apps that open instantly, work offline and sync when users choose

feat/prf-provision-custody
lofi / package / access / access_operations_test.ts
access_operations_test.ts 2.4K
1import { schema as s } from "jazz-tools";2import {3 AccessError,4 createSharingOperations,5 decodeSharingIdentity,6 encodeSharingIdentity,7 sharedGrantTable,8} from "./mod.ts";9import { assert } from "../runtime/test-assert.ts";1011const app = s.defineApp({12 docs: s.table({ title: s.string() }),13 grants: sharedGrantTable("docs"),14});1516Deno.test("sharing identities round-trip only inside the configured app", () => {17 const identity = encodeSharingIdentity("stable-jazz-principal");18 assert(19 decodeSharingIdentity(identity) === "stable-jazz-principal",20 "identity did not round-trip",21 );22 let code = "";23 try {24 decodeSharingIdentity("lofi1:different-app:principal");25 } catch (error) {26 code = error instanceof AccessError ? error.code : "unexpected";27 }28 assert(code === "invalid-identity", "cross-app identity was accepted");29});3031Deno.test("sharing identities tolerate copy/paste padding but never inner whitespace", () => {32 const identity = encodeSharingIdentity("stable-jazz-principal");33 assert(34 decodeSharingIdentity(` ${identity}\n`) === "stable-jazz-principal",35 "surrounding whitespace from copy/paste must not change the decoded principal",36 );37 // A grant recorded for a whitespace-bearing principal looks active to the38 // owner but never matches the recipient's account — reject at both ends.39 let decoded = "";40 try {41 decodeSharingIdentity(identity.replace("principal", "prin cipal"));42 } catch (error) {43 decoded = error instanceof AccessError ? error.code : "unexpected";44 }45 assert(decoded === "invalid-identity", "inner whitespace in the principal was accepted");46 let encoded = "";47 try {48 encodeSharingIdentity("principal with spaces");49 } catch (error) {50 encoded = error instanceof AccessError ? error.code : "unexpected";51 }52 assert(encoded === "invalid-identity", "a whitespace-bearing principal must not encode");53});5455Deno.test("shared operations reject managed-sync work before touching a local-only runtime", async () => {56 const operations = createSharingOperations({ resource: app.docs, grants: app.grants });57 let code = "";58 try {59 await operations.share("resource", encodeSharingIdentity("recipient"), "read");60 } catch (error) {61 code = error instanceof AccessError ? error.code : "unexpected";62 }63 assert(code === "sync-required", "local-only sharing did not report its sync precondition");64});