1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
import { describe, it, expect } from "bun:test";
import { expandMatrix, validateMatrix } from "../lib/workflow-matrix";
describe("workflow-matrix — expandMatrix", () => {
it("empty axes {} with no include returns []", () => {
const combos = expandMatrix({ axes: {} });
expect(combos).toEqual([]);
});
it("single axis expands to one combo per value in alpha-key order", () => {
const combos = expandMatrix({ axes: { os: ["a", "b", "c"] } });
expect(combos).toHaveLength(3);
expect(combos[0]).toEqual({ os: "a" });
expect(combos[1]).toEqual({ os: "b" });
expect(combos[2]).toEqual({ os: "c" });
});
it("two axes produce the cartesian product with both keys", () => {
const combos = expandMatrix({
axes: { os: ["ubuntu", "mac"], node: [16, 18] },
});
expect(combos).toHaveLength(4);
for (const c of combos) {
expect(Object.keys(c).sort()).toEqual(["node", "os"]);
}
const serialized = combos.map((c) => JSON.stringify(c)).sort();
expect(serialized).toEqual(
[
{ node: 16, os: "ubuntu" },
{ node: 16, os: "mac" },
{ node: 18, os: "ubuntu" },
{ node: 18, os: "mac" },
]
.map((c) => JSON.stringify(c))
.sort()
);
});
it("exclude removes matching combos", () => {
const combos = expandMatrix({
axes: { os: ["a", "b"], node: [16, 18] },
exclude: [{ os: "a", node: 16 }],
});
expect(combos).toHaveLength(3);
expect(combos.find((c) => c.os === "a" && c.node === 16)).toBeUndefined();
});
it("include adds a standalone combo when it does not match any cartesian entry", () => {
const combos = expandMatrix({
axes: { os: ["a"] },
include: [{ os: "windows", extra: "bonus" }],
});
expect(combos).toHaveLength(2);
expect(combos.find((c) => c.os === "windows" && c.extra === "bonus")).toBeDefined();
});
it("include extends an existing combo with extra keys when axis keys match", () => {
const combos = expandMatrix({
axes: { os: ["a", "b"] },
include: [{ os: "a", env: "prod" }],
});
expect(combos).toHaveLength(2);
const aCombo = combos.find((c) => c.os === "a");
const bCombo = combos.find((c) => c.os === "b");
expect(aCombo).toEqual({ os: "a", env: "prod" });
expect(bCombo).toEqual({ os: "b" });
});
it("empty axis value [] yields no combos", () => {
const combos = expandMatrix({ axes: { os: [] } });
expect(combos).toEqual([]);
});
it("validateMatrix rejects non-object input and non-array axis values", () => {
expect(validateMatrix(null).ok).toBe(false);
expect(validateMatrix(undefined).ok).toBe(false);
expect(validateMatrix("not an object").ok).toBe(false);
expect(validateMatrix([]).ok).toBe(false);
const badAxis = validateMatrix({ axes: { os: "not-an-array" } });
expect(badAxis.ok).toBe(false);
if (!badAxis.ok) expect(badAxis.error).toMatch(/array/i);
});
it("validateMatrix accepts a well-formed spec", () => {
const good = validateMatrix({
axes: { os: ["a", "b"] },
include: [{ os: "a", env: "x" }],
exclude: [{ os: "b" }],
failFast: true,
maxParallel: 4,
});
expect(good.ok).toBe(true);
if (good.ok) {
expect(good.spec.axes.os).toEqual(["a", "b"]);
expect(good.spec.failFast).toBe(true);
expect(good.spec.maxParallel).toBe(4);
}
});
});
|