Skip to content

Commit f90daf7

Browse files
fix(mock-module-exports): crash
cause: import-statement was catching `vitest` dude to `vite`
1 parent 0e7840b commit f90daf7

4 files changed

Lines changed: 247 additions & 3 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// took form https://github.com/calcom/cal.diy/blob/e3eaa69339c549365f7749634e5d27f3aef1462f/packages/features/webhooks/lib/sendPayload.test.ts
2+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
3+
4+
import { WebhookVersion } from "./interface/IWebhookRepository";
5+
import sendPayload from "./sendPayload";
6+
7+
describe("sendPayload", () => {
8+
const mockFetch = vi.fn();
9+
10+
beforeEach(() => {
11+
vi.stubGlobal("fetch", mockFetch);
12+
mockFetch.mockResolvedValue({
13+
ok: true,
14+
status: 200,
15+
});
16+
});
17+
18+
afterEach(() => {
19+
vi.unstubAllGlobals();
20+
vi.resetAllMocks();
21+
});
22+
23+
describe("X-Cal-Webhook-Version header", () => {
24+
it("should include X-Cal-Webhook-Version header with the webhook version", async () => {
25+
const webhook = {
26+
subscriberUrl: "https://example.com/webhook",
27+
appId: null,
28+
payloadTemplate: null,
29+
version: WebhookVersion.V_2021_10_20,
30+
};
31+
32+
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
33+
title: "Test Booking",
34+
startTime: "2024-01-01T10:00:00Z",
35+
endTime: "2024-01-01T11:00:00Z",
36+
organizer: {
37+
email: "organizer@example.com",
38+
name: "Organizer",
39+
timeZone: "UTC",
40+
language: { locale: "en" },
41+
},
42+
attendees: [],
43+
type: "test-event",
44+
description: "",
45+
} as unknown as Parameters<typeof sendPayload>[4]);
46+
47+
expect(mockFetch).toHaveBeenCalledTimes(1);
48+
const [url, options] = mockFetch.mock.calls[0];
49+
50+
expect(url).toBe("https://example.com/webhook");
51+
expect(options.headers).toHaveProperty("X-Cal-Webhook-Version", "2021-10-20");
52+
});
53+
54+
it("should include X-Cal-Signature-256 header alongside version header", async () => {
55+
const webhook = {
56+
subscriberUrl: "https://example.com/webhook",
57+
appId: null,
58+
payloadTemplate: null,
59+
version: WebhookVersion.V_2021_10_20,
60+
};
61+
62+
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
63+
title: "Test Booking",
64+
startTime: "2024-01-01T10:00:00Z",
65+
endTime: "2024-01-01T11:00:00Z",
66+
organizer: {
67+
email: "organizer@example.com",
68+
name: "Organizer",
69+
timeZone: "UTC",
70+
language: { locale: "en" },
71+
},
72+
attendees: [],
73+
type: "test-event",
74+
description: "",
75+
} as unknown as Parameters<typeof sendPayload>[4]);
76+
77+
const [, options] = mockFetch.mock.calls[0];
78+
79+
expect(options.headers).toHaveProperty("X-Cal-Signature-256");
80+
expect(options.headers).toHaveProperty("X-Cal-Webhook-Version");
81+
expect(options.headers).toHaveProperty("Content-Type", "application/json");
82+
});
83+
84+
it("should send correct version for different webhook versions", async () => {
85+
// Test with the current version
86+
const webhook = {
87+
subscriberUrl: "https://example.com/webhook",
88+
appId: null,
89+
payloadTemplate: null,
90+
version: WebhookVersion.V_2021_10_20,
91+
};
92+
93+
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
94+
title: "Test",
95+
startTime: "2024-01-01T10:00:00Z",
96+
endTime: "2024-01-01T11:00:00Z",
97+
organizer: {
98+
email: "test@example.com",
99+
name: "Test",
100+
timeZone: "UTC",
101+
language: { locale: "en" },
102+
},
103+
attendees: [],
104+
type: "test",
105+
description: "",
106+
} as unknown as Parameters<typeof sendPayload>[4]);
107+
108+
const [, options] = mockFetch.mock.calls[0];
109+
expect(options.headers["X-Cal-Webhook-Version"]).toBe("2021-10-20");
110+
});
111+
});
112+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// took form https://github.com/calcom/cal.diy/blob/e3eaa69339c549365f7749634e5d27f3aef1462f/packages/features/webhooks/lib/sendPayload.test.ts
2+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
3+
4+
import { WebhookVersion } from "./interface/IWebhookRepository";
5+
import sendPayload from "./sendPayload";
6+
7+
describe("sendPayload", () => {
8+
const mockFetch = vi.fn();
9+
10+
beforeEach(() => {
11+
vi.stubGlobal("fetch", mockFetch);
12+
mockFetch.mockResolvedValue({
13+
ok: true,
14+
status: 200,
15+
});
16+
});
17+
18+
afterEach(() => {
19+
vi.unstubAllGlobals();
20+
vi.resetAllMocks();
21+
});
22+
23+
describe("X-Cal-Webhook-Version header", () => {
24+
it("should include X-Cal-Webhook-Version header with the webhook version", async () => {
25+
const webhook = {
26+
subscriberUrl: "https://example.com/webhook",
27+
appId: null,
28+
payloadTemplate: null,
29+
version: WebhookVersion.V_2021_10_20,
30+
};
31+
32+
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
33+
title: "Test Booking",
34+
startTime: "2024-01-01T10:00:00Z",
35+
endTime: "2024-01-01T11:00:00Z",
36+
organizer: {
37+
email: "organizer@example.com",
38+
name: "Organizer",
39+
timeZone: "UTC",
40+
language: { locale: "en" },
41+
},
42+
attendees: [],
43+
type: "test-event",
44+
description: "",
45+
} as unknown as Parameters<typeof sendPayload>[4]);
46+
47+
expect(mockFetch).toHaveBeenCalledTimes(1);
48+
const [url, options] = mockFetch.mock.calls[0];
49+
50+
expect(url).toBe("https://example.com/webhook");
51+
expect(options.headers).toHaveProperty("X-Cal-Webhook-Version", "2021-10-20");
52+
});
53+
54+
it("should include X-Cal-Signature-256 header alongside version header", async () => {
55+
const webhook = {
56+
subscriberUrl: "https://example.com/webhook",
57+
appId: null,
58+
payloadTemplate: null,
59+
version: WebhookVersion.V_2021_10_20,
60+
};
61+
62+
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
63+
title: "Test Booking",
64+
startTime: "2024-01-01T10:00:00Z",
65+
endTime: "2024-01-01T11:00:00Z",
66+
organizer: {
67+
email: "organizer@example.com",
68+
name: "Organizer",
69+
timeZone: "UTC",
70+
language: { locale: "en" },
71+
},
72+
attendees: [],
73+
type: "test-event",
74+
description: "",
75+
} as unknown as Parameters<typeof sendPayload>[4]);
76+
77+
const [, options] = mockFetch.mock.calls[0];
78+
79+
expect(options.headers).toHaveProperty("X-Cal-Signature-256");
80+
expect(options.headers).toHaveProperty("X-Cal-Webhook-Version");
81+
expect(options.headers).toHaveProperty("Content-Type", "application/json");
82+
});
83+
84+
it("should send correct version for different webhook versions", async () => {
85+
// Test with the current version
86+
const webhook = {
87+
subscriberUrl: "https://example.com/webhook",
88+
appId: null,
89+
payloadTemplate: null,
90+
version: WebhookVersion.V_2021_10_20,
91+
};
92+
93+
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
94+
title: "Test",
95+
startTime: "2024-01-01T10:00:00Z",
96+
endTime: "2024-01-01T11:00:00Z",
97+
organizer: {
98+
email: "test@example.com",
99+
name: "Test",
100+
timeZone: "UTC",
101+
language: { locale: "en" },
102+
},
103+
attendees: [],
104+
type: "test",
105+
description: "",
106+
} as unknown as Parameters<typeof sendPayload>[4]);
107+
108+
const [, options] = mockFetch.mock.calls[0];
109+
expect(options.headers["X-Cal-Webhook-Version"]).toBe("2021-10-20");
110+
});
111+
});
112+
});

utils/src/ast-grep/import-statement.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,24 @@ describe("import-statement", () => {
248248
assert.strictEqual(getNodeImportStatements(ast, "path").length, 1);
249249
assert.strictEqual(getNodeImportStatements(ast, "empty").length, 0);
250250
});
251+
252+
it("should not partially match module names", () => {
253+
const code = dedent`
254+
import { describe } from "node:test";
255+
import { it } from "test";
256+
import { describe as vDescribe } from "vitest";
257+
import foo from "@scope/test";
258+
import bar from "test/utils";
259+
`;
260+
261+
const ast = astGrep.parse(astGrep.Lang.JavaScript, code);
262+
263+
const imports = getNodeImportStatements(ast, "test");
264+
265+
assert.strictEqual(imports.length, 2);
266+
assert.deepStrictEqual(
267+
imports.map((i) => i.field("source")?.text()),
268+
['"node:test"', '"test"'],
269+
);
270+
});
251271
});

utils/src/ast-grep/import-statement.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const getNodeImportStatements = (
1414
kind: 'string',
1515
has: {
1616
kind: 'string_fragment',
17-
regex: `(node:)?${nodeModuleName}$`,
17+
regex: `^(node:)?${nodeModuleName}$`,
1818
},
1919
},
2020
not: {
@@ -67,7 +67,7 @@ export const getNodeImportCalls = (
6767
kind: 'string',
6868
has: {
6969
kind: 'string_fragment',
70-
regex: `(node:)?${nodeModuleName}$`,
70+
regex: `^(node:)?${nodeModuleName}$`,
7171
},
7272
},
7373
},
@@ -89,7 +89,7 @@ export const getNodeImportCalls = (
8989
kind: 'string',
9090
has: {
9191
kind: 'string_fragment',
92-
regex: `(node:)?${nodeModuleName}$`,
92+
regex: `^(node:)?${nodeModuleName}$`,
9393
},
9494
},
9595
},

0 commit comments

Comments
 (0)