import { describe, expect, test } from "vitest"; import { createColumnValueId, createGlobalPObjectId, type ColumnLink, type ColumnValue, type LinkCheck, } from "@platforma-sdk/model"; import { ALERTS, blockName, describeMissingValue, linkAlerts, linkSections, type LinkSection, } from "./columnLinkViews"; const REF = createGlobalPObjectId("producer", "out"); describe("linkSections", () => { test("yields both sides, also when they have no links", () => { const sections = linkSections({ dependents: [], dependencies: [], }); expect(sections.map((section) => [section.side, section.rows])).toEqual([ ["usedBy", []], ["uses", []], ]); }); test("merges picks of one block from one output, keeping a whole-column use apart", () => { const [usedBy] = linkSections({ dependents: [ dependent("b1", "out", ["1", "a"]), dependent("b1", "out", ["1", "b"]), dependent("b1", "out", null), dependent("b2", "out", ["a"]), ], dependencies: [], }); expect(usedBy.rows.map((row) => [blockName(LABELS, row.blockId), row.values])).toEqual([ ["Enrichment", ["1", "a", "b"]], ["Enrichment", null], ["Block b2", ["a"]], ]); }); test("keeps the column label, and the output name apart from it", () => { const [, uses] = linkSections({ dependents: [], dependencies: [ dependency("p1", "out1", null, { type: "unavailable" }), dependency("p1", "out2", null, { type: "ok" }, "Condition"), ], }); expect(uses.rows.map((row) => [row.column.name, row.columnLabel])).toEqual([ ["out1", null], ["out2", "Condition"], ]); }); test("takes a label from any merged reference", () => { const [, uses] = linkSections({ dependents: [], dependencies: [ dependency("p1", "out", ["a"], { type: "pending" }), dependency("p1", "out", ["b"], { type: "ok" }, "Condition"), ], }); expect(uses.rows[0].columnLabel).toBe("Condition"); }); test("merged references take the status needing attention most, uniting missing values", () => { const [, uses] = linkSections({ dependents: [], dependencies: [ dependency("p1", "out", ["a"], { type: "ok" }), dependency("p1", "out", ["b"], { type: "unreadable" }), dependency("p1", "out", ["c"], { type: "pending" }), dependency("p2", "out", ["a"], missingValues(["1"])), dependency("p2", "out", ["b"], missingValues(["1", "b"])), dependency("p2", "out", ["c"], { type: "ok" }), dependency("p3", "out", ["a"], missingValues(["a"])), dependency("p3", "out", ["b"], { type: "unavailable" }), ], }); expect(uses.rows.map((row) => row.check)).toEqual([ { type: "pending" }, { type: "missingValues", values: ["1", "b"] }, { type: "unavailable" }, ]); }); test("missing values of a dependent block are listed as they are", () => { const [usedBy] = linkSections({ dependents: [dependent("b1", "out", ["a"], { type: "missingValues", values: ["2"] })], dependencies: [], }); expect(usedBy.rows[0].check).toEqual({ type: "missingValues", values: ["2"] }); }); test("an output another block uses and this block does not export is not exported", () => { const [usedBy] = linkSections({ dependents: [dependent("b1", "out", ["a"], notExported())], dependencies: [], }); expect(usedBy.rows.map((row) => row.check)).toEqual([{ type: "unavailable" }]); }); test("keeps the reason a link was not checked on both sides", () => { const [usedBy, uses] = linkSections({ dependents: [dependent("b1", "out", ["a"], { type: "unreadable" })], dependencies: [dependency("p1", "out", ["a"], { type: "unreadable" })], }); const statuses = [{ type: "unreadable" }]; expect(usedBy.rows.map((row) => row.check)).toEqual(statuses); expect(uses.rows.map((row) => row.check)).toEqual(statuses); }); test("an output of a block not run keeps that state on both sides", () => { const [usedBy, uses] = linkSections({ dependents: [dependent("b1", "out", ["a"], { type: "notRun" })], dependencies: [dependency("p1", "out", ["a"], { type: "notRun" })], }); expect(usedBy.rows.map((row) => row.check)).toEqual([{ type: "notRun" }]); expect(uses.rows.map((row) => row.check)).toEqual([{ type: "notRun" }]); }); }); describe("linkAlerts", () => { test("lists only links that are broken or lack values, one alert per side", () => { const sections = linkSections({ dependents: [ dependent("b1", "out", ["x", "y"], { type: "missingValues", values: ["x", "y"] }), dependent("b1", "other", ["x"], notExported()), dependent("b1", "third", ["x"], { type: "unreadable" }), ], dependencies: [ dependency("p1", "out1", null, { type: "unavailable" }), dependency("p1", "out2", ["a"], missingValues(["a"]), "Condition"), dependency("p1", "out3", ["a"], { type: "pending" }), dependency("p1", "out4", null, { type: "ok" }), ], }); expect( linkAlerts(sections).map((alert) => [ alert.side, ALERTS[alert.side].label, ALERTS[alert.side].hint, alert.issues.map((issue) => `${blockName(LABELS, issue.blockId)} ${issue.text}`), ]), ).toEqual([ [ "usedBy", "Some blocks are missing data from this block", "Checked against the last run of this block. Add the missing data and run it, or " + "reconfigure those blocks.", [ 'Enrichment uses "x", "y", which this block\'s data no longer has', "Enrichment uses data this block does not export", ], ], [ "uses", "Links to blocks this block takes data from are broken", null, ["Samples does not provide data this block uses", 'Samples has no "a" in "Condition"'], ], ]); }); test("names a column by its quoted label", () => { const alerts = linkAlerts( linkSections({ dependents: [ dependent("b1", "out", ["x"], notExported(), "Clonotypes"), dependent("b1", "other", ["x"], { type: "missingValues", values: ["x"] }, "Samples"), ], dependencies: [ dependency("p1", "out", null, { type: "unavailable" }, "Condition"), dependency("p1", "other", ["a"], missingValues(["a"]), "Condition"), ], }), ); expect(alerts.map((alert) => alert.issues.map((issue) => issue.text))).toEqual([ [ 'uses "Clonotypes", which this block does not export', 'uses "x" of "Samples", which this block no longer has', ], ['does not provide "Condition"', 'has no "a" in "Condition"'], ]); }); test("says the links are checked against the last run of this block", () => { const [usedBy] = linkAlerts( linkSections({ dependents: [dependent("b1", "out", ["a"], notExported())], dependencies: [], }), ); expect(usedBy?.side).toBe("usedBy"); expect(ALERTS.usedBy.hint).toBe( "Checked against the last run of this block. Add the missing data and run it, or " + "reconfigure those blocks.", ); }); test("never names an output by its name, which is kept for a hint", () => { const [usedBy, uses] = linkAlerts( linkSections({ dependents: [ dependent("b1", "pf.dataset.X", null, notExported()), dependent("b1", "pf.metadata.Y", ["a"], { type: "missingValues", values: ["a"] }), ], dependencies: [ dependency("p1", "pf.dataset.X", null, { type: "unavailable" }), dependency("p1", "pf.metadata.Y", ["a"], missingValues(["a"])), ], }), ); const issues = [...usedBy.issues, ...uses.issues]; for (const issue of issues) expect(issue.text).not.toMatch(/pf\./); expect(issues.map((issue) => issue.outputs)).toEqual([ ["pf.dataset.X"], ["pf.metadata.Y"], ["pf.dataset.X"], ["pf.metadata.Y"], ]); }); test("outputs of one block that are not exported and have no label share a line", () => { const [usedBy, uses] = linkAlerts( linkSections({ dependents: [ dependent("b1", "out1", ["a"], notExported()), dependent("b1", "out1", null, notExported()), dependent("b2", "out1", null, notExported()), dependent("b1", "out2", null, notExported()), dependent("b1", "out3", null, notExported(), "Clonotypes"), ], dependencies: [ dependency("p1", "out1", null, { type: "unavailable" }), dependency("p1", "out2", ["a"], { type: "unavailable" }), ], }), ); expect( usedBy.issues.map((issue) => [blockName(LABELS, issue.blockId), issue.text, issue.outputs]), ).toEqual([ ["Enrichment", "uses 2 outputs this block does not export", ["out1", "out2"]], ["Block b2", "uses data this block does not export", ["out1"]], ["Enrichment", 'uses "Clonotypes", which this block does not export', ["out3"]], ]); expect( uses.issues.map((issue) => [blockName(LABELS, issue.blockId), issue.text, issue.outputs]), ).toEqual([["Samples", "does not provide 2 outputs this block uses", ["out1", "out2"]]]); }); test("issues name the other block by id, for opening it", () => { const [usedBy] = linkAlerts( linkSections({ dependents: [dependent("b1", "out", ["a"], notExported())], dependencies: [], }), ); expect(usedBy.issues.map((issue) => [issue.blockId, blockName(LABELS, issue.blockId)])).toEqual( [["b1", "Enrichment"]], ); }); test("a side with no links needing attention has no alert", () => { const alerts = linkAlerts( linkSections({ dependents: [dependent("b1", "out", ["a"], { type: "pending" })], dependencies: [dependency("p1", "out", null, { type: "ok" })], }), ); expect(alerts).toEqual([]); }); test("links to a block that has not run say it does not provide the data", () => { const [usedBy, uses] = linkAlerts( linkSections({ dependents: [dependent("b1", "out", ["a"], { type: "notRun" }, "Path")], dependencies: [dependency("p1", "out", null, { type: "notRun" }, "Path")], }), ); expect([ALERTS.usedBy.type, ALERTS.usedBy.hint]).toEqual([ "warn", "Checked against the last run of this block. Add the missing data and run it, or " + "reconfigure those blocks.", ]); expect(usedBy.issues.map((issue) => issue.text)).toEqual([ 'uses "Path", which this block does not export', ]); expect([ALERTS.uses.type, ALERTS.uses.hint]).toEqual(["error", null]); expect(uses.issues.map((issue) => issue.text)).toEqual(['does not provide "Path"']); }); test("broken links this block uses are an error", () => { const [uses] = linkAlerts( linkSections({ dependents: [], dependencies: [dependency("p1", "out", ["a"], missingValues(["a"]))], }), ); expect([uses.side, ALERTS.uses.type]).toEqual(["uses", "error"]); }); test("blocks using an output this block does not export are a warning", () => { const [usedBy] = linkAlerts( linkSections({ dependents: [ dependent("b1", "out", ["a"], { type: "missingValues", values: ["a"] }), dependent("b2", "out", ["a"], notExported()), ], dependencies: [], }), ); expect([usedBy.side, ALERTS.usedBy.type]).toEqual(["usedBy", "warn"]); }); test("blocks picking values this block's output lacks are a warning", () => { const [usedBy] = linkAlerts( linkSections({ dependents: [dependent("b1", "out", ["a"], { type: "missingValues", values: ["a"] })], dependencies: [], }), ); expect([usedBy.side, ALERTS.usedBy.type]).toEqual(["usedBy", "warn"]); }); test("keys are unique across sides", () => { const sections: LinkSection[] = linkSections({ dependents: [dependent("p1", "out", ["a"], { type: "missingValues", values: ["a"] })], dependencies: [dependency("p1", "out", ["a"], missingValues(["a"]))], }); const keys = linkAlerts(sections).flatMap((alert) => alert.issues.map((issue) => issue.key)); expect(keys).toHaveLength(2); expect(new Set(keys).size).toBe(keys.length); }); }); describe("blockName", () => { test("falls back to a short id", () => { expect(blockName({ b1: "Enrichment" }, "b1")).toBe("Enrichment"); expect(blockName({}, "0123456789abcdef")).toBe("Block 01234567"); }); }); // Internals /** Labels of the other blocks the links name. */ const LABELS = { b1: "Enrichment", p1: "Samples" }; /** The block whose links these are. */ const THIS = "this"; const PATH = createGlobalPObjectId("samples", "metadata.path"); const ty1 = createColumnValueId({ column: PATH, value: "Ty1" }); describe("describeMissingValue", () => { test("names the block that does not provide the column", () => { const cause = describeMissingValue([pathEntry({ type: "unavailable" })], ty1); expect(cause).toEqual({ type: "source", name: "Ty1", blockId: "samples", text: "does not provide “Path”.", }); }); test("names the block whose column lacks the value", () => { const cause = describeMissingValue( [pathEntry({ type: "missingValues", values: ["Ty1"] })], ty1, ); expect(cause?.text).toBe("has no “Ty1” in “Path”."); }); test("tells nothing of a value the column still has", () => { const cause = describeMissingValue( [pathEntry({ type: "missingValues", values: ["Ty2"] })], ty1, ); expect(cause).toBeUndefined(); }); test("names a block that has not been run as not providing the column", () => { const cause = describeMissingValue([pathEntry({ type: "notRun" })], ty1); expect(cause?.text).toBe("does not provide “Path”."); }); test("names the block still computing the column", () => { const cause = describeMissingValue([pathEntry({ type: "pending" })], ty1); expect(cause?.text).toBe("is still computing “Path”."); }); test("tells nothing of a column that is fine or was not checked", () => { for (const status of [{ type: "ok" }, { type: "unreadable" }] satisfies LinkCheck[]) { expect(describeMissingValue([pathEntry(status)], ty1)).toBeUndefined(); } }); test("tells a provided column waits for the other inputs of the block", () => { for (const status of [ { type: "notRun" }, { type: "pending" }, { type: "unavailable" }, ] satisfies LinkCheck[]) { const dependencies = [pathEntry({ type: "ok" }), abundanceEntry(status)]; const text = "Waiting for the other inputs of this block."; expect(describeMissingValue(dependencies, PATH)).toEqual({ type: "waiting", name: "Path", text, }); expect(describeMissingValue(dependencies, ty1)).toEqual({ type: "waiting", name: "Ty1", text, }); } }); test("does not tell a column waits while it lacks the value or was not checked", () => { const abundance = abundanceEntry({ type: "notRun" }); const lacking = pathEntry({ type: "missingValues", values: ["Ty1"] }); expect(describeMissingValue([lacking, abundance], ty1)?.text).toBe("has no “Ty1” in “Path”."); const unchecked = pathEntry({ type: "unreadable" }); expect(describeMissingValue([unchecked, abundance], ty1)).toBeUndefined(); }); test("does not tell a column waits while every other input is fine", () => { const dependencies = [pathEntry({ type: "ok" }), abundanceEntry({ type: "unreadable" })]; expect(describeMissingValue(dependencies, ty1)).toBeUndefined(); }); test("names a column without a label by its role", () => { const cause = describeMissingValue([pathEntry({ type: "pending" }, null)], ty1); expect(cause).toEqual({ type: "source", name: "Ty1", blockId: "samples", text: "is still computing the column.", }); }); test("names the block that does not provide a column selected as a whole, by id or ref", () => { const deps = [pathEntry({ type: "unavailable" })]; expect(describeMissingValue(deps, PATH)?.text).toBe("does not provide “Path”."); expect(describeMissingValue(deps, PATH)?.name).toBe("Path"); expect( describeMissingValue(deps, { __isRef: true, blockId: "samples", name: "metadata.path" }) ?.text, ).toBe("does not provide “Path”."); }); test("tells nothing of a column selected as a whole that lacks values picked elsewhere", () => { const deps = [pathEntry({ type: "missingValues", values: ["Ty1"] })]; expect(describeMissingValue(deps, PATH)).toBeUndefined(); }); test("tells nothing of a value that is not a column value id, or of another column", () => { const deps = [pathEntry({ type: "unavailable" })]; expect(describeMissingValue(deps, "Ty1")).toBeUndefined(); expect(describeMissingValue(deps, { ref: PATH })).toBeUndefined(); const other = createColumnValueId({ column: createGlobalPObjectId("samples", "metadata.round"), value: "Ty1", }); expect(describeMissingValue(deps, other)).toBeUndefined(); }); }); function dependent( holderBlockId: string, output: string, values: null | ColumnValue[], check: LinkCheck = { type: "ok" }, columnLabel: null | string = null, ): ColumnLink { return { producer: { blockId: THIS, name: output }, holderBlockId, ref: REF, values, columnLabel, check, }; } function dependency( producerBlockId: string, output: string, values: null | ColumnValue[], check: LinkCheck, columnLabel: null | string = null, ): ColumnLink { return { producer: { blockId: producerBlockId, name: output }, holderBlockId: THIS, ref: REF, values, columnLabel, check, }; } function missingValues(values: ColumnValue[]): LinkCheck { return { type: "missingValues", values }; } function notExported(): LinkCheck { return { type: "unavailable" }; } function abundanceEntry(check: LinkCheck): ColumnLink { return { producer: { blockId: "mixcr", name: "abundance" }, holderBlockId: THIS, ref: REF, values: null, columnLabel: "Abundance", check, }; } function pathEntry(check: LinkCheck, columnLabel: null | string = "Path"): ColumnLink { return { producer: { blockId: "samples", name: "metadata.path" }, holderBlockId: THIS, ref: PATH, values: ["Ty1"], columnLabel, check, }; }