@embedpdf/engine-core 3.0.0-next.12 → 3.0.0-next.13

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.
Files changed (59) hide show
  1. package/dist/{AbortError-CRSooKsk.cjs → AbortError-Cq2igpZ6.cjs} +2 -2
  2. package/dist/{AbortError-CRSooKsk.cjs.map → AbortError-Cq2igpZ6.cjs.map} +1 -1
  3. package/dist/{AbortError-8-9JAX1Y.js → AbortError-DHqPBnYc.js} +2 -2
  4. package/dist/{AbortError-8-9JAX1Y.js.map → AbortError-DHqPBnYc.js.map} +1 -1
  5. package/dist/{AnnotationListSnapshot-x6tKulNV.d.cts → AnnotationListSnapshot-D3mFut1e.d.cts} +1155 -171
  6. package/dist/{AnnotationListSnapshot-x6tKulNV.d.ts → AnnotationListSnapshot-D3mFut1e.d.ts} +1155 -171
  7. package/dist/{DocumentSecurityService-CK0pejAK.d.cts → DocumentSecurityService-ClTZc0zO.d.cts} +2 -2
  8. package/dist/{DocumentSecurityService-9MawYxP-.d.ts → DocumentSecurityService-_JRjqvoI.d.ts} +2 -2
  9. package/dist/{Engine-Ds0ytbl6.d.ts → Engine-Bx0NOfmd.d.cts} +166 -11
  10. package/dist/{Engine--0XF0UAK.d.cts → Engine-DxtpCYcP.d.ts} +166 -11
  11. package/dist/{PageInsertBlankInput-CzShuSJI.cjs → PageInsertBlankInput-CqXBmFhN.cjs} +37 -2
  12. package/dist/PageInsertBlankInput-CqXBmFhN.cjs.map +1 -0
  13. package/dist/{PageInsertBlankInput-WKY0tr6S.js → PageInsertBlankInput-cHq9-i-z.js} +37 -2
  14. package/dist/PageInsertBlankInput-cHq9-i-z.js.map +1 -0
  15. package/dist/{builders-8Hl1AYNU.cjs → builders-Bqm8yTpi.cjs} +1470 -4
  16. package/dist/builders-Bqm8yTpi.cjs.map +1 -0
  17. package/dist/{builders-DOjcKDWQ.js → builders-Dqo2X-dt.js} +1351 -5
  18. package/dist/builders-Dqo2X-dt.js.map +1 -0
  19. package/dist/conformance.cjs +1079 -13
  20. package/dist/conformance.cjs.map +1 -1
  21. package/dist/conformance.d.cts +70 -3
  22. package/dist/conformance.d.ts +70 -3
  23. package/dist/conformance.js +1079 -14
  24. package/dist/conformance.js.map +1 -1
  25. package/dist/{fold-C7I5GpBz.cjs → fold-2V29cxCV.cjs} +2 -2
  26. package/dist/{fold-C7I5GpBz.cjs.map → fold-2V29cxCV.cjs.map} +1 -1
  27. package/dist/{fold-BfyxxwS6.js → fold-CdEJTBTn.js} +2 -2
  28. package/dist/{fold-BfyxxwS6.js.map → fold-CdEJTBTn.js.map} +1 -1
  29. package/dist/index.cjs +33 -4
  30. package/dist/index.d.cts +2 -2
  31. package/dist/index.d.ts +2 -2
  32. package/dist/index.js +5 -5
  33. package/dist/{resolver-D-jumvgM.cjs → resolver-CYVNV4hn.cjs} +216 -10
  34. package/dist/resolver-CYVNV4hn.cjs.map +1 -0
  35. package/dist/{resolver-CHG4waXL.js → resolver-inAVzsWt.js} +157 -11
  36. package/dist/resolver-inAVzsWt.js.map +1 -0
  37. package/dist/runtime.cjs +34 -5
  38. package/dist/runtime.d.cts +192 -5
  39. package/dist/runtime.d.ts +192 -5
  40. package/dist/runtime.js +6 -6
  41. package/dist/{schemas-DOyVEPk7.js → schemas-BLARNCgx.js} +420 -4
  42. package/dist/schemas-BLARNCgx.js.map +1 -0
  43. package/dist/{schemas-RChsAMWw.cjs → schemas-DIqFMmrC.cjs} +581 -3
  44. package/dist/schemas-DIqFMmrC.cjs.map +1 -0
  45. package/dist/wire.cjs +228 -12
  46. package/dist/wire.cjs.map +1 -1
  47. package/dist/wire.d.cts +415 -4
  48. package/dist/wire.d.ts +415 -4
  49. package/dist/wire.js +199 -13
  50. package/dist/wire.js.map +1 -1
  51. package/package.json +1 -1
  52. package/dist/PageInsertBlankInput-CzShuSJI.cjs.map +0 -1
  53. package/dist/PageInsertBlankInput-WKY0tr6S.js.map +0 -1
  54. package/dist/builders-8Hl1AYNU.cjs.map +0 -1
  55. package/dist/builders-DOjcKDWQ.js.map +0 -1
  56. package/dist/resolver-CHG4waXL.js.map +0 -1
  57. package/dist/resolver-D-jumvgM.cjs.map +0 -1
  58. package/dist/schemas-DOyVEPk7.js.map +0 -1
  59. package/dist/schemas-RChsAMWw.cjs.map +0 -1
@@ -1,8 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_fold = require("./fold-C7I5GpBz.cjs");
3
- const require_PageInsertBlankInput = require("./PageInsertBlankInput-CzShuSJI.cjs");
4
- const require_AbortError = require("./AbortError-CRSooKsk.cjs");
5
- const require_schemas = require("./schemas-RChsAMWw.cjs");
2
+ const require_fold = require("./fold-2V29cxCV.cjs");
3
+ const require_PageInsertBlankInput = require("./PageInsertBlankInput-CqXBmFhN.cjs");
4
+ const require_AbortError = require("./AbortError-Cq2igpZ6.cjs");
5
+ const require_schemas = require("./schemas-DIqFMmrC.cjs");
6
6
  //#region src/conformance/runMetadataConformance.ts
7
7
  /**
8
8
  * The conformance harness is transport-agnostic. It takes the test runner as
@@ -389,7 +389,7 @@ function runActionsConformance(runner, opts) {
389
389
  if (engine) await engine.destroy();
390
390
  });
391
391
  test("reads catalog additional actions and preserves /CO identity", async () => {
392
- const doc = await open(engine, opts, opts.fixtures.document);
392
+ const doc = await open$1(engine, opts, opts.fixtures.document);
393
393
  try {
394
394
  expect(Boolean(doc.actions)).toBe(true);
395
395
  const snapshot = await doc.actions.read();
@@ -410,7 +410,7 @@ function runActionsConformance(runner, opts) {
410
410
  }
411
411
  });
412
412
  test("attaches page /AA to PageLayout", async () => {
413
- const doc = await open(engine, opts, opts.fixtures.page);
413
+ const doc = await open$1(engine, opts, opts.fixtures.page);
414
414
  try {
415
415
  const pages = await doc.pages.list();
416
416
  expect(pages.pages[0].actions?.open?.root?.type).toBe("goto-embedded");
@@ -422,7 +422,7 @@ function runActionsConformance(runner, opts) {
422
422
  }
423
423
  });
424
424
  test("attaches activation actions to AnnotationBase, including links", async () => {
425
- const doc = await open(engine, opts, opts.fixtures.annotation);
425
+ const doc = await open$1(engine, opts, opts.fixtures.annotation);
426
426
  try {
427
427
  const firstPage = (await doc.pages.list()).pages[0];
428
428
  const snapshot = await doc.page(firstPage.pageObjectNumber).annotations.list();
@@ -439,7 +439,7 @@ function runActionsConformance(runner, opts) {
439
439
  test("classifies a /S /JavaScript link target, script staying on base.actions", async () => {
440
440
  const fixture = opts.fixtures.javascriptLink;
441
441
  if (!fixture) return;
442
- const doc = await open(engine, opts, fixture);
442
+ const doc = await open$1(engine, opts, fixture);
443
443
  try {
444
444
  const firstPage = (await doc.pages.list()).pages[0];
445
445
  const link = (await doc.page(firstPage.pageObjectNumber).annotations.list()).annotations.find((annotation) => annotation.subtype === "link");
@@ -452,7 +452,7 @@ function runActionsConformance(runner, opts) {
452
452
  }
453
453
  });
454
454
  test("keeps field actions separate from merged widget events", async () => {
455
- const doc = await open(engine, opts, opts.fixtures.field);
455
+ const doc = await open$1(engine, opts, opts.fixtures.field);
456
456
  try {
457
457
  const form = await doc.forms.list();
458
458
  expect(form.fields).toHaveLength(1);
@@ -466,7 +466,7 @@ function runActionsConformance(runner, opts) {
466
466
  }
467
467
  });
468
468
  test("carries interpreter payloads on every executable node", async () => {
469
- const doc = await open(engine, opts, opts.fixtures.payloads);
469
+ const doc = await open$1(engine, opts, opts.fixtures.payloads);
470
470
  try {
471
471
  const page = (await doc.pages.list()).pages[0];
472
472
  const snapshot = await doc.page(page.pageObjectNumber).annotations.list();
@@ -627,7 +627,7 @@ function runActionsConformance(runner, opts) {
627
627
  }
628
628
  });
629
629
  test("degrades unreadable payloads to unknown with payload-dropped", async () => {
630
- const doc = await open(engine, opts, opts.fixtures.payloads);
630
+ const doc = await open$1(engine, opts, opts.fixtures.payloads);
631
631
  try {
632
632
  const page = (await doc.pages.list()).pages[0];
633
633
  const snapshot = await doc.page(page.pageObjectNumber).annotations.list();
@@ -651,7 +651,7 @@ function runActionsConformance(runner, opts) {
651
651
  }
652
652
  });
653
653
  test("reads a destination-form /OpenAction as openDestination", async () => {
654
- const doc = await open(engine, opts, opts.fixtures.openDestination);
654
+ const doc = await open$1(engine, opts, opts.fixtures.openDestination);
655
655
  try {
656
656
  expect(Boolean(doc.actions)).toBe(true);
657
657
  const snapshot = await doc.actions.read();
@@ -671,7 +671,7 @@ function runActionsConformance(runner, opts) {
671
671
  });
672
672
  });
673
673
  }
674
- async function open(engine, opts, fixture) {
674
+ async function open$1(engine, opts, fixture) {
675
675
  if (opts.openKind === "bytes") return engine.open({
676
676
  kind: "bytes",
677
677
  id: fixture.id,
@@ -1519,6 +1519,287 @@ function runAnnotationMutationConformance(runner, opts) {
1519
1519
  await doc.close();
1520
1520
  }
1521
1521
  });
1522
+ test("rich text: a plain draft reads back body-style paragraphs and a body from its font", async () => {
1523
+ const doc = await openFixture$18(engine, opts);
1524
+ try {
1525
+ const created = await doc.page(fix.pageObjectNumber).annotations.create({
1526
+ subtype: "free-text",
1527
+ intent: "free-text",
1528
+ contents: "Plain\rtext",
1529
+ rect: shapeRect,
1530
+ fontFamily: "helvetica-bold",
1531
+ fontSize: 18,
1532
+ textAlign: "center",
1533
+ color: {
1534
+ r: 0,
1535
+ g: 0,
1536
+ b: 0
1537
+ }
1538
+ });
1539
+ expect(created.created.subtype).toBe("free-text");
1540
+ if (created.created.subtype === "free-text") {
1541
+ const rich = created.created.richText;
1542
+ expect(created.created.fontFamily).toBe("helvetica-bold");
1543
+ expect(rich.body.family).toBe("Helvetica");
1544
+ expect(rich.body.weight).toBe(700);
1545
+ expect(rich.body.italic).toBe(false);
1546
+ expect(rich.body.size).toBe(18);
1547
+ expect(rich.body.align).toBe("center");
1548
+ expect(rich.paragraphs).toEqual([{ runs: [{ text: "Plain" }] }, { runs: [{ text: "text" }] }]);
1549
+ }
1550
+ } finally {
1551
+ await doc.close();
1552
+ }
1553
+ });
1554
+ test("rich text: a draft with runs round-trips the body, the runs and the plain projection", async () => {
1555
+ const doc = await openFixture$18(engine, opts);
1556
+ try {
1557
+ const page = doc.page(fix.pageObjectNumber);
1558
+ const created = await page.annotations.create({
1559
+ subtype: "free-text",
1560
+ intent: "free-text",
1561
+ rect: shapeRect,
1562
+ fontFamily: "helvetica",
1563
+ fontSize: 12,
1564
+ textAlign: "left",
1565
+ color: {
1566
+ r: 0,
1567
+ g: 0,
1568
+ b: 255
1569
+ },
1570
+ richText: {
1571
+ body: {
1572
+ family: "Helvetica",
1573
+ size: 18,
1574
+ color: "#102030"
1575
+ },
1576
+ paragraphs: [{ runs: [
1577
+ { text: "Hello " },
1578
+ {
1579
+ text: "bold",
1580
+ style: { weight: 700 }
1581
+ },
1582
+ {
1583
+ text: " red",
1584
+ style: { color: "#FF0000" }
1585
+ }
1586
+ ] }, {
1587
+ align: "center",
1588
+ runs: [{ text: "H" }, {
1589
+ text: "2",
1590
+ style: { script: "sub" }
1591
+ }]
1592
+ }]
1593
+ }
1594
+ });
1595
+ expect(require_schemas.AnnotationCreateResultSchema.safeParse(created).success).toBe(true);
1596
+ expect(created.created.subtype).toBe("free-text");
1597
+ if (created.created.subtype === "free-text") {
1598
+ const dto = created.created;
1599
+ expect(dto.contents).toBe("Hello bold red\rH2");
1600
+ expect(dto.fontFamily).toBe("helvetica");
1601
+ expect(dto.fontSize).toBe(18);
1602
+ expect(dto.color).toMatchObject({
1603
+ r: 0,
1604
+ g: 0,
1605
+ b: 255
1606
+ });
1607
+ expect(dto.richText.body.color).toBe("#102030");
1608
+ expect(dto.richText.paragraphs[0].runs).toEqual([
1609
+ { text: "Hello " },
1610
+ {
1611
+ text: "bold",
1612
+ style: { weight: 700 }
1613
+ },
1614
+ {
1615
+ text: " red",
1616
+ style: { color: "#FF0000" }
1617
+ }
1618
+ ]);
1619
+ expect(dto.richText.paragraphs[1].align).toBe("center");
1620
+ expect(dto.richText.paragraphs[1].runs[1]).toEqual({
1621
+ text: "2",
1622
+ style: { script: "sub" }
1623
+ });
1624
+ }
1625
+ const listed = (await page.annotations.list()).annotations.find((a) => a.index === created.created.index);
1626
+ expect(listed?.subtype).toBe("free-text");
1627
+ if (listed?.subtype === "free-text") expect(listed.richText.paragraphs[0].runs[1]).toEqual({
1628
+ text: "bold",
1629
+ style: { weight: 700 }
1630
+ });
1631
+ } finally {
1632
+ await doc.close();
1633
+ }
1634
+ });
1635
+ test("rich text: paragraphs-only patch keeps the body; contents-only rewrites body-style paragraphs", async () => {
1636
+ const doc = await openFixture$18(engine, opts);
1637
+ try {
1638
+ const page = doc.page(fix.pageObjectNumber);
1639
+ const ref = (await page.annotations.create({
1640
+ subtype: "free-text",
1641
+ intent: "free-text",
1642
+ rect: shapeRect,
1643
+ fontFamily: "helvetica",
1644
+ fontSize: 14,
1645
+ textAlign: "center",
1646
+ color: {
1647
+ r: 0,
1648
+ g: 0,
1649
+ b: 0
1650
+ },
1651
+ richText: {
1652
+ body: {
1653
+ family: "Helvetica",
1654
+ size: 14
1655
+ },
1656
+ paragraphs: [{ runs: [{ text: "a" }, {
1657
+ text: "b",
1658
+ style: { weight: 700 }
1659
+ }] }]
1660
+ }
1661
+ })).created.ref;
1662
+ const restyled = await page.annotations.update(ref, {
1663
+ subtype: "free-text",
1664
+ richText: { paragraphs: [{ runs: [{
1665
+ text: "ab",
1666
+ style: { italic: true }
1667
+ }, { text: "c" }] }] }
1668
+ });
1669
+ expect(require_schemas.AnnotationUpdateResultSchema.safeParse(restyled).success).toBe(true);
1670
+ expect(restyled.updated.subtype).toBe("free-text");
1671
+ if (restyled.updated.subtype === "free-text") {
1672
+ expect(restyled.updated.contents).toBe("abc");
1673
+ expect(restyled.updated.richText.body.size).toBe(14);
1674
+ expect(restyled.updated.richText.body.align).toBe("center");
1675
+ expect(restyled.updated.textAlign).toBe("center");
1676
+ expect(restyled.updated.richText.paragraphs[0].runs[0]).toEqual({
1677
+ text: "ab",
1678
+ style: { italic: true }
1679
+ });
1680
+ }
1681
+ const rewritten = await page.annotations.update(ref, {
1682
+ subtype: "free-text",
1683
+ contents: "one\rtwo"
1684
+ });
1685
+ expect(rewritten.updated.subtype).toBe("free-text");
1686
+ if (rewritten.updated.subtype === "free-text") {
1687
+ expect(rewritten.updated.contents).toBe("one\rtwo");
1688
+ expect(rewritten.updated.richText.paragraphs).toEqual([{ runs: [{ text: "one" }] }, { runs: [{ text: "two" }] }]);
1689
+ expect(rewritten.updated.richText.body.size).toBe(14);
1690
+ }
1691
+ expect(rewritten.meta.weakRefsInvalidated).toBe(false);
1692
+ } finally {
1693
+ await doc.close();
1694
+ }
1695
+ });
1696
+ test("rich text: fontSize / fontColor / fontFamily move the body while runs keep their deltas", async () => {
1697
+ const doc = await openFixture$18(engine, opts);
1698
+ try {
1699
+ const page = doc.page(fix.pageObjectNumber);
1700
+ const created = await page.annotations.create({
1701
+ subtype: "free-text",
1702
+ intent: "free-text",
1703
+ rect: shapeRect,
1704
+ fontFamily: "helvetica",
1705
+ fontSize: 14,
1706
+ textAlign: "left",
1707
+ color: {
1708
+ r: 0,
1709
+ g: 0,
1710
+ b: 0
1711
+ },
1712
+ richText: {
1713
+ body: {
1714
+ family: "Helvetica",
1715
+ size: 14
1716
+ },
1717
+ paragraphs: [{ runs: [{ text: "a" }, {
1718
+ text: "b",
1719
+ style: { size: 30 }
1720
+ }] }]
1721
+ }
1722
+ });
1723
+ const updated = await page.annotations.update(created.created.ref, {
1724
+ subtype: "free-text",
1725
+ fontSize: 20,
1726
+ fontColor: {
1727
+ r: 255,
1728
+ g: 0,
1729
+ b: 0
1730
+ },
1731
+ fontFamily: "times-bold"
1732
+ });
1733
+ expect(updated.updated.subtype).toBe("free-text");
1734
+ if (updated.updated.subtype === "free-text") {
1735
+ const dto = updated.updated;
1736
+ expect(dto.fontSize).toBe(20);
1737
+ expect(dto.fontFamily).toBe("times-bold");
1738
+ expect(dto.richText.body.size).toBe(20);
1739
+ expect(dto.richText.body.color).toBe("#FF0000");
1740
+ expect(dto.richText.body.family).toBe("Times");
1741
+ expect(dto.richText.body.weight).toBe(700);
1742
+ expect(dto.richText.paragraphs[0].runs[1]).toEqual({
1743
+ text: "b",
1744
+ style: { size: 30 }
1745
+ });
1746
+ }
1747
+ } finally {
1748
+ await doc.close();
1749
+ }
1750
+ });
1751
+ test("rich text: contents and richText that disagree reject with InvalidArg; agreeing writes the runs", async () => {
1752
+ const doc = await openFixture$18(engine, opts);
1753
+ try {
1754
+ const page = doc.page(fix.pageObjectNumber);
1755
+ const created = await page.annotations.create({
1756
+ subtype: "free-text",
1757
+ intent: "free-text",
1758
+ contents: "x",
1759
+ rect: shapeRect,
1760
+ fontFamily: "helvetica",
1761
+ fontSize: 14,
1762
+ textAlign: "left",
1763
+ color: {
1764
+ r: 0,
1765
+ g: 0,
1766
+ b: 0
1767
+ }
1768
+ });
1769
+ const ref = created.created.ref;
1770
+ let caught;
1771
+ try {
1772
+ await page.annotations.update(ref, {
1773
+ subtype: "free-text",
1774
+ contents: "stale",
1775
+ richText: { paragraphs: [{ runs: [{ text: "fresh" }] }] }
1776
+ });
1777
+ } catch (err) {
1778
+ caught = err;
1779
+ }
1780
+ expect(require_fold.EngineError.is(caught, require_PageInsertBlankInput.EngineErrorCode.InvalidArg)).toBe(true);
1781
+ const after = (await page.annotations.list()).annotations.find((a) => a.index === created.created.index);
1782
+ expect(after?.contents).toBe("x");
1783
+ const agreed = await page.annotations.update(ref, {
1784
+ subtype: "free-text",
1785
+ contents: "fresh",
1786
+ richText: { paragraphs: [{ runs: [{ text: "fr" }, {
1787
+ text: "esh",
1788
+ style: { weight: 700 }
1789
+ }] }] }
1790
+ });
1791
+ expect(agreed.updated.subtype).toBe("free-text");
1792
+ if (agreed.updated.subtype === "free-text") {
1793
+ expect(agreed.updated.contents).toBe("fresh");
1794
+ expect(agreed.updated.richText.paragraphs[0].runs[1]).toEqual({
1795
+ text: "esh",
1796
+ style: { weight: 700 }
1797
+ });
1798
+ }
1799
+ } finally {
1800
+ await doc.close();
1801
+ }
1802
+ });
1522
1803
  test("create redact (area + text) round-trips label + colour fields", async () => {
1523
1804
  const doc = await openFixture$18(engine, opts);
1524
1805
  try {
@@ -6675,6 +6956,790 @@ function diffAnnotation(i, a, b, errs) {
6675
6956
  if (a.nm !== b.nm) errs.push(`annotations[${i}].nm mismatch: ${a.nm} vs ${b.nm}`);
6676
6957
  }
6677
6958
  //#endregion
6959
+ //#region src/conformance/runSignatureConformance.ts
6960
+ /**
6961
+ * Digital signatures, read side. The service is OPTIONAL on the contract
6962
+ * (`downloadLayer?` pattern) — the suite runs only where the engine
6963
+ * provides it.
6964
+ *
6965
+ * Invariants:
6966
+ * 1. Byte facts describe the LOADED bytes: a revision is a byte prefix,
6967
+ * a whole-revision signature seals exactly one, the digest over its
6968
+ * `/ByteRange` is what the file's bytes hash to, and an unsaved edit
6969
+ * changes none of it.
6970
+ * 2. Protection is derived from the signatures in the file and applied
6971
+ * as document-derived capabilities: a certification with permission 2
6972
+ * takes page assembly and annotation writes away, a FieldMDP freezes
6973
+ * the fields it names, a rewrite save is refused. Under
6974
+ * `signedDocumentPolicy: 'permit'` none of that applies and the
6975
+ * analysis is unchanged.
6976
+ * 3. The same answers come from a plain session and from a layer
6977
+ * session over the same bytes.
6978
+ */
6979
+ function runSignatureConformance(runner, opts) {
6980
+ const { describe, test, beforeAll, afterAll, expect } = runner;
6981
+ describe(`signatures conformance: ${opts.label}`, () => {
6982
+ let engine;
6983
+ let permitEngine = null;
6984
+ let supported = false;
6985
+ beforeAll(async () => {
6986
+ engine = await opts.makeEngine();
6987
+ const probe = await open(engine, opts, opts.fixtures.unsignedForm);
6988
+ supported = probe.signatures !== void 0;
6989
+ await probe.close();
6990
+ if (supported && opts.makePermitEngine) permitEngine = await opts.makePermitEngine();
6991
+ });
6992
+ afterAll(async () => {
6993
+ if (permitEngine) await permitEngine.destroy();
6994
+ if (engine) await engine.destroy();
6995
+ });
6996
+ test("an unsigned form has no signatures and no protection; version() hashes the loaded bytes", async () => {
6997
+ if (!supported) return;
6998
+ const bytes = await opts.fixtures.unsignedForm.bytes();
6999
+ const doc = await open(engine, opts, opts.fixtures.unsignedForm);
7000
+ try {
7001
+ const snapshot = await doc.signatures.list();
7002
+ expect(snapshot.chainValid).toBe(true);
7003
+ expect(snapshot.signatures).toEqual([]);
7004
+ expect(snapshot.revisions.length >= 1).toBe(true);
7005
+ expect(snapshot.protection.judged).toBeNull();
7006
+ expect(snapshot.protection.enforced).toBeNull();
7007
+ expect(snapshot.protection.certification).toBeNull();
7008
+ expect(snapshot.protection.fieldLocks).toEqual([]);
7009
+ expect(doc.security.allows("doc.pages.assemble")).toBe(true);
7010
+ if (doc.version) {
7011
+ const version = await doc.version();
7012
+ expect(version.byteLength).toBe(bytes.byteLength);
7013
+ expect(version.sha256).toBe(await sha256Hex(bytes));
7014
+ }
7015
+ let caught;
7016
+ try {
7017
+ await doc.signatures.contents({
7018
+ kind: "fqn",
7019
+ name: "no-such-field"
7020
+ });
7021
+ } catch (err) {
7022
+ caught = err;
7023
+ }
7024
+ expect(require_fold.EngineError.is(caught, require_PageInsertBlankInput.EngineErrorCode.NotFound)).toBe(true);
7025
+ } finally {
7026
+ await doc.close();
7027
+ }
7028
+ });
7029
+ test("two approval signatures: revisions, coverage, digests, contents, revision bytes", async () => {
7030
+ if (!supported) return;
7031
+ const fixture = opts.fixtures.twoApprovals;
7032
+ const bytes = await fixture.bytes();
7033
+ const doc = await open(engine, opts, fixture);
7034
+ try {
7035
+ const snapshot = await doc.signatures.list();
7036
+ expect(snapshot.chainValid).toBe(true);
7037
+ expect(snapshot.revisions).toHaveLength(3);
7038
+ expect(snapshot.signatures).toHaveLength(2);
7039
+ expect(snapshot.revisions[2].end).toBe(bytes.byteLength);
7040
+ for (const [i, sig] of snapshot.signatures.entries()) {
7041
+ expect(sig.index).toBe(i);
7042
+ expect(sig.signed).toBe(true);
7043
+ expect(sig.kind).toBe("signature");
7044
+ expect(sig.field.kind).toBe("objectNumber");
7045
+ expect(sig.fieldName).toBe(fixture.fieldNames[i]);
7046
+ expect(sig.coverage).toBe("whole-revision");
7047
+ expect(sig.revisionIndex).toBe(i + 1);
7048
+ expect(snapshot.revisions[i + 1].signatureIndex).toBe(i);
7049
+ expect(sig.subFilter).toBe("ETSI.CAdES.detached");
7050
+ expect(typeof sig.signer.claimedTime).toBe("string");
7051
+ expect(sig.docMdp).toBeNull();
7052
+ expect(sig.catalogCertification).toBe(false);
7053
+ expect(sig.contentsSize > 0).toBe(true);
7054
+ expect(sig.byteRange[0]).toBe(0);
7055
+ expect(sig.byteRange[2] + sig.byteRange[3]).toBe(snapshot.revisions[i + 1].end);
7056
+ }
7057
+ expect(snapshot.protection.judged).toBe("annotate");
7058
+ expect(snapshot.protection.enforced).toBeNull();
7059
+ expect(snapshot.protection.certification).toBeNull();
7060
+ expect(snapshot.protection.fieldLocks).toEqual([]);
7061
+ const first = snapshot.signatures[0];
7062
+ const contents = await doc.signatures.contents(first.field);
7063
+ expect(contents.byteLength).toBe(first.contentsSize);
7064
+ expect(contents[0]).toBe(48);
7065
+ const [a, b, c, d] = first.byteRange;
7066
+ const sealed = await doc.signatures.revisionBytes(1);
7067
+ expect(sealed.byteLength).toBe(snapshot.revisions[1].end);
7068
+ expect(bytesEqual(sealed, bytes.subarray(0, sealed.byteLength))).toBe(true);
7069
+ const signedBytes = concat(sealed.subarray(a, a + b), sealed.subarray(c, c + d));
7070
+ const digest = await doc.signatures.digest(first.field, "sha256");
7071
+ expect(digest.byteLength).toBe(32);
7072
+ expect(toHex(digest)).toBe(await sha256Hex(signedBytes));
7073
+ expect((await doc.signatures.digest(first.field, "sha512")).byteLength).toBe(64);
7074
+ const byName = await doc.signatures.digest({
7075
+ kind: "fqn",
7076
+ name: first.fieldName
7077
+ }, "sha256");
7078
+ expect(toHex(byName)).toBe(toHex(digest));
7079
+ const text = (await doc.forms.list()).fields.find((f) => f.name === fixture.textField);
7080
+ expect(text).toBeTruthy();
7081
+ await doc.forms.setValue({
7082
+ kind: "objectNumber",
7083
+ fieldObjectNumber: text.fieldObjectNumber
7084
+ }, {
7085
+ type: "text",
7086
+ value: "unsaved"
7087
+ });
7088
+ const again = await doc.signatures.list();
7089
+ expect(again.revisions).toEqual(snapshot.revisions);
7090
+ expect(again.signatures.map((s) => [
7091
+ s.coverage,
7092
+ s.revisionIndex,
7093
+ s.byteRange
7094
+ ])).toEqual(snapshot.signatures.map((s) => [
7095
+ s.coverage,
7096
+ s.revisionIndex,
7097
+ s.byteRange
7098
+ ]));
7099
+ expect(toHex(await doc.signatures.digest(first.field, "sha256"))).toBe(toHex(digest));
7100
+ if (doc.version) expect((await doc.version()).sha256).toBe(await sha256Hex(bytes));
7101
+ } finally {
7102
+ await doc.close();
7103
+ }
7104
+ });
7105
+ test("a certification takes structural capabilities away; permit lifts it", async () => {
7106
+ if (!supported) return;
7107
+ const fixture = opts.fixtures.certified;
7108
+ const doc = await open(engine, opts, fixture);
7109
+ try {
7110
+ const snapshot = await doc.signatures.list();
7111
+ const signed = snapshot.signatures.filter((s) => s.signed);
7112
+ expect(signed).toHaveLength(1);
7113
+ const sig = signed[0];
7114
+ expect(sig.fieldName).toBe(fixture.fieldName);
7115
+ expect(sig.coverage).toBe("whole-revision");
7116
+ expect(sig.docMdp).toBe(2);
7117
+ expect(sig.catalogCertification).toBe(true);
7118
+ expect(snapshot.protection.judged).toBe("fill");
7119
+ expect(snapshot.protection.enforced).toBe("fill");
7120
+ expect(snapshot.protection.certification).toEqual({
7121
+ signatureIndex: sig.index,
7122
+ permission: 2
7123
+ });
7124
+ expect(doc.security.allows("doc.forms.read")).toBe(true);
7125
+ expect(doc.security.allows("doc.forms.fill")).toBe(true);
7126
+ expect(doc.security.allows("doc.annotate.modify")).toBe(false);
7127
+ expect(doc.security.allows("doc.pages.assemble")).toBe(false);
7128
+ expect(doc.security.allows("doc.redact")).toBe(false);
7129
+ const list = await doc.pages.list();
7130
+ let caught;
7131
+ try {
7132
+ await doc.pages.delete([list.pages[0].pageObjectNumber]);
7133
+ } catch (err) {
7134
+ caught = err;
7135
+ }
7136
+ expect(require_fold.EngineError.is(caught, require_PageInsertBlankInput.EngineErrorCode.ProtectedDocument)).toBe(true);
7137
+ caught = void 0;
7138
+ try {
7139
+ await doc.download({ mode: "rewrite" });
7140
+ } catch (err) {
7141
+ caught = err;
7142
+ }
7143
+ expect(require_fold.EngineError.is(caught, require_PageInsertBlankInput.EngineErrorCode.ProtectedDocument)).toBe(true);
7144
+ const saved = await doc.download();
7145
+ expect(saved.byteLength >= snapshot.revisions[1].end).toBe(true);
7146
+ } finally {
7147
+ await doc.close();
7148
+ }
7149
+ if (!permitEngine) return;
7150
+ const permitted = await open(permitEngine, opts, fixture);
7151
+ try {
7152
+ const snapshot = await permitted.signatures.list();
7153
+ expect(snapshot.protection.judged).toBe("fill");
7154
+ expect(snapshot.protection.enforced).toBe("fill");
7155
+ expect(permitted.security.allows("doc.pages.assemble")).toBe(true);
7156
+ const rewritten = await permitted.download({ mode: "rewrite" });
7157
+ expect(rewritten.byteLength > 0).toBe(true);
7158
+ } finally {
7159
+ await permitted.close();
7160
+ }
7161
+ });
7162
+ test("a FieldMDP freezes the fields it names; permit lifts it", async () => {
7163
+ if (!supported) return;
7164
+ const fixture = opts.fixtures.fieldMdp;
7165
+ const doc = await open(engine, opts, fixture);
7166
+ const lockedRef = {
7167
+ kind: "fqn",
7168
+ name: fixture.lockedField
7169
+ };
7170
+ try {
7171
+ const snapshot = await doc.signatures.list();
7172
+ const sig = snapshot.signatures[0];
7173
+ expect(sig.fieldName).toBe(fixture.fieldName);
7174
+ expect(sig.fieldMdp).toEqual({
7175
+ action: "include",
7176
+ fields: [fixture.lockedField]
7177
+ });
7178
+ expect(snapshot.protection.judged).toBe("annotate");
7179
+ expect(snapshot.protection.enforced).toBeNull();
7180
+ expect(snapshot.protection.fieldLocks[0]).toEqual({
7181
+ signatureIndex: 0,
7182
+ source: "fieldmdp",
7183
+ spec: {
7184
+ action: "include",
7185
+ fields: [fixture.lockedField]
7186
+ }
7187
+ });
7188
+ for (const lock of snapshot.protection.fieldLocks) {
7189
+ expect(lock.signatureIndex).toBe(0);
7190
+ expect(lock.spec).toEqual({
7191
+ action: "include",
7192
+ fields: [fixture.lockedField]
7193
+ });
7194
+ }
7195
+ expect(doc.security.allows("doc.forms.fill")).toBe(true);
7196
+ let caught;
7197
+ try {
7198
+ await doc.forms.setValue(lockedRef, {
7199
+ type: "text",
7200
+ value: "changed"
7201
+ });
7202
+ } catch (err) {
7203
+ caught = err;
7204
+ }
7205
+ expect(require_fold.EngineError.is(caught, require_PageInsertBlankInput.EngineErrorCode.ProtectedDocument)).toBe(true);
7206
+ const field = await doc.forms.get(lockedRef);
7207
+ expect(field.family).toBe("text");
7208
+ expect(field.value === "changed").toBe(false);
7209
+ } finally {
7210
+ await doc.close();
7211
+ }
7212
+ if (!permitEngine) return;
7213
+ const permitted = await open(permitEngine, opts, fixture);
7214
+ try {
7215
+ const result = await permitted.forms.setValue(lockedRef, {
7216
+ type: "text",
7217
+ value: "changed"
7218
+ });
7219
+ expect(result.field.name).toBe(fixture.lockedField);
7220
+ } finally {
7221
+ await permitted.close();
7222
+ }
7223
+ });
7224
+ test("well-formed ranges that seal no revision are partial, with no revision index", async () => {
7225
+ if (!supported) return;
7226
+ const doc = await open(engine, opts, opts.fixtures.partialChain);
7227
+ try {
7228
+ const snapshot = await doc.signatures.list();
7229
+ expect(snapshot.chainValid).toBe(true);
7230
+ expect(snapshot.revisions).toHaveLength(3);
7231
+ expect(snapshot.signatures).toHaveLength(2);
7232
+ for (const sig of snapshot.signatures) {
7233
+ expect(sig.signed).toBe(true);
7234
+ expect(sig.coverage).toBe("partial");
7235
+ expect(sig.revisionIndex).toBeNull();
7236
+ expect(sig.contentsSize).toBe(13);
7237
+ expect((await doc.signatures.contents(sig.field)).byteLength).toBe(13);
7238
+ expect((await doc.signatures.digest(sig.field, "sha256")).byteLength).toBe(32);
7239
+ }
7240
+ expect(snapshot.revisions.every((r) => r.signatureIndex === null)).toBe(true);
7241
+ expect(snapshot.protection.judged).toBe("annotate");
7242
+ expect(snapshot.protection.enforced).toBeNull();
7243
+ let caught;
7244
+ try {
7245
+ await doc.signatures.revisionBytes(3);
7246
+ } catch (err) {
7247
+ caught = err;
7248
+ }
7249
+ expect(require_fold.EngineError.is(caught, require_PageInsertBlankInput.EngineErrorCode.NotFound)).toBe(true);
7250
+ } finally {
7251
+ await doc.close();
7252
+ }
7253
+ });
7254
+ if (opts.openKind !== "id") {
7255
+ runSigningTests(runner, opts, () => engine, () => supported);
7256
+ runAnalysisTests(runner, opts, () => engine, () => permitEngine, () => supported);
7257
+ }
7258
+ });
7259
+ }
7260
+ function runAnalysisTests(runner, opts, engineOf, permitEngineOf, supported) {
7261
+ const { test, expect } = runner;
7262
+ test("analyze: a form fill and a second signature after an approval are permitted", async () => {
7263
+ if (!supported()) return;
7264
+ const doc = await open(engineOf(), opts, opts.fixtures.twoApprovals);
7265
+ try {
7266
+ const analysis = await doc.signatures.analyze({ since: { signatureIndex: 0 } });
7267
+ expect(analysis.mode).toBe("authoritative");
7268
+ expect(analysis.basis.source).toBe("persisted");
7269
+ expect(analysis.since).toEqual({
7270
+ revisionIndex: 1,
7271
+ signatureIndex: 0
7272
+ });
7273
+ expect(analysis.until.revisionIndex).toBe(2);
7274
+ expect(analysis.steps).toHaveLength(1);
7275
+ const [step] = analysis.steps;
7276
+ expect(step.levelInForce).toBe("annotate");
7277
+ expect(step.verdict).toBe("permitted");
7278
+ expect(step.changes.length > 0).toBe(true);
7279
+ const rules = new Set(step.findings.filter((f) => f.verdict === "permitted").map((f) => f.rule));
7280
+ expect(rules.has("form-fill")).toBe(true);
7281
+ expect(rules.has("signature-added")).toBe(true);
7282
+ expect(step.findings.filter((f) => f.verdict === "forbidden")).toEqual([]);
7283
+ expect(analysis.verdict).toBe("permitted");
7284
+ const latest = await doc.signatures.analyze({ since: { signatureIndex: 1 } });
7285
+ expect(latest.steps).toHaveLength(0);
7286
+ expect(latest.verdict).toBe("unchanged");
7287
+ const historic = await doc.signatures.analyze({
7288
+ since: { revisionIndex: 0 },
7289
+ until: { revisionIndex: 1 }
7290
+ });
7291
+ expect(historic.steps).toHaveLength(1);
7292
+ expect(historic.verdict).toBe("permitted");
7293
+ } finally {
7294
+ await doc.close();
7295
+ }
7296
+ });
7297
+ test("analyze: the working copy is one more revision; a partial signature cannot anchor", async () => {
7298
+ if (!supported()) return;
7299
+ const fx = opts.fixtures.unsignedSigField;
7300
+ const doc = await open(engineOf(), opts, fx);
7301
+ try {
7302
+ const prepared = await doc.signatures.prepare({
7303
+ field: {
7304
+ kind: "fqn",
7305
+ name: fx.fieldName
7306
+ },
7307
+ certify: { permission: 2 }
7308
+ });
7309
+ await doc.signatures.complete({
7310
+ signingId: prepared.signingId,
7311
+ cms: FAKE_CMS,
7312
+ expectedVersion: prepared.expectedVersion
7313
+ });
7314
+ const clean = await doc.signatures.analyze({
7315
+ since: { signatureIndex: 0 },
7316
+ until: "working-copy"
7317
+ });
7318
+ expect(clean.steps).toHaveLength(0);
7319
+ expect(clean.verdict).toBe("unchanged");
7320
+ await doc.forms.setValue({
7321
+ kind: "fqn",
7322
+ name: fx.textField
7323
+ }, {
7324
+ type: "text",
7325
+ value: "draft"
7326
+ });
7327
+ const persisted = await doc.signatures.analyze({ since: { signatureIndex: 0 } });
7328
+ expect(persisted.steps).toHaveLength(0);
7329
+ const working = await doc.signatures.analyze({
7330
+ since: { signatureIndex: 0 },
7331
+ until: "working-copy"
7332
+ });
7333
+ expect(working.basis.source).toBe("working-copy");
7334
+ expect(working.steps).toHaveLength(1);
7335
+ expect(working.steps[0].levelInForce).toBe("fill");
7336
+ expect(working.verdict).toBe("permitted");
7337
+ expect(working.steps[0].findings.some((f) => f.rule === "form-fill" && f.verdict === "permitted")).toBe(true);
7338
+ } finally {
7339
+ await doc.close();
7340
+ }
7341
+ const partial = await open(engineOf(), opts, opts.fixtures.partialChain);
7342
+ try {
7343
+ expect(await caughtCode(() => partial.signatures.analyze({ since: { signatureIndex: 0 } }))).toBe(require_PageInsertBlankInput.EngineErrorCode.InvalidArg);
7344
+ const byRevision = await partial.signatures.analyze({ since: { revisionIndex: 0 } });
7345
+ expect(byRevision.later.revisionCount).toBe(2);
7346
+ expect(byRevision.steps).toHaveLength(0);
7347
+ const inFull = await partial.signatures.analyze({
7348
+ since: { revisionIndex: 0 },
7349
+ detail: "full"
7350
+ });
7351
+ expect(inFull.steps).toHaveLength(2);
7352
+ expect(inFull.current.verdict).toBe(byRevision.current.verdict);
7353
+ } finally {
7354
+ await partial.close();
7355
+ }
7356
+ });
7357
+ test("analyze: a page edit under a certification and a locked field edit are forbidden", async () => {
7358
+ if (!supported()) return;
7359
+ const permit = permitEngineOf();
7360
+ if (!permit) return;
7361
+ const certified = await open(permit, opts, opts.fixtures.certified);
7362
+ let reopened = null;
7363
+ try {
7364
+ const list = await certified.pages.list();
7365
+ await certified.pages.rotate([list.pages[0].pageObjectNumber], 90);
7366
+ const working = await certified.signatures.analyze({
7367
+ since: { signatureIndex: 0 },
7368
+ until: "working-copy"
7369
+ });
7370
+ expect(working.verdict).toBe("forbidden");
7371
+ const bytes = await certified.download();
7372
+ reopened = await reopen(permit, opts, `${opts.fixtures.certified.id}-tampered`, bytes);
7373
+ const analysis = await reopened.signatures.analyze({ since: { signatureIndex: 0 } });
7374
+ expect(analysis.verdict).toBe("forbidden");
7375
+ expect(analysis.steps[0].findings.some((f) => f.verdict === "forbidden")).toBe(true);
7376
+ } finally {
7377
+ if (reopened) await reopened.close();
7378
+ await certified.close();
7379
+ }
7380
+ const locked = await open(permit, opts, opts.fixtures.fieldMdp);
7381
+ try {
7382
+ await locked.forms.setValue({
7383
+ kind: "fqn",
7384
+ name: opts.fixtures.fieldMdp.lockedField
7385
+ }, {
7386
+ type: "text",
7387
+ value: "tampered"
7388
+ });
7389
+ const working = await locked.signatures.analyze({
7390
+ since: { signatureIndex: 0 },
7391
+ until: "working-copy"
7392
+ });
7393
+ expect(working.verdict).toBe("forbidden");
7394
+ expect(working.steps[0].findings.some((f) => f.rule === "field-lock")).toBe(true);
7395
+ } finally {
7396
+ await locked.close();
7397
+ }
7398
+ });
7399
+ }
7400
+ /** A minimal DER SEQUENCE standing in for a CMS: the engine's job ends at the bytes. */
7401
+ const FAKE_CMS = new Uint8Array([
7402
+ 48,
7403
+ 6,
7404
+ 2,
7405
+ 1,
7406
+ 1,
7407
+ 2,
7408
+ 1,
7409
+ 2
7410
+ ]);
7411
+ async function caughtCode(run) {
7412
+ try {
7413
+ await run();
7414
+ return null;
7415
+ } catch (err) {
7416
+ return err instanceof require_fold.EngineError ? err.code : "not-an-engine-error";
7417
+ }
7418
+ }
7419
+ function runSigningTests(runner, opts, engineOf, supported) {
7420
+ const { test, expect } = runner;
7421
+ const fx = () => opts.fixtures.unsignedSigField;
7422
+ const sigRef = () => ({
7423
+ kind: "fqn",
7424
+ name: fx().fieldName
7425
+ });
7426
+ const textRef = () => ({
7427
+ kind: "fqn",
7428
+ name: fx().textField
7429
+ });
7430
+ test("sign: prepare seals a candidate, complete installs it, download returns it verbatim", async () => {
7431
+ if (!supported()) return;
7432
+ const engine = engineOf();
7433
+ const doc = await open(engine, opts, fx());
7434
+ const events = [];
7435
+ const unsubscribe = doc.events.subscribe((event) => events.push(event.type));
7436
+ let reopened = null;
7437
+ try {
7438
+ const v0 = await doc.version();
7439
+ const before = await doc.signatures.list();
7440
+ expect(before.revisions).toHaveLength(1);
7441
+ expect(before.signatures).toHaveLength(1);
7442
+ expect(before.signatures[0].signed).toBe(false);
7443
+ expect(before.protection.judged).toBeNull();
7444
+ const prepared = await doc.signatures.prepare({
7445
+ field: sigRef(),
7446
+ subFilter: "ETSI.CAdES.detached",
7447
+ digest: "sha256",
7448
+ contentsSize: 1024,
7449
+ attribution: { reason: "conformance" }
7450
+ });
7451
+ expect(prepared.digest.byteLength).toBe(32);
7452
+ expect(prepared.algorithm).toBe("sha256");
7453
+ expect(prepared.subFilter).toBe("ETSI.CAdES.detached");
7454
+ expect(prepared.contentsSize).toBe(1024);
7455
+ expect(prepared.byteRange[0]).toBe(0);
7456
+ expect(prepared.byteRange[2] > prepared.byteRange[1]).toBe(true);
7457
+ expect(prepared.expectedVersion.baseSha256).toBe(v0.sha256);
7458
+ expect(await caughtCode(() => doc.signatures.prepare({ field: sigRef() }))).toBe(require_PageInsertBlankInput.EngineErrorCode.SigningPending);
7459
+ expect(await caughtCode(() => doc.forms.setValue(textRef(), {
7460
+ type: "text",
7461
+ value: "x"
7462
+ }))).toBe(require_PageInsertBlankInput.EngineErrorCode.SigningPending);
7463
+ const during = await doc.signatures.list();
7464
+ expect(during.signatures[0].signed).toBe(false);
7465
+ expect((await doc.version()).sha256).toBe(v0.sha256);
7466
+ expect(await caughtCode(() => doc.signatures.complete({
7467
+ signingId: prepared.signingId,
7468
+ cms: FAKE_CMS,
7469
+ expectedVersion: {
7470
+ baseSha256: prepared.expectedVersion.baseSha256,
7471
+ editsVersion: prepared.expectedVersion.editsVersion + 1
7472
+ }
7473
+ }))).toBe(require_PageInsertBlankInput.EngineErrorCode.SigningVersionMismatch);
7474
+ const result = await doc.signatures.complete({
7475
+ signingId: prepared.signingId,
7476
+ cms: FAKE_CMS,
7477
+ expectedVersion: prepared.expectedVersion
7478
+ });
7479
+ expect(result.status).toBe("completed");
7480
+ expect(result.signature.signed).toBe(true);
7481
+ expect(result.signature.fieldName).toBe(fx().fieldName);
7482
+ expect(result.signature.coverage).toBe("whole-revision");
7483
+ expect(result.signature.revisionIndex).toBe(1);
7484
+ expect(result.signature.subFilter).toBe("ETSI.CAdES.detached");
7485
+ expect(result.signature.signer.reason).toBe("conformance");
7486
+ expect(result.signature.contentsSize).toBe(FAKE_CMS.byteLength);
7487
+ expect(result.previous).toEqual(prepared.expectedVersion);
7488
+ expect(result.version.sha256 === v0.sha256).toBe(false);
7489
+ expect(result.protection.judged).toBe("annotate");
7490
+ expect(result.protection.enforced).toBeNull();
7491
+ expect(result.meta.affectedPages.length >= 1).toBe(true);
7492
+ expect(await doc.version()).toEqual(result.version);
7493
+ const after = await doc.signatures.list();
7494
+ expect(after.revisions).toHaveLength(2);
7495
+ expect(after.signatures[0].signed).toBe(true);
7496
+ expect(after.signatures[0].revisionIndex).toBe(1);
7497
+ expect(after.revisions[1].signatureIndex).toBe(0);
7498
+ expect(toHex(await doc.signatures.digest(sigRef(), "sha256"))).toBe(toHex(prepared.digest));
7499
+ expect(bytesEqual(await doc.signatures.contents(sigRef()), FAKE_CMS)).toBe(true);
7500
+ const bytes = await doc.download();
7501
+ expect(bytes.byteLength).toBe(result.version.byteLength);
7502
+ expect(await sha256Hex(bytes)).toBe(result.version.sha256);
7503
+ reopened = await reopen(engine, opts, `${fx().id}-signed`, bytes);
7504
+ const fresh = await reopened.signatures.list();
7505
+ expect(fresh.revisions).toHaveLength(2);
7506
+ expect(fresh.signatures[0].coverage).toBe("whole-revision");
7507
+ expect(toHex(await reopened.signatures.digest(sigRef(), "sha256"))).toBe(toHex(prepared.digest));
7508
+ const replay = await doc.signatures.complete({
7509
+ signingId: prepared.signingId,
7510
+ cms: FAKE_CMS,
7511
+ expectedVersion: prepared.expectedVersion
7512
+ });
7513
+ expect(replay.status).toBe("already-completed");
7514
+ expect(replay.version).toEqual(result.version);
7515
+ expect((await doc.signatures.abort(prepared.signingId)).status).toBe("already-completed");
7516
+ expect((await doc.signatures.abort("never-prepared")).status).toBe("unknown");
7517
+ await doc.forms.setValue(textRef(), {
7518
+ type: "text",
7519
+ value: "after"
7520
+ });
7521
+ expect(await caughtCode(() => doc.signatures.prepare({ field: sigRef() }))).toBe(require_PageInsertBlankInput.EngineErrorCode.SignatureRefused);
7522
+ expect(events.filter((t) => t === "signature.prepared")).toHaveLength(1);
7523
+ expect(events.filter((t) => t === "signature.completed")).toHaveLength(1);
7524
+ expect(events.filter((t) => t === "document.versioned")).toHaveLength(1);
7525
+ } finally {
7526
+ unsubscribe();
7527
+ if (reopened) await reopened.close();
7528
+ await doc.close();
7529
+ }
7530
+ });
7531
+ test("sign: unsaved edits are sealed in the signature's own revision (layer session)", async () => {
7532
+ if (!supported()) return;
7533
+ const doc = await open(engineOf(), opts, fx());
7534
+ try {
7535
+ await doc.forms.setValue(textRef(), {
7536
+ type: "text",
7537
+ value: "unsaved"
7538
+ });
7539
+ const prepared = await doc.signatures.prepare({ field: sigRef() });
7540
+ const result = await doc.signatures.complete({
7541
+ signingId: prepared.signingId,
7542
+ cms: FAKE_CMS,
7543
+ expectedVersion: prepared.expectedVersion
7544
+ });
7545
+ expect(result.signature.revisionIndex).toBe(1);
7546
+ const after = await doc.signatures.list();
7547
+ expect(after.revisions).toHaveLength(2);
7548
+ const text = await doc.forms.get(textRef());
7549
+ expect(text.value).toBe("unsaved");
7550
+ const bytes = await doc.download();
7551
+ expect(await sha256Hex(bytes)).toBe(result.version.sha256);
7552
+ } finally {
7553
+ await doc.close();
7554
+ }
7555
+ });
7556
+ test("sign: a certification protects the document from the moment it completes", async () => {
7557
+ if (!supported()) return;
7558
+ const doc = await open(engineOf(), opts, fx());
7559
+ try {
7560
+ expect(doc.security.allows("doc.pages.assemble")).toBe(true);
7561
+ const prepared = await doc.signatures.prepare({
7562
+ field: sigRef(),
7563
+ certify: { permission: 2 },
7564
+ subFilter: "adbe.pkcs7.detached"
7565
+ });
7566
+ const result = await doc.signatures.complete({
7567
+ signingId: prepared.signingId,
7568
+ cms: FAKE_CMS,
7569
+ expectedVersion: prepared.expectedVersion
7570
+ });
7571
+ expect(result.signature.docMdp).toBe(2);
7572
+ expect(result.signature.catalogCertification).toBe(true);
7573
+ expect(result.protection.judged).toBe("fill");
7574
+ expect(result.protection.enforced).toBe("fill");
7575
+ expect(result.protection.certification).toEqual({
7576
+ signatureIndex: result.signature.index,
7577
+ permission: 2
7578
+ });
7579
+ expect(doc.security.allows("doc.pages.assemble")).toBe(false);
7580
+ expect(doc.security.allows("doc.forms.fill")).toBe(true);
7581
+ const list = await doc.pages.list();
7582
+ expect(await caughtCode(() => doc.pages.delete([list.pages[0].pageObjectNumber]))).toBe(require_PageInsertBlankInput.EngineErrorCode.ProtectedDocument);
7583
+ expect(await caughtCode(() => doc.download({ mode: "rewrite" }))).toBe(require_PageInsertBlankInput.EngineErrorCode.ProtectedDocument);
7584
+ await doc.forms.setValue(textRef(), {
7585
+ type: "text",
7586
+ value: "filled"
7587
+ });
7588
+ const after = await doc.signatures.list();
7589
+ expect(after.signatures[0].coverage).toBe("whole-revision");
7590
+ } finally {
7591
+ await doc.close();
7592
+ }
7593
+ });
7594
+ test("sign: a lock freezes the named fields and is mirrored on the field", async () => {
7595
+ if (!supported()) return;
7596
+ const doc = await open(engineOf(), opts, fx());
7597
+ try {
7598
+ const prepared = await doc.signatures.prepare({
7599
+ field: sigRef(),
7600
+ lock: {
7601
+ action: "include",
7602
+ fields: [fx().textField]
7603
+ }
7604
+ });
7605
+ const result = await doc.signatures.complete({
7606
+ signingId: prepared.signingId,
7607
+ cms: FAKE_CMS,
7608
+ expectedVersion: prepared.expectedVersion
7609
+ });
7610
+ expect(result.signature.fieldMdp).toEqual({
7611
+ action: "include",
7612
+ fields: [fx().textField]
7613
+ });
7614
+ expect(result.signature.lock).toEqual({
7615
+ action: "include",
7616
+ fields: [fx().textField]
7617
+ });
7618
+ expect(result.protection.fieldLocks.length >= 1).toBe(true);
7619
+ expect(await caughtCode(() => doc.forms.setValue(textRef(), {
7620
+ type: "text",
7621
+ value: "nope"
7622
+ }))).toBe(require_PageInsertBlankInput.EngineErrorCode.ProtectedDocument);
7623
+ } finally {
7624
+ await doc.close();
7625
+ }
7626
+ });
7627
+ test("sign: abort discards the candidate and lifts the fence", async () => {
7628
+ if (!supported()) return;
7629
+ const doc = await open(engineOf(), opts, fx());
7630
+ try {
7631
+ const v0 = await doc.version();
7632
+ const prepared = await doc.signatures.prepare({ field: sigRef() });
7633
+ expect((await doc.signatures.abort(prepared.signingId)).status).toBe("aborted");
7634
+ expect((await doc.signatures.list()).signatures[0].signed).toBe(false);
7635
+ expect((await doc.version()).sha256).toBe(v0.sha256);
7636
+ await doc.forms.setValue(textRef(), {
7637
+ type: "text",
7638
+ value: "free again"
7639
+ });
7640
+ expect(await caughtCode(() => doc.signatures.complete({
7641
+ signingId: prepared.signingId,
7642
+ cms: FAKE_CMS,
7643
+ expectedVersion: prepared.expectedVersion
7644
+ }))).toBe(require_PageInsertBlankInput.EngineErrorCode.NotFound);
7645
+ } finally {
7646
+ await doc.close();
7647
+ }
7648
+ });
7649
+ test("sign: artwork from a PDF page lands on the widget", async () => {
7650
+ if (!supported()) return;
7651
+ const doc = await open(engineOf(), opts, fx());
7652
+ try {
7653
+ const artwork = await opts.fixtures.artwork.bytes();
7654
+ const prepared = await doc.signatures.prepare({
7655
+ field: sigRef(),
7656
+ appearance: {
7657
+ pdf: artwork,
7658
+ pageIndex: 0
7659
+ }
7660
+ });
7661
+ const result = await doc.signatures.complete({
7662
+ signingId: prepared.signingId,
7663
+ cms: FAKE_CMS,
7664
+ expectedVersion: prepared.expectedVersion
7665
+ });
7666
+ expect(result.signature.coverage).toBe("whole-revision");
7667
+ expect(result.signature.widget).toBeTruthy();
7668
+ const widget = (await doc.page(result.signature.widget.pageObjectNumber).annotations.list()).annotations.find((a) => a.ref.kind === "objectNumber" && a.ref.annotObjectNumber === result.signature.widget.annotObjectNumber);
7669
+ expect(widget).toBeTruthy();
7670
+ } finally {
7671
+ await doc.close();
7672
+ }
7673
+ });
7674
+ if (opts.openKind === "layerBytes") test("sign: a layer session keeps its kind, with an empty layer over the new base", async () => {
7675
+ if (!supported()) return;
7676
+ const doc = await open(engineOf(), opts, fx());
7677
+ try {
7678
+ const prepared = await doc.signatures.prepare({ field: sigRef() });
7679
+ const result = await doc.signatures.complete({
7680
+ signingId: prepared.signingId,
7681
+ cms: FAKE_CMS,
7682
+ expectedVersion: prepared.expectedVersion
7683
+ });
7684
+ const layer = await doc.downloadLayer();
7685
+ expect(layer.byteLength > 0).toBe(true);
7686
+ expect(await sha256Hex(await doc.download())).toBe(result.version.sha256);
7687
+ } finally {
7688
+ await doc.close();
7689
+ }
7690
+ });
7691
+ }
7692
+ async function reopen(engine, opts, id, bytes) {
7693
+ if (opts.openKind === "layerBytes") return engine.open({
7694
+ kind: "layerBytes",
7695
+ id,
7696
+ baseBytes: bytes
7697
+ });
7698
+ return engine.open({
7699
+ kind: "bytes",
7700
+ id,
7701
+ bytes
7702
+ });
7703
+ }
7704
+ async function open(engine, opts, fixture) {
7705
+ if (opts.openKind === "bytes") return engine.open({
7706
+ kind: "bytes",
7707
+ id: fixture.id,
7708
+ bytes: await fixture.bytes()
7709
+ });
7710
+ if (opts.openKind === "layerBytes") return engine.open({
7711
+ kind: "layerBytes",
7712
+ id: `${fixture.id}-layer`,
7713
+ baseBytes: await fixture.bytes()
7714
+ });
7715
+ return engine.open({
7716
+ kind: "id",
7717
+ id: fixture.cloudId ?? fixture.id
7718
+ });
7719
+ }
7720
+ function concat(a, b) {
7721
+ const out = new Uint8Array(a.byteLength + b.byteLength);
7722
+ out.set(a, 0);
7723
+ out.set(b, a.byteLength);
7724
+ return out;
7725
+ }
7726
+ function bytesEqual(a, b) {
7727
+ if (a.byteLength !== b.byteLength) return false;
7728
+ for (let i = 0; i < a.byteLength; i++) if (a[i] !== b[i]) return false;
7729
+ return true;
7730
+ }
7731
+ function toHex(bytes) {
7732
+ let out = "";
7733
+ for (const b of bytes) out += b.toString(16).padStart(2, "0");
7734
+ return out;
7735
+ }
7736
+ async function sha256Hex(bytes) {
7737
+ const copy = new Uint8Array(bytes.byteLength);
7738
+ copy.set(bytes);
7739
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", copy);
7740
+ return toHex(new Uint8Array(digest));
7741
+ }
7742
+ //#endregion
6678
7743
  exports.TEXT_DIVERGENCE_CASES = TEXT_DIVERGENCE_CASES;
6679
7744
  exports.diffAnnotationListSnapshot = diffAnnotationListSnapshot;
6680
7745
  exports.diffAnnotationListSnapshotAll = diffAnnotationListSnapshotAll;
@@ -6701,6 +7766,7 @@ exports.runPageTextConformance = runPageTextConformance;
6701
7766
  exports.runPieceInfoConformance = runPieceInfoConformance;
6702
7767
  exports.runRedactionApplyConformance = runRedactionApplyConformance;
6703
7768
  exports.runSearchConformance = runSearchConformance;
7769
+ exports.runSignatureConformance = runSignatureConformance;
6704
7770
  exports.runTextDivergenceConformance = runTextDivergenceConformance;
6705
7771
 
6706
7772
  //# sourceMappingURL=conformance.cjs.map