@crawlcheck/sdk 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/sdk.test.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Runs against the live API. npm test
2
2
  import test from "node:test"; import assert from "node:assert/strict";
3
- import { CrawlCheck, verifyBundle, verifyReceipt, CrawlCheckError } from "../dist/index.js";
3
+ import { CrawlCheck, verifyBundle, verifyReceipt, CrawlCheckError, verifyDocument } from "../dist/index.js";
4
4
  const c = new CrawlCheck();
5
5
 
6
6
  test("conformance: every published fixture gives its expected output through this package's verifier", async () => {
@@ -43,3 +43,28 @@ test("a tampered live bundle fails offline", async () => {
43
43
  test("locked data surfaces as a typed error, not a crash", async () => {
44
44
  await assert.rejects(c.impact({ domain: "wikipedia.org" }), (e) => e instanceof CrawlCheckError && e.locked === true && e.status === 403);
45
45
  });
46
+
47
+ test("live preflight: the decision receipt verifies offline, its key is published, a changed decision fails, guard agrees", async () => {
48
+ const rc = await c.preflight("example.com", "read");
49
+ assert.equal(rc.kind, "crawlcheck-decision");
50
+ assert.ok(["allow", "warn", "require_confirmation", "block", "unsupported"].includes(rc.decision));
51
+ const v = await c.verifyDocument(rc);
52
+ assert.equal(v.verified, true, JSON.stringify(v.checks));
53
+ for (const id of ["document_hash", "key_id", "signature", "key_published", "resolve_bound", "decision_known"]) assert.equal(v.checks.find((c) => c.id === id)?.ok, true, id);
54
+ const forged = JSON.parse(JSON.stringify(rc)); forged.decision = rc.decision === "allow" ? "block" : "allow";
55
+ const f = await verifyDocument(forged);
56
+ assert.equal(f.verified, false);
57
+ assert.equal(f.checks.find((c) => c.id === "document_hash").ok, false);
58
+ const g = await c.guard("example.com", "read");
59
+ assert.equal(g.verification.verified, true);
60
+ assert.equal(g.proceed, g.receipt.decision === "allow");
61
+ const r = await c.resolve("example.com");
62
+ const off = await verifyDocument(r);
63
+ assert.equal(off.verified, true); assert.equal(off.issuer_trusted, null); assert.equal(off.accepted, false, "offline is never accepted");
64
+ assert.equal((await c.verifyDocument(r)).accepted, true);
65
+ const ids = await c.publishedKeyIds();
66
+ const ix = await (await fetch(c.base + "/conformance/index.json")).json();
67
+ const foreign = await (await fetch(ix.fixtures.find((x) => x.name === "receipt-foreign-key").file)).json();
68
+ assert.equal((await verifyReceipt(foreign)).verified, true, "self-consistent offline");
69
+ assert.ok(!ids.includes(foreign.signature.kid), "but not a CrawlCheck key, so never accepted");
70
+ });