/** * The field tools as a table: the built-in ones, in their two families (the form tools, which * place a box, and the signing tools, which place a field with no look of its own that is * required), what the `tools` setting makes of them, and what a tool with a purpose or a format * places. Every tool that was there before places what it placed. */ import type { PageRef } from '@embedpdf/engine-core/runtime'; import { toPageRef } from '@embedpdf/engine-core/runtime'; import { describe, expect, it } from 'vitest'; import { FORM_TOOL_BY_ID, FORM_TOOLS, resolveFormTools } from '../src/tools/definitions'; import { toolDraft } from '../src/tools/handlers'; const PAGE: PageRef = toPageRef(3); const WIDGET = { page: PAGE, rect: { x: 10, y: 10, width: 160, height: 24 } }; describe('the field tools', () => { it('keeps the seven tools it had, adds the image tool and the seven signing tools', () => { expect(FORM_TOOLS.map((tool) => tool.id)).toEqual([ 'form-text', 'form-checkbox', 'form-radio', 'form-combobox', 'form-listbox', 'form-button', 'form-signature', 'form-image', 'signer-signature', 'signer-initials', 'signer-date-signed', 'signer-name', 'signer-email', 'signer-organization', 'signer-organization-title', ]); // The first six say nothing about what their fields start with; the signature tool's // fields say what they're for. for (const tool of FORM_TOOLS.slice(0, 6)) expect(tool).not.toHaveProperty('field'); expect(toolDraft(FORM_TOOL_BY_ID.get('form-text')!, 'text_1', WIDGET)).toEqual({ kind: 'text', name: 'text_1', widgets: [WIDGET], }); expect(toolDraft(FORM_TOOL_BY_ID.get('form-signature')!, 'signature_1', WIDGET)).toEqual({ kind: 'signature', name: 'signature_1', widgets: [WIDGET], purpose: 'signature', }); }); it('a form tool places a box of its own that is not required, the seven released ones as they were', () => { const BOX = { interiorColor: '#ffffff', color: '#6b7280', strokeWidth: 1 }; const tool = { upright: true, cursor: 'crosshair' }; // Seven of these are released with this look and size: an app that upgrades places the box // it placed. (The signature tool's purpose came with the image fields.) expect(FORM_TOOLS.filter((each) => each.id.startsWith('form-'))).toEqual([ { ...tool, id: 'form-text', fieldKind: 'text', visualKind: 'widget-text', clickCreate: { width: 160, height: 24 }, defaults: { ...BOX, fontSize: 12 }, }, { ...tool, id: 'form-checkbox', fieldKind: 'checkbox', visualKind: 'widget-toggle', clickCreate: { width: 18, height: 18 }, defaults: BOX, }, { ...tool, id: 'form-radio', fieldKind: 'radio', visualKind: 'widget-radio', clickCreate: { width: 18, height: 18 }, defaults: BOX, }, { ...tool, id: 'form-combobox', fieldKind: 'combobox', visualKind: 'widget-choice', clickCreate: { width: 140, height: 24 }, defaults: { ...BOX, fontSize: 12 }, }, { ...tool, id: 'form-listbox', fieldKind: 'listbox', visualKind: 'widget-choice', clickCreate: { width: 140, height: 72 }, defaults: { ...BOX, fontSize: 12 }, }, { ...tool, id: 'form-button', fieldKind: 'pushbutton', visualKind: 'widget-button', clickCreate: { width: 100, height: 28 }, defaults: { ...BOX, interiorColor: '#e5e7eb', fontSize: 12 }, }, { ...tool, id: 'form-signature', fieldKind: 'signature', visualKind: 'widget-box', clickCreate: { width: 160, height: 48 }, defaults: BOX, field: { purpose: 'signature' }, }, { ...tool, id: 'form-image', fieldKind: 'pushbutton', visualKind: 'widget-button', clickCreate: { width: 120, height: 90 }, defaults: { ...BOX, layout: 'image' }, field: { takesImage: true }, }, ]); for (const each of FORM_TOOLS.filter((one) => one.id.startsWith('form-'))) { expect({ id: each.id, required: toolDraft(each, 'field_1', WIDGET).required }).toEqual({ id: each.id, required: undefined, }); } }); it('a signing tool places a field with no border and no background, required but for the day of signing', () => { const signing = FORM_TOOLS.filter((tool) => tool.id.startsWith('signer-')); // The two families are every built-in tool: none is in both, none in neither. expect(signing.length + FORM_TOOLS.filter((tool) => tool.id.startsWith('form-')).length).toBe( FORM_TOOLS.length, ); for (const tool of signing) { const { id } = tool; // No color is no border, whatever its width: the tool names none. expect({ id, look: tool.defaults }).toMatchObject({ id, look: { color: null, interiorColor: null }, }); expect({ id, strokeWidth: 'strokeWidth' in tool.defaults }).toEqual({ id, strokeWidth: false, }); // Its id is `signer-` and the purpose its fields have. expect({ id, purpose: `signer-${tool.field?.purpose}` }).toEqual({ id, purpose: id }); expect({ id, required: toolDraft(tool, 'field_1', WIDGET).required }).toEqual({ id, required: id === 'signer-date-signed' ? undefined : true, }); } }); it('the signing signature tool places the field the form one places, with the signing look and required', () => { const form = FORM_TOOL_BY_ID.get('form-signature')!; const signer = FORM_TOOL_BY_ID.get('signer-signature')!; expect(signer).toEqual({ ...form, id: 'signer-signature', defaults: { color: null, interiorColor: null }, field: { purpose: 'signature', required: true }, }); const placed = { ...WIDGET, ...signer.defaults }; expect(toolDraft(signer, 'signature_1', placed)).toEqual({ kind: 'signature', name: 'signature_1', widgets: [placed], purpose: 'signature', required: true, }); // One signer signs it: placed for anyone, it is nobody's, as the form tool's is. expect(toolDraft(signer, 'signature_1', placed, { kind: 'anyone' })).not.toHaveProperty( 'assignment', ); }); it('places an image field: a push button that takes an image, in a box that shows it alone, with no caption', () => { const initials = FORM_TOOL_BY_ID.get('signer-initials')!; const image = FORM_TOOL_BY_ID.get('form-image')!; expect(initials).toMatchObject({ fieldKind: 'pushbutton', clickCreate: { width: 72, height: 36 }, defaults: { layout: 'image' }, }); expect(initials.field).toEqual({ takesImage: true, purpose: 'initials', required: true }); expect(image).toMatchObject({ fieldKind: 'pushbutton', clickCreate: { width: 120, height: 90 }, defaults: { layout: 'image' }, }); expect(image.field).toEqual({ takesImage: true }); const placed = { ...WIDGET, layout: 'image' as const }; expect(toolDraft(initials, 'pushbutton_1', placed)).toEqual({ kind: 'pushbutton', name: 'pushbutton_1', widgets: [placed], takesImage: true, purpose: 'initials', required: true, }); // The two differ in their family alone: the look, "required", and the purpose and size. expect(initials).toEqual({ ...image, id: 'signer-initials', clickCreate: { width: 72, height: 36 }, defaults: { color: null, interiorColor: null, layout: 'image' }, field: { takesImage: true, purpose: 'initials', required: true }, }); expect(toolDraft(image, 'pushbutton_2', placed)).toEqual({ kind: 'pushbutton', name: 'pushbutton_2', widgets: [placed], takesImage: true, }); // A button that isn't an image field keeps its caption. expect(toolDraft(FORM_TOOL_BY_ID.get('form-button')!, 'pushbutton_3', WIDGET)).toEqual({ kind: 'pushbutton', name: 'pushbutton_3', widgets: [{ ...WIDGET, caption: 'Button' }], }); }); it('an app’s tool made from an image tool places an image field; `takesImage` is a push button’s', () => { const tools = resolveFormTools([ { id: 'form-photo', extends: 'form-image', field: { purpose: 'photo' } }, // An image field in a box that shows a caption too: it still gets none of the tool's. { id: 'form-badge', extends: 'form-image', defaults: { layout: 'caption-below' } }, // An app may make a plain picture button of it. { id: 'form-logo', extends: 'form-image', field: { takesImage: false } }, ]); expect(tools.get('form-photo')!.field).toEqual({ takesImage: true, purpose: 'photo' }); expect(toolDraft(tools.get('form-photo')!, 'pushbutton_1', WIDGET)).toMatchObject({ takesImage: true, purpose: 'photo', }); const below = { ...WIDGET, layout: 'caption-below' as const }; expect(toolDraft(tools.get('form-badge')!, 'pushbutton_2', below)).toEqual({ kind: 'pushbutton', name: 'pushbutton_2', widgets: [below], takesImage: true, }); expect(toolDraft(tools.get('form-logo')!, 'pushbutton_3', WIDGET)).toMatchObject({ takesImage: false, }); // The engine's own refusal: no other kind of field takes an image. expect(() => resolveFormTools([ { id: 'form-picture-text', extends: 'form-text', field: { takesImage: true } as never }, ]), ).toThrow(/form-picture-text.*takesImage/); }); it('a value tool is the text tool with a purpose, required, and the signing look: the same size and kind', () => { const text = FORM_TOOL_BY_ID.get('form-text')!; for (const purpose of ['name', 'email', 'organization', 'organization-title']) { const tool = FORM_TOOL_BY_ID.get(`signer-${purpose}`)!; expect(tool).toEqual({ ...text, id: `signer-${purpose}`, defaults: { color: null, interiorColor: null, fontSize: 12 }, field: { purpose, required: true }, }); const buyer = { kind: 'group', groupId: 'buyer' } as const; expect(toolDraft(tool, 'text_1', WIDGET, buyer)).toEqual({ kind: 'text', name: 'text_1', widgets: [WIDGET], purpose, required: true, assignment: buyer, }); } // The day of signing has the look and no "required": nobody is asked for it. expect(FORM_TOOL_BY_ID.get('signer-date-signed')!.defaults).toEqual({ color: null, interiorColor: null, fontSize: 12, }); expect(toolDraft(FORM_TOOL_BY_ID.get('signer-date-signed')!, 'text_1', WIDGET)).toEqual({ kind: 'text', name: 'text_1', widgets: [WIDGET], purpose: 'date-signed', format: { kind: 'date', pattern: 'yyyy-mm-dd' }, readOnly: true, }); expect(FORM_TOOL_BY_ID.get('signer-date-signed')!.clickCreate).toEqual({ width: 100, height: 24, }); }); it('with no entries the document’s tools are the built-in ones, the same objects', () => { const tools = resolveFormTools(); expect([...tools.keys()]).toEqual(FORM_TOOLS.map((tool) => tool.id)); for (const tool of FORM_TOOLS) expect(tools.get(tool.id)).toBe(tool); }); it('an entry with `extends` adds a tool, and one without changes the tool that has its id', () => { const tools = resolveFormTools([ { id: 'form-company-number', extends: 'form-text', field: { purpose: 'company-number', required: true }, defaults: { color: '#1d4ed8' }, }, // A tool listed before it: it starts from the app's own. { id: 'form-vat', extends: 'form-company-number', field: { purpose: 'vat' } }, { id: 'signer-date-signed', field: { format: { kind: 'date', pattern: 'dd/mm/yyyy' } } }, { id: 'form-checkbox', clickCreate: { width: 14, height: 14 } }, ]); const text = FORM_TOOL_BY_ID.get('form-text')!; expect(tools.get('form-company-number')).toEqual({ ...text, id: 'form-company-number', defaults: { ...text.defaults, color: '#1d4ed8' }, field: { purpose: 'company-number', required: true }, }); // What an entry gives goes over what the tool had, member by member. expect(tools.get('form-vat')!.field).toEqual({ purpose: 'vat', required: true }); expect(tools.get('form-vat')!.defaults.color).toBe('#1d4ed8'); expect(tools.get('signer-date-signed')!.field).toEqual({ purpose: 'date-signed', format: { kind: 'date', pattern: 'dd/mm/yyyy' }, readOnly: true, }); expect(tools.get('form-checkbox')!.clickCreate).toEqual({ width: 14, height: 14 }); // The built-in table is not written to, and the tools nobody named are the same objects. expect(FORM_TOOL_BY_ID.get('form-checkbox')!.clickCreate).toEqual({ width: 18, height: 18 }); expect(tools.get('form-text')).toBe(text); expect([...tools.keys()].slice(-2)).toEqual(['form-company-number', 'form-vat']); }); it('an app’s tool takes either family’s look: a box over a signing tool, none over a form tool', () => { const tools = resolveFormTools([ // A signing tool for a value of the app's own: the text tool without its box, required. { id: 'signer-company-number', extends: 'form-text', field: { purpose: 'company-number', required: true }, defaults: { color: null, interiorColor: null }, }, // A signing field that keeps a border of its own, and one that isn't required. { id: 'signer-name', defaults: { color: '#1d4ed8' } }, { id: 'signer-email', field: { required: false } }, ]); expect(tools.get('signer-company-number')!.defaults).toEqual({ color: null, interiorColor: null, strokeWidth: 1, fontSize: 12, }); expect(tools.get('signer-name')!.defaults).toEqual({ color: '#1d4ed8', interiorColor: null, fontSize: 12, }); expect(tools.get('signer-name')!.field).toEqual({ purpose: 'name', required: true }); expect(tools.get('signer-email')!.field).toEqual({ purpose: 'email', required: false }); }); it('refuses an entry that names no tool, and a field no field of its kind can have', () => { expect(() => resolveFormTools([{ id: 'form-mine' }])).toThrowError( /'form-mine' is not a field tool: give it `extends`/, ); expect(() => resolveFormTools([{ id: 'form-mine', extends: 'form-nothing' }])).toThrowError( /extends 'form-nothing', which isn't a field tool listed before it/, ); expect(() => resolveFormTools([ { id: 'form-first', extends: 'form-second' }, { id: 'form-second', extends: 'form-text' }, ]), ).toThrowError(/'form-first' extends 'form-second'/); // `extends` adds a tool: under the id of a tool there is, it would replace that one unasked. expect(() => resolveFormTools([{ id: 'form-text', extends: 'form-checkbox' }])).toThrowError( /'form-text' extends 'form-checkbox', but 'form-text' is a field tool already: leave `extends` out to change it/, ); expect(() => resolveFormTools([ { id: 'form-mine', extends: 'form-text' }, { id: 'form-mine', extends: 'form-text', field: { purpose: 'mine' } }, ]), ).toThrowError(/'form-mine' is a field tool already/); expect(() => resolveFormTools([{ id: 'signer-name', extends: 'signer-name' }])).toThrowError( /'signer-name' is a field tool already/, ); expect(() => resolveFormTools([ { id: 'form-tick-date', extends: 'form-checkbox', field: { format: { kind: 'date', pattern: 'dd/mm/yyyy' } } as never, }, ]), ).toThrowError(/tool 'form-tick-date': 'format' does not apply to a checkbox field/); expect(() => resolveFormTools([ { id: 'form-bad-date', extends: 'form-text', field: { format: { kind: 'date', pattern: 'yyyymmdd' } }, }, ]), ).toThrowError(/tool 'form-bad-date':/); expect(() => resolveFormTools([{ id: 'form-padded', extends: 'form-text', field: { purpose: ' x ' } }]), ).toThrowError(/tool 'form-padded': a field's purpose is 1 to 256 characters/); expect(() => resolveFormTools([{ id: 'form-hint', extends: 'form-text', field: { placeholder: ' x ' } }]), ).toThrowError(/tool 'form-hint': a field's placeholder is 1 to 256 characters/); }); it('a tool gives its fields a placeholder, over the tool it extends, and keeps the rest', () => { const tools = resolveFormTools([ { id: 'form-iban', extends: 'form-text', field: { placeholder: 'NL12 ABCD 0123 4567 89' } }, // A built-in tool with a purpose takes one too: its purpose stays. { id: 'signer-email', field: { placeholder: 'name@example.com' } }, { id: 'form-iban-be', extends: 'form-iban', field: { placeholder: 'BE68 5390 0754 7034' } }, ]); expect(tools.get('form-iban')!.field).toEqual({ placeholder: 'NL12 ABCD 0123 4567 89' }); expect(tools.get('signer-email')!.field).toEqual({ purpose: 'email', required: true, placeholder: 'name@example.com', }); expect(tools.get('form-iban-be')!.field).toEqual({ placeholder: 'BE68 5390 0754 7034' }); expect(FORM_TOOL_BY_ID.get('signer-email')!.field).toEqual({ purpose: 'email', required: true, }); }); });