Skip to content

Commit 8d4528a

Browse files
committed
fix: make tests pass
1 parent 1262777 commit 8d4528a

3 files changed

Lines changed: 151 additions & 28 deletions

File tree

electron-app/src/config/__tests__/ConfigManager.initialization.test.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, it, expect, beforeEach, vi } from "vitest";
21
import fs from "fs";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import { ConfigManager } from "../configManager.js";
44

55
// Mock fs module
@@ -8,15 +8,24 @@ vi.mock("fs");
88
describe("ConfigManager - Initialization", () => {
99
const templatePath = "/path/to/template.toml";
1010
const userConfigPath = "/path/to/user.toml";
11+
const versionFilePath = "/path/to/version.toml";
12+
const appVersion = "1.0.0";
13+
const appVersionReturnValue = `version = "${appVersion}"`;
1114

1215
beforeEach(() => {
1316
vi.clearAllMocks();
1417
});
1518

1619
it("should create config manager instance", () => {
1720
fs.existsSync.mockReturnValue(true);
21+
fs.readFileSync.mockReturnValue(appVersionReturnValue);
1822

19-
const manager = new ConfigManager(userConfigPath, templatePath);
23+
const manager = new ConfigManager(
24+
userConfigPath,
25+
templatePath,
26+
versionFilePath,
27+
appVersion,
28+
);
2029

2130
expect(manager.userConfigPath).toBe(userConfigPath);
2231
expect(manager.templatePath).toBe(templatePath);
@@ -28,8 +37,9 @@ describe("ConfigManager - Initialization", () => {
2837
return true;
2938
});
3039
fs.mkdirSync.mockImplementation(() => {});
40+
fs.readFileSync.mockReturnValue(appVersionReturnValue);
3141

32-
new ConfigManager(userConfigPath, templatePath);
42+
new ConfigManager(userConfigPath, templatePath, versionFilePath, appVersion);
3343

3444
expect(fs.mkdirSync).toHaveBeenCalledWith("/path/to", {
3545
recursive: true,
@@ -39,17 +49,17 @@ describe("ConfigManager - Initialization", () => {
3949
it("should copy template if user config does not exist", () => {
4050
fs.existsSync.mockImplementation((path) => {
4151
if (path === userConfigPath) return false;
42-
if (path === templatePath) return true;
4352
return true;
4453
});
4554
fs.copyFileSync.mockImplementation(() => {});
55+
fs.writeFileSync.mockImplementation(() => {});
4656
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
4757

48-
new ConfigManager(userConfigPath, templatePath);
58+
new ConfigManager(userConfigPath, templatePath, versionFilePath, appVersion);
4959

5060
expect(fs.copyFileSync).toHaveBeenCalledWith(templatePath, userConfigPath);
5161
expect(consoleSpy).toHaveBeenCalledWith(
52-
expect.stringContaining("Created config from template")
62+
expect.stringContaining("Created config from template"),
5363
);
5464

5565
consoleSpy.mockRestore();
@@ -59,7 +69,7 @@ describe("ConfigManager - Initialization", () => {
5969
fs.existsSync.mockReturnValue(false);
6070

6171
expect(() => {
62-
new ConfigManager(userConfigPath, templatePath);
72+
new ConfigManager(userConfigPath, templatePath, versionFilePath, appVersion);
6373
}).toThrow("Template not found");
6474
});
6575
});

electron-app/src/config/__tests__/ConfigManager.read-write.test.js

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import fs from "fs";
99
describe("ConfigManager - Read/Write Operations", () => {
1010
const templatePath = "/path/to/template.toml";
1111
const userConfigPath = "/path/to/user.toml";
12+
const versionFilePath = "/path/to/version.toml";
13+
const appVersion = "1.0.0";
14+
const appVersionReturnValue = `version = "${appVersion}"`;
1215
const mockTomlContent = `# User Config
1316
name = "test"
1417
enabled = true
@@ -23,9 +26,15 @@ host = "localhost"`;
2326
describe("read", () => {
2427
it("should read and parse TOML config", () => {
2528
fs.existsSync.mockReturnValue(true);
29+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
2630
fs.readFileSync.mockReturnValue(mockTomlContent);
2731

28-
const manager = new ConfigManager(userConfigPath, templatePath);
32+
const manager = new ConfigManager(
33+
userConfigPath,
34+
templatePath,
35+
versionFilePath,
36+
appVersion,
37+
);
2938
const config = manager.read();
3039

3140
expect(config).toHaveProperty("name", "test");
@@ -35,20 +44,32 @@ host = "localhost"`;
3544

3645
it("should throw error on invalid TOML", () => {
3746
fs.existsSync.mockReturnValue(true);
47+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
3848
fs.readFileSync.mockReturnValue("invalid toml [[[");
3949

40-
const manager = new ConfigManager(userConfigPath, templatePath);
50+
const manager = new ConfigManager(
51+
userConfigPath,
52+
templatePath,
53+
versionFilePath,
54+
appVersion,
55+
);
4156

4257
expect(() => manager.read()).toThrow("Failed to read config");
4358
});
4459

4560
it("should throw error on file read failure", () => {
4661
fs.existsSync.mockReturnValue(true);
62+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
4763
fs.readFileSync.mockImplementation(() => {
4864
throw new Error("Permission denied");
4965
});
5066

51-
const manager = new ConfigManager(userConfigPath, templatePath);
67+
const manager = new ConfigManager(
68+
userConfigPath,
69+
templatePath,
70+
versionFilePath,
71+
appVersion,
72+
);
5273

5374
expect(() => manager.read()).toThrow("Failed to read config");
5475
});
@@ -57,9 +78,15 @@ host = "localhost"`;
5778
describe("readRaw", () => {
5879
it("should return raw TOML content", () => {
5980
fs.existsSync.mockReturnValue(true);
81+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
6082
fs.readFileSync.mockReturnValue(mockTomlContent);
6183

62-
const manager = new ConfigManager(userConfigPath, templatePath);
84+
const manager = new ConfigManager(
85+
userConfigPath,
86+
templatePath,
87+
versionFilePath,
88+
appVersion,
89+
);
6390
const raw = manager.readRaw();
6491

6592
expect(raw).toBe(mockTomlContent);
@@ -70,11 +97,17 @@ host = "localhost"`;
7097
describe("update", () => {
7198
it("should update config from object", () => {
7299
fs.existsSync.mockReturnValue(true);
100+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
73101
fs.readFileSync.mockReturnValue(mockTomlContent);
74102
fs.writeFileSync.mockImplementation(() => {});
75103
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
76104

77-
const manager = new ConfigManager(userConfigPath, templatePath);
105+
const manager = new ConfigManager(
106+
userConfigPath,
107+
templatePath,
108+
versionFilePath,
109+
appVersion,
110+
);
78111
const result = manager.update({
79112
name: "updated",
80113
enabled: false,
@@ -91,12 +124,18 @@ host = "localhost"`;
91124

92125
it("should throw error on update failure", () => {
93126
fs.existsSync.mockReturnValue(true);
127+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
94128
fs.readFileSync.mockReturnValue(mockTomlContent);
95129
fs.writeFileSync.mockImplementation(() => {
96130
throw new Error("Write failed");
97131
});
98132

99-
const manager = new ConfigManager(userConfigPath, templatePath);
133+
const manager = new ConfigManager(
134+
userConfigPath,
135+
templatePath,
136+
versionFilePath,
137+
appVersion,
138+
);
100139

101140
expect(() => manager.update({ name: "test" })).toThrow(
102141
"Failed to update config"
@@ -107,11 +146,17 @@ host = "localhost"`;
107146
describe("updateValue", () => {
108147
it("should update a single value", () => {
109148
fs.existsSync.mockReturnValue(true);
149+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
110150
fs.readFileSync.mockReturnValue(mockTomlContent);
111151
fs.writeFileSync.mockImplementation(() => {});
112152
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
113153

114-
const manager = new ConfigManager(userConfigPath, templatePath);
154+
const manager = new ConfigManager(
155+
userConfigPath,
156+
templatePath,
157+
versionFilePath,
158+
appVersion,
159+
);
115160
const result = manager.updateValue("database", "host", "192.168.1.1");
116161

117162
expect(result).toBe(true);
@@ -125,11 +170,17 @@ host = "localhost"`;
125170

126171
it("should throw error on value update failure", () => {
127172
fs.existsSync.mockReturnValue(true);
173+
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
128174
fs.readFileSync.mockImplementation(() => {
129175
throw new Error("Read failed");
130176
});
131177

132-
const manager = new ConfigManager(userConfigPath, templatePath);
178+
const manager = new ConfigManager(
179+
userConfigPath,
180+
templatePath,
181+
versionFilePath,
182+
appVersion,
183+
);
133184

134185
expect(() => manager.updateValue("section", "key", "value")).toThrow(
135186
"Failed to update value"

0 commit comments

Comments
 (0)