-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigManager.read-write.test.js
More file actions
190 lines (159 loc) · 5.44 KB
/
Copy pathConfigManager.read-write.test.js
File metadata and controls
190 lines (159 loc) · 5.44 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, it, expect, beforeEach, vi } from "vitest";
import { ConfigManager } from "../configManager.js";
// Mock fs using Vitest's module mocking for CommonJS
vi.mock("fs");
import fs from "fs";
describe("ConfigManager - Read/Write Operations", () => {
const templatePath = "/path/to/template.toml";
const userConfigPath = "/path/to/user.toml";
const versionFilePath = "/path/to/version.toml";
const appVersion = "1.0.0";
const appVersionReturnValue = `version = "${appVersion}"`;
const mockTomlContent = `# User Config
name = "test"
enabled = true
[database]
host = "localhost"`;
beforeEach(() => {
vi.clearAllMocks();
});
describe("read", () => {
it("should read and parse TOML config", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockReturnValue(mockTomlContent);
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
const config = manager.read();
expect(config).toHaveProperty("name", "test");
expect(config).toHaveProperty("enabled", true);
expect(config.database).toHaveProperty("host", "localhost");
});
it("should throw error on invalid TOML", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockReturnValue("invalid toml [[[");
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
expect(() => manager.read()).toThrow("Failed to read config");
});
it("should throw error on file read failure", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockImplementation(() => {
throw new Error("Permission denied");
});
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
expect(() => manager.read()).toThrow("Failed to read config");
});
});
describe("readRaw", () => {
it("should return raw TOML content", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockReturnValue(mockTomlContent);
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
const raw = manager.readRaw();
expect(raw).toBe(mockTomlContent);
expect(raw).toContain("# User Config");
});
});
describe("update", () => {
it("should update config from object", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockReturnValue(mockTomlContent);
fs.writeFileSync.mockImplementation(() => {});
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
const result = manager.update({
name: "updated",
enabled: false,
});
expect(result).toBe(true);
expect(fs.writeFileSync).toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining("Config updated successfully")
);
consoleSpy.mockRestore();
});
it("should throw error on update failure", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockReturnValue(mockTomlContent);
fs.writeFileSync.mockImplementation(() => {
throw new Error("Write failed");
});
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
expect(() => manager.update({ name: "test" })).toThrow(
"Failed to update config"
);
});
});
describe("updateValue", () => {
it("should update a single value", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockReturnValue(mockTomlContent);
fs.writeFileSync.mockImplementation(() => {});
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
const result = manager.updateValue("database", "host", "192.168.1.1");
expect(result).toBe(true);
expect(fs.writeFileSync).toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining("Updated database.host")
);
consoleSpy.mockRestore();
});
it("should throw error on value update failure", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValueOnce(appVersionReturnValue);
fs.readFileSync.mockImplementation(() => {
throw new Error("Read failed");
});
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
expect(() => manager.updateValue("section", "key", "value")).toThrow(
"Failed to update value"
);
});
});
});