Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript
3.4 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";

import {
  fieldPlacementSchema,
  replaceFieldsResponseSchema,
  signingFieldSubmissionSchema,
} from "./field.ts";

const placement = {
  height: 0.05,
  page: 1,
  signerId: "signer-1",
  width: 0.2,
  x: 0.1,
  y: 0.1,
};

test("fields that fill themselves in or are the signature are always required", () => {
  for (const type of ["signature", "initials", "date", "name"] as const) {
    assert.equal(
      fieldPlacementSchema.safeParse({ ...placement, required: false, type })
        .success,
      false,
      `an optional ${type} field was accepted`,
    );
    assert.equal(
      fieldPlacementSchema.safeParse({ ...placement, type }).success,
      true,
      `a ${type} field with omitted required was refused`,
    );
    assert.equal(
      fieldPlacementSchema.safeParse({ ...placement, required: true, type })
        .success,
      true,
      `a ${type} field with required: true was refused`,
    );
  }
});

test("interactive non-signature fields may be optional", () => {
  for (const candidate of [
    { ...placement, required: false, type: "text" as const },
    { ...placement, required: false, type: "checkbox" as const },
    { ...placement, required: false, type: "number" as const },
    {
      ...placement,
      options: ["Yes", "No"],
      required: false,
      type: "dropdown" as const,
    },
    {
      ...placement,
      groupId: "choice",
      required: false,
      type: "radio" as const,
    },
  ]) {
    assert.equal(
      fieldPlacementSchema.safeParse(candidate).success,
      true,
      `an optional ${candidate.type} field was refused`,
    );
  }
});

test("dropdown choices accept 80-code-point emoji strings and refuse lone surrogates", () => {
  const eightyWithEmoji = `${"a".repeat(79)}πŸ˜€`;
  const eightyOneWithEmoji = `${"a".repeat(80)}πŸ˜€`;
  const loneHighSurrogate = `${"a".repeat(79)}\ud83d`;
  const loneLowSurrogate = `${"a".repeat(79)}\ude00`;

  assert.equal(
    fieldPlacementSchema.safeParse({
      ...placement,
      options: [eightyWithEmoji, "No"],
      type: "dropdown",
    }).success,
    true,
  );

  assert.equal(
    signingFieldSubmissionSchema.safeParse({
      fieldId: "field-1",
      type: "dropdown",
      value: eightyWithEmoji,
    }).success,
    true,
  );

  assert.equal(
    replaceFieldsResponseSchema.safeParse({
      envelopeId: "env-1",
      fields: [
        {
          ...placement,
          id: "field-1",
          options: [eightyWithEmoji, "No"],
          type: "dropdown",
        },
      ],
      fieldsRevision: 1,
      redirectPath: "/envelopes/env-1/review",
      updatedAt: 1700000000,
    }).success,
    true,
  );

  assert.equal(
    fieldPlacementSchema.safeParse({
      ...placement,
      options: [eightyOneWithEmoji],
      type: "dropdown",
    }).success,
    false,
  );

  for (const broken of [loneHighSurrogate, loneLowSurrogate]) {
    assert.equal(
      fieldPlacementSchema.safeParse({
        ...placement,
        options: [broken],
        type: "dropdown",
      }).success,
      false,
    );

    assert.equal(
      signingFieldSubmissionSchema.safeParse({
        fieldId: "field-1",
        type: "dropdown",
        value: broken,
      }).success,
      false,
    );

    assert.equal(
      fieldPlacementSchema.safeParse({
        ...placement,
        groupId: "group-1",
        label: broken,
        type: "radio",
      }).success,
      false,
    );
  }
});