Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions packages/action/__tests__/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,189 @@ describe("compareWithBase", () => {
});
});

test("compares against output file path when provided", async () => {
const explicitResultWithOutput: MinifyResult = {
files: [
{
file: "src/app.js",
outputFile: "dist/app.min.js",
originalSize: 10000,
minifiedSize: 3000,
reduction: 70,
timeMs: 50,
},
],
compressor: "terser",
totalOriginalSize: 10000,
totalMinifiedSize: 3000,
totalReduction: 70,
totalTimeMs: 50,
};

(context as { payload: Record<string, unknown> }).payload = {
pull_request: { base: { ref: "main" } },
};

const mockGetContent = vi.fn().mockResolvedValue({
data: { type: "file", size: 3500 },
});

vi.mocked(getOctokit).mockReturnValue({
rest: {
repos: { getContent: mockGetContent },
},
} as unknown as ReturnType<typeof getOctokit>);

const result = await compareWithBase(explicitResultWithOutput, "token");

expect(result[0]?.file).toBe("src/app.js");
expect(mockGetContent).toHaveBeenCalledWith({
owner: "test-owner",
repo: "test-repo",
path: "dist/app.min.js",
ref: "main",
});
});

test("normalizes windows separators in output compare path", async () => {
const explicitResultWithWindowsPath: MinifyResult = {
files: [
{
file: "src/app.js",
outputFile: "dist\\app.min.js",
originalSize: 10000,
minifiedSize: 3000,
reduction: 70,
timeMs: 50,
},
],
compressor: "terser",
totalOriginalSize: 10000,
totalMinifiedSize: 3000,
totalReduction: 70,
totalTimeMs: 50,
};

(context as { payload: Record<string, unknown> }).payload = {
pull_request: { base: { ref: "main" } },
};

const mockGetContent = vi.fn().mockResolvedValue({
data: { type: "file", size: 3500 },
});

vi.mocked(getOctokit).mockReturnValue({
rest: {
repos: { getContent: mockGetContent },
},
} as unknown as ReturnType<typeof getOctokit>);

await compareWithBase(explicitResultWithWindowsPath, "token");

expect(mockGetContent).toHaveBeenCalledWith({
owner: "test-owner",
repo: "test-repo",
path: "dist/app.min.js",
ref: "main",
});
});

test("skips unsafe output compare path and falls back to source path", async () => {
const explicitResultWithUnsafeOutput: MinifyResult = {
files: [
{
file: "src/app.js",
outputFile: "../secrets.env",
originalSize: 10000,
minifiedSize: 3000,
reduction: 70,
timeMs: 50,
},
],
compressor: "terser",
totalOriginalSize: 10000,
totalMinifiedSize: 3000,
totalReduction: 70,
totalTimeMs: 50,
};

(context as { payload: Record<string, unknown> }).payload = {
pull_request: { base: { ref: "main" } },
};

const mockGetContent = vi.fn().mockResolvedValue({
data: { type: "file", size: 3500 },
});

vi.mocked(getOctokit).mockReturnValue({
rest: {
repos: { getContent: mockGetContent },
},
} as unknown as ReturnType<typeof getOctokit>);

await compareWithBase(explicitResultWithUnsafeOutput, "token");

expect(mockGetContent).toHaveBeenCalledWith({
owner: "test-owner",
repo: "test-repo",
path: "src/app.js",
ref: "main",
});
expect(warning).toHaveBeenCalledWith(
"Skipping unsafe base-compare path: ../secrets.env"
);
});

test("marks file as new when both output and source compare paths are unsafe", async () => {
const resultWithUnsafePaths: MinifyResult = {
files: [
{
file: "..",
outputFile: "../secrets.env",
originalSize: 10000,
minifiedSize: 3000,
reduction: 70,
timeMs: 50,
},
],
compressor: "terser",
totalOriginalSize: 10000,
totalMinifiedSize: 3000,
totalReduction: 70,
totalTimeMs: 50,
};

(context as { payload: Record<string, unknown> }).payload = {
pull_request: { base: { ref: "main" } },
};

const mockGetContent = vi.fn();
vi.mocked(getOctokit).mockReturnValue({
rest: {
repos: { getContent: mockGetContent },
},
} as unknown as ReturnType<typeof getOctokit>);

const result = await compareWithBase(resultWithUnsafePaths, "token");

expect(result).toEqual([
{
file: "..",
baseSize: null,
currentSize: 3000,
change: null,
isNew: true,
},
]);
expect(mockGetContent).not.toHaveBeenCalled();
expect(warning).toHaveBeenCalledWith(
"Skipping unsafe base-compare path: ../secrets.env"
);
expect(warning).toHaveBeenCalledWith(
"Skipping unsafe base-compare path: .."
);
});

test("handles new file (404 error)", async () => {
(context as { payload: Record<string, unknown> }).payload = {
pull_request: { base: { ref: "main" } },
Expand Down
15 changes: 15 additions & 0 deletions packages/action/__tests__/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ describe("parseInputs auto mode", () => {
expect(() => parseInputs()).not.toThrow();
});

test("does not require type for esbuild when auto=true", () => {
vi.mocked(getInput).mockImplementation((name: string) => {
if (name === "compressor") return "esbuild";
if (name === "type") return "";
if (name === "output-dir") return "dist";
return "";
});
vi.mocked(getBooleanInput).mockImplementation((name: string) => {
if (name === "auto") return true;
return false;
});

expect(() => parseInputs()).not.toThrow();
});

test("parses patterns from comma-separated string", () => {
vi.mocked(getInput).mockImplementation((name: string) => {
if (name === "patterns") return "src/**/*.js, lib/**/*.ts";
Expand Down
42 changes: 42 additions & 0 deletions packages/action/__tests__/minify.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*! node-minify action tests - MIT Licensed */

import { stat } from "node:fs/promises";
import path from "node:path";
import { minify } from "@node-minify/core";
import { getFilesizeGzippedRaw, resolveCompressor } from "@node-minify/utils";
import { beforeEach, describe, expect, test, vi } from "vitest";
Expand Down Expand Up @@ -66,6 +67,7 @@ describe("runMinification", () => {
files: [
{
file: "src/app.js",
outputFile: "dist/app.min.js",
originalSize: 1000,
minifiedSize: 500,
reduction: 50,
Expand Down Expand Up @@ -171,4 +173,44 @@ describe("runMinification", () => {
})
);
});

test("should store outputFile as repository-relative path for absolute output", async () => {
vi.mocked(stat)
.mockResolvedValueOnce({ size: 1000 } as any)
.mockResolvedValueOnce({ size: 500 } as any);
vi.mocked(resolveCompressor).mockResolvedValue({
compressor: vi.fn(),
label: "terser",
} as any);
vi.mocked(minify).mockResolvedValue("minified content");

const absoluteOutput = path.resolve(process.cwd(), "dist/app.min.js");
const result = await runMinification({
...mockInputs,
output: absoluteOutput,
});

expect(result.files[0]?.outputFile).toBe("dist/app.min.js");
});

test("should include working-directory prefix in outputFile", async () => {
vi.mocked(stat)
.mockResolvedValueOnce({ size: 1000 } as any)
.mockResolvedValueOnce({ size: 500 } as any);
vi.mocked(resolveCompressor).mockResolvedValue({
compressor: vi.fn(),
label: "terser",
} as any);
vi.mocked(minify).mockResolvedValue("minified content");

const result = await runMinification({
...mockInputs,
workingDirectory: "packages/action",
output: "dist/app.min.js",
});

expect(result.files[0]?.outputFile).toBe(
"packages/action/dist/app.min.js"
);
});
});
Loading