// Pure Templates helper tests: wording (singular/plural, placeholders), the // card mapping and the stage-summary/graph-note derivations. No DOM, no server. import { test } from "node:test"; import assert from "node:assert/strict"; import { CREATE_CANCEL, CREATE_CLOSE, CREATE_CONFIRM, CREATE_NAME_LABEL, CREATE_NOTE, CREATE_SENDING, createConfirmLabel, createDismissLabel, createFormTitle, createFormValid, createSuccessMessage, createdPipelineName, effectiveStageType, stageCountText, templateCard, templateDetailName, templateGalleryName, templateGraphNote, templateStageCountLabel, templateStageSummary, TEMPLATE_GRAPH_NOTE, TEMPLATE_NO_STAGES_NOTE, TEMPLATE_UNPARSED_GRAPH_NOTE, TEMPLATE_UNPARSED_SUMMARY_NOTE, } from "../src/templates.js"; test("stageCountText spells one stage and many stages", () => { assert.equal(stageCountText(1), "1 stage"); assert.equal(stageCountText(2), "2 stages"); assert.equal(stageCountText(0), "0 stages"); }); test("templateStageCountLabel says unavailable for an unknown count, never zero", () => { assert.equal(templateStageCountLabel(null), "stage count unavailable"); assert.equal(templateStageCountLabel(undefined), "stage count unavailable"); assert.equal(templateStageCountLabel(1), "1 stage"); assert.equal(templateStageCountLabel(3), "3 stages"); }); test("a blank gallery name becomes the unnamed placeholder", () => { assert.equal(templateGalleryName(""), "(unnamed template)"); assert.equal(templateGalleryName(" "), "(unnamed template)"); assert.equal(templateGalleryName("Review a repository"), "Review a repository"); }); test("a blank detail name falls back to the template key", () => { assert.equal(templateDetailName({ key: "task", name: "" }), "task"); assert.equal(templateDetailName({ key: "task", name: " " }), "task"); assert.equal(templateDetailName({ key: "task", name: "Complete a task" }), "Complete a task"); }); test("effectiveStageType defaults a blank type to model", () => { assert.equal(effectiveStageType(""), "model"); assert.equal(effectiveStageType(null), "model"); assert.equal(effectiveStageType(" "), "model"); assert.equal(effectiveStageType("human"), "human"); }); test("templateCard takes the stage count from the parsed graph, null when absent", () => { const entry = { key: "task", name: "Complete a task", description: "Do it." }; assert.equal(templateCard(entry, null).stageCount, null); assert.equal(templateCard(entry, { stages: [] }).stageCount, 0); assert.equal(templateCard(entry, { stages: [{ key: "a" }, { key: "b" }] }).stageCount, 2); assert.deepEqual(templateCard(entry, null), { key: "task", name: "Complete a task", description: "Do it.", stageCount: null, }); }); test("templateStageSummary distinguishes unparsed, empty and populated", () => { assert.deepEqual(templateStageSummary(null), { note: TEMPLATE_UNPARSED_SUMMARY_NOTE, stages: [], }); assert.deepEqual(templateStageSummary({ stages: [] }), { note: TEMPLATE_NO_STAGES_NOTE, stages: [], }); const summary = templateStageSummary({ stages: [{ key: "plan", type: "" }, { key: "review", type: "human" }] }); assert.equal(summary.note, null); assert.deepEqual(summary.stages, [ { key: "plan", type: "model" }, { key: "review", type: "human" }, ]); }); test("templateGraphNote picks the unparsed or parsed note", () => { assert.equal(templateGraphNote(null), TEMPLATE_UNPARSED_GRAPH_NOTE); assert.equal(templateGraphNote({ stages: [] }), TEMPLATE_GRAPH_NOTE); }); test("createFormTitle names the template, a blank name falling back to the key", () => { assert.equal(createFormTitle({ key: "task", name: "Complete a task" }), "Create from Complete a task"); assert.equal(createFormTitle({ key: "task", name: "" }), "Create from task"); assert.equal(createFormTitle({ key: "task", name: " " }), "Create from task"); }); test("createFormValid rejects blank/whitespace, sending and an existing creation", () => { assert.equal(createFormValid("a pipeline"), true); assert.equal(createFormValid(""), false); assert.equal(createFormValid(" "), false); assert.equal(createFormValid(null), false); assert.equal(createFormValid("a pipeline", { sending: true }), false); assert.equal(createFormValid("a pipeline", { created: "a pipeline" }), false); assert.equal(createFormValid("a pipeline", { created: "" }), false); // Defaults: a non-blank name with no options is valid. assert.equal(createFormValid("a pipeline", {}), true); }); test("createConfirmLabel switches Create / Creating…", () => { assert.equal(createConfirmLabel(false), CREATE_CONFIRM); assert.equal(createConfirmLabel(true), CREATE_SENDING); }); test("createDismissLabel is Cancel until a creation, then Close", () => { assert.equal(createDismissLabel(null), CREATE_CANCEL); assert.equal(createDismissLabel(undefined), CREATE_CANCEL); assert.equal(createDismissLabel("Made"), CREATE_CLOSE); }); test("createSuccessMessage is the exact success line", () => { assert.equal(createSuccessMessage("Release helper"), 'Created "Release helper". It is in the Pipelines list.'); }); test("createdPipelineName prefers the returned name, else the trimmed submitted name", () => { assert.equal(createdPipelineName({ name: "Server name" }, "Typed name"), "Server name"); assert.equal(createdPipelineName({ name: " " }, " Typed name "), "Typed name"); assert.equal(createdPipelineName(null, " Typed name "), "Typed name"); assert.equal(createdPipelineName({}, ""), ""); }); test("the create dialog's fixed wording is the app's", () => { assert.equal(CREATE_NAME_LABEL, "New pipeline name (required)"); assert.equal( CREATE_NOTE, "Creates a new draft pipeline on the server from this template's source; it is not run.", ); assert.equal(CREATE_CONFIRM, "Create"); assert.equal(CREATE_SENDING, "Creating…"); assert.equal(CREATE_CANCEL, "Cancel"); assert.equal(CREATE_CLOSE, "Close"); });