-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
92 lines (78 loc) · 2.73 KB
/
index.test.js
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
const { jest: jestObj } = require("@jest/globals");
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const core = require("@actions/core");
// Mock dependencies
jest.mock("fs");
jest.mock("path");
jest.mock("glob");
jest.mock("@actions/core");
jest.mock("@continuedev/config-yaml", () => ({
parseConfigYaml: jest.fn(),
}));
// Mock fetch
global.fetch = jest.fn();
// Import the module under test
const indexModule = require("./index");
describe("GitHub Action", () => {
let originalConsoleLog;
let originalEnv;
beforeEach(() => {
// Store original environment variables
originalEnv = { ...process.env };
// Set GitHub Actions environment
process.env = { ...originalEnv, GITHUB_ACTIONS: "true" };
// Spy on console.log
originalConsoleLog = console.log;
console.log = jest.fn();
// Mock successful fetch response
global.fetch.mockReset();
global.fetch.mockResolvedValue({
ok: true,
json: async () => ({ versionId: "test-version-id" }),
text: async () => "",
});
});
afterEach(() => {
// Restore process.env and console.log
process.env = originalEnv;
console.log = originalConsoleLog;
});
test("Should upload a package successfully", async () => {
// Import the module - this will run the code
jest.isolateModules(() => {
require("./index");
});
// Wait for any promises to resolve (including the run() function's promises)
await new Promise((process) => setTimeout(process, 100));
// Verify that core.getInput was called with the right parameters
expect(core.getInput).toHaveBeenCalledWith("paths", { required: true });
expect(core.getInput).toHaveBeenCalledWith("owner-slug", {
required: true,
});
expect(core.getInput).toHaveBeenCalledWith("api-key", { required: true });
// Verify that the file was read and glob was used
expect(glob.sync).toHaveBeenCalled();
expect(fs.readFileSync).toHaveBeenCalled();
// Verify fetch was called with the right URL and parameters
expect(global.fetch).toHaveBeenCalledWith(
"https://api.continue.dev/packages/test-owner/package/versions/new",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
"Content-Type": "application/json",
Authorization: "Bearer test-api-key",
}),
})
);
// Verify console output - first call
expect(console.log).toHaveBeenNthCalledWith(1, "Uploading...");
// Verify console output - second call
expect(console.log).toHaveBeenNthCalledWith(
2,
"Successfully published new version from test/package.yaml to https://hub.continue.dev/platform/test-owner/package:",
"test-version-id"
);
});
});