Elffuss BraidHub

kikocis/continue public

9d5865e0e05b · 3.8 KiBRawPermalink
1// Mock the version module before any other imports
2vi.mock("../version.js", () => ({
3 getVersion: vi.fn(),
4 getLatestVersion: vi.fn(),
5 compareVersions: vi.fn(),
6}));
7
8// Mock useTerminalSize hook
9vi.mock("./hooks/useTerminalSize.js", () => ({
10 useTerminalSize: () => ({ columns: 80, rows: 24 }),
11}));
12
13// Mock sentry to prevent initialization issues
14vi.mock("../sentry.js", () => ({
15 sentryService: {
16 captureException: vi.fn(),
17 captureMessage: vi.fn(),
18 setUser: vi.fn(),
19 setTag: vi.fn(),
20 setContext: vi.fn(),
21 addBreadcrumb: vi.fn(),
22 startSpan: vi.fn(),
23 flush: vi.fn(),
24 close: vi.fn(),
25 isEnabled: vi.fn().mockReturnValue(false),
26 },
27}));
28
29import { render } from "ink-testing-library";
30import React from "react";
31import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
32
33import * as versionModule from "../version.js";
34
35import { UpdateNotification } from "./UpdateNotification.js";
36
37const mockVersionModule = vi.mocked(versionModule);
38
39describe("UpdateNotification", () => {
40 let originalNodeEnv: string | undefined;
41
42 beforeEach(() => {
43 vi.clearAllMocks();
44 originalNodeEnv = process.env.NODE_ENV;
45 // Default mock implementations
46 mockVersionModule.getLatestVersion.mockResolvedValue("1.0.1");
47 mockVersionModule.compareVersions.mockReturnValue("older");
48 });
49
50 afterEach(() => {
51 // Restore original NODE_ENV
52 if (originalNodeEnv === undefined) {
53 delete process.env.NODE_ENV;
54 } else {
55 process.env.NODE_ENV = originalNodeEnv;
56 }
57 });
58
59 it("should not check for updates when version ends with -dev", async () => {
60 // Set NODE_ENV to non-test value to enable update checks
61 process.env.NODE_ENV = "development";
62 mockVersionModule.getVersion.mockReturnValue("1.0.0-dev");
63
64 render(<UpdateNotification />);
65
66 // Wait a bit to ensure useEffect has run
67 await new Promise((resolve) => setTimeout(resolve, 50));
68
69 // getLatestVersion should not have been called for dev versions
70 expect(mockVersionModule.getLatestVersion).not.toHaveBeenCalled();
71 });
72
73 it("should check for updates when version does not end with -dev", async () => {
74 // Set NODE_ENV to non-test value to enable update checks
75 process.env.NODE_ENV = "development";
76 mockVersionModule.getVersion.mockReturnValue("1.0.0");
77
78 render(<UpdateNotification />);
79
80 // Wait a bit to ensure useEffect has run
81 await new Promise((resolve) => setTimeout(resolve, 50));
82
83 // getLatestVersion should have been called for non-dev versions
84 expect(mockVersionModule.getLatestVersion).toHaveBeenCalled();
85 });
86
87 it("should handle various dev version formats", async () => {
88 // Set NODE_ENV to non-test value to enable update checks
89 process.env.NODE_ENV = "development";
90 const devVersions = ["1.0.0-dev", "2.1.3-dev", "0.5.0-dev"];
91
92 for (const version of devVersions) {
93 vi.clearAllMocks();
94 mockVersionModule.getVersion.mockReturnValue(version);
95
96 render(<UpdateNotification />);
97
98 // Wait a bit to ensure useEffect has run
99 await new Promise((resolve) => setTimeout(resolve, 50));
100
101 expect(mockVersionModule.getLatestVersion).not.toHaveBeenCalled();
102 }
103 });
104
105 it("should still check for updates with other version suffixes", async () => {
106 // Set NODE_ENV to non-test value to enable update checks
107 process.env.NODE_ENV = "development";
108 const nonDevVersions = ["1.0.0-beta", "1.0.0-alpha", "1.0.0-rc1"];
109
110 for (const version of nonDevVersions) {
111 vi.clearAllMocks();
112 mockVersionModule.getVersion.mockReturnValue(version);
113
114 render(<UpdateNotification />);
115
116 // Wait a bit to ensure useEffect has run
117 await new Promise((resolve) => setTimeout(resolve, 50));
118
119 expect(mockVersionModule.getLatestVersion).toHaveBeenCalled();
120 }
121 });
122});