|
| 1 | +const assert = require("node:assert/strict"); |
| 2 | +const { describe, it, beforeEach, afterEach } = require("node:test"); |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const mock = require("mock-fs"); |
| 6 | + |
| 7 | +const { run } = require("../index.js"); |
| 8 | + |
| 9 | +describe("generate-brand-content run", () => { |
| 10 | + let messages; |
| 11 | + let core; |
| 12 | + let io; |
| 13 | + let outputDir; |
| 14 | + let defaultInputs; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + messages = []; |
| 18 | + core = { |
| 19 | + info: (message) => { |
| 20 | + messages.push(message); |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + io = { |
| 25 | + mkdirP: async (dir) => fs.promises.mkdir(dir, { recursive: true }), |
| 26 | + }; |
| 27 | + |
| 28 | + outputDir = path.join("tmp", "brand"); |
| 29 | + |
| 30 | + defaultInputs = { |
| 31 | + version: "1.2.3", |
| 32 | + commit: "deadbeef", |
| 33 | + colors: JSON.stringify({ |
| 34 | + items: [ |
| 35 | + { id: "primary", value: "#000000" }, |
| 36 | + { id: "secondary", value: "#ffffff" }, |
| 37 | + ], |
| 38 | + }), |
| 39 | + brandMission: JSON.stringify({ statement: "Empower builders" }), |
| 40 | + logos: JSON.stringify({ items: [{ id: "primary", format: "svg" }] }), |
| 41 | + typography: JSON.stringify({ |
| 42 | + items: [{ id: "heading", family: "HK Grotesk" }], |
| 43 | + }), |
| 44 | + }; |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + mock.restore(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("writes generated files and logs progress", async () => { |
| 52 | + mock({}); |
| 53 | + |
| 54 | + await run({ |
| 55 | + ...defaultInputs, |
| 56 | + core, |
| 57 | + io, |
| 58 | + outputDir, |
| 59 | + }); |
| 60 | + |
| 61 | + const colorsFile = fs.readFileSync( |
| 62 | + path.join(outputDir, "generated-colors.ts"), |
| 63 | + "utf8", |
| 64 | + ); |
| 65 | + assert.match(colorsFile, /export const brandColors: ColorCollection =/); |
| 66 | + assert.match(colorsFile, /Version: 1\.2\.3/); |
| 67 | + assert.match(colorsFile, /Commit: deadbeef/); |
| 68 | + |
| 69 | + const missionFile = fs.readFileSync( |
| 70 | + path.join(outputDir, "generated-mission.ts"), |
| 71 | + "utf8", |
| 72 | + ); |
| 73 | + assert.match(missionFile, /export const brandMission: BrandMission =/); |
| 74 | + |
| 75 | + const typographyFile = fs.readFileSync( |
| 76 | + path.join(outputDir, "generated-typography.ts"), |
| 77 | + "utf8", |
| 78 | + ); |
| 79 | + assert.match( |
| 80 | + typographyFile, |
| 81 | + /export const typography: TypographyCollection =/, |
| 82 | + ); |
| 83 | + |
| 84 | + assert.deepEqual(messages, [ |
| 85 | + "\u2713 Generated generated-colors.ts (2 items)", |
| 86 | + "\u2713 Generated generated-mission.ts", |
| 87 | + "\u2713 Generated generated-typography.ts (1 items)", |
| 88 | + "\u2713 Generated generated-logos.ts (1 items)", |
| 89 | + "\u2705 All brand content files generated successfully", |
| 90 | + ]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("throws when required version is missing", async () => { |
| 94 | + await assert.rejects( |
| 95 | + () => |
| 96 | + run({ |
| 97 | + ...defaultInputs, |
| 98 | + version: " ", |
| 99 | + core, |
| 100 | + io, |
| 101 | + outputDir, |
| 102 | + }), |
| 103 | + /version input is required/, |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it("throws when colors payload is not valid JSON", async () => { |
| 108 | + await assert.rejects( |
| 109 | + () => |
| 110 | + run({ |
| 111 | + ...defaultInputs, |
| 112 | + colors: "not-json", |
| 113 | + core, |
| 114 | + io, |
| 115 | + outputDir, |
| 116 | + }), |
| 117 | + /Failed to parse colors input/, |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it("throws when core toolkit is missing", async () => { |
| 122 | + await assert.rejects( |
| 123 | + () => |
| 124 | + run({ |
| 125 | + ...defaultInputs, |
| 126 | + core: null, |
| 127 | + io, |
| 128 | + outputDir, |
| 129 | + }), |
| 130 | + /@actions\/core instance must be provided/, |
| 131 | + ); |
| 132 | + }); |
| 133 | +}); |
0 commit comments