@@ -9,6 +9,9 @@ import fs from "fs";
99describe ( "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
1316name = "test"
1417enabled = 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