|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vite-plus/test"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 4 | +import { EOL } from "node:os"; |
| 5 | +import { configAuthentication } from "./auth.js"; |
| 6 | +import { exportVariable } from "@actions/core"; |
| 7 | + |
| 8 | +vi.mock("@actions/core", () => ({ |
| 9 | + debug: vi.fn(), |
| 10 | + exportVariable: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("node:fs", async () => { |
| 14 | + const actual = await vi.importActual<typeof import("node:fs")>("node:fs"); |
| 15 | + return { |
| 16 | + ...actual, |
| 17 | + existsSync: vi.fn(), |
| 18 | + readFileSync: vi.fn(), |
| 19 | + writeFileSync: vi.fn(), |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +describe("configAuthentication", () => { |
| 24 | + const runnerTemp = "/tmp/runner"; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + vi.stubEnv("RUNNER_TEMP", runnerTemp); |
| 28 | + vi.mocked(existsSync).mockReturnValue(false); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + vi.unstubAllEnvs(); |
| 33 | + vi.resetAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should write .npmrc with registry and auth token", () => { |
| 37 | + configAuthentication("https://registry.npmjs.org/"); |
| 38 | + |
| 39 | + const expectedPath = join(runnerTemp, ".npmrc"); |
| 40 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 41 | + expectedPath, |
| 42 | + expect.stringContaining("//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}"), |
| 43 | + ); |
| 44 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 45 | + expectedPath, |
| 46 | + expect.stringContaining("registry=https://registry.npmjs.org/"), |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should append trailing slash if missing", () => { |
| 51 | + configAuthentication("https://registry.npmjs.org"); |
| 52 | + |
| 53 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 54 | + expect.any(String), |
| 55 | + expect.stringContaining("registry=https://registry.npmjs.org/"), |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should auto-detect scope for GitHub Packages registry", () => { |
| 60 | + vi.stubEnv("GITHUB_REPOSITORY_OWNER", "voidzero-dev"); |
| 61 | + |
| 62 | + configAuthentication("https://npm.pkg.github.com"); |
| 63 | + |
| 64 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 65 | + expect.any(String), |
| 66 | + expect.stringContaining("@voidzero-dev:registry=https://npm.pkg.github.com/"), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should use explicit scope", () => { |
| 71 | + configAuthentication("https://npm.pkg.github.com", "@myorg"); |
| 72 | + |
| 73 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 74 | + expect.any(String), |
| 75 | + expect.stringContaining("@myorg:registry=https://npm.pkg.github.com/"), |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should prepend @ to scope if missing", () => { |
| 80 | + configAuthentication("https://npm.pkg.github.com", "myorg"); |
| 81 | + |
| 82 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 83 | + expect.any(String), |
| 84 | + expect.stringContaining("@myorg:registry=https://npm.pkg.github.com/"), |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should lowercase scope", () => { |
| 89 | + configAuthentication("https://npm.pkg.github.com", "@MyOrg"); |
| 90 | + |
| 91 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 92 | + expect.any(String), |
| 93 | + expect.stringContaining("@myorg:registry=https://npm.pkg.github.com/"), |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should preserve existing .npmrc content except registry lines", () => { |
| 98 | + vi.mocked(existsSync).mockReturnValue(true); |
| 99 | + vi.mocked(readFileSync).mockReturnValue( |
| 100 | + `always-auth=true${EOL}registry=https://old.reg/${EOL}`, |
| 101 | + ); |
| 102 | + |
| 103 | + configAuthentication("https://registry.npmjs.org/"); |
| 104 | + |
| 105 | + const written = vi.mocked(writeFileSync).mock.calls[0]![1] as string; |
| 106 | + expect(written).toContain("always-auth=true"); |
| 107 | + expect(written).not.toContain("https://old.reg/"); |
| 108 | + expect(written).toContain("registry=https://registry.npmjs.org/"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should export NPM_CONFIG_USERCONFIG", () => { |
| 112 | + configAuthentication("https://registry.npmjs.org/"); |
| 113 | + |
| 114 | + expect(exportVariable).toHaveBeenCalledWith( |
| 115 | + "NPM_CONFIG_USERCONFIG", |
| 116 | + join(runnerTemp, ".npmrc"), |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should export NODE_AUTH_TOKEN placeholder when not set", () => { |
| 121 | + configAuthentication("https://registry.npmjs.org/"); |
| 122 | + |
| 123 | + expect(exportVariable).toHaveBeenCalledWith("NODE_AUTH_TOKEN", "XXXXX-XXXXX-XXXXX-XXXXX"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should preserve existing NODE_AUTH_TOKEN", () => { |
| 127 | + vi.stubEnv("NODE_AUTH_TOKEN", "my-real-token"); |
| 128 | + |
| 129 | + configAuthentication("https://registry.npmjs.org/"); |
| 130 | + |
| 131 | + expect(exportVariable).toHaveBeenCalledWith("NODE_AUTH_TOKEN", "my-real-token"); |
| 132 | + }); |
| 133 | +}); |
0 commit comments