@@ -24,58 +24,6 @@ func skipIfNotUnix(t *testing.T) {
2424 }
2525}
2626
27- const (
28- legacyDirName = ".agent-session-viewer"
29- newDirName = ".agentsview"
30- )
31-
32- // setupLegacyEnv creates a temp directory with a populated legacy
33- // data dir and returns (tmp, newDir). Files are written into the
34- // legacy directory with config.json getting 0o600 permissions and
35- // all other files getting 0o644.
36- func setupLegacyEnv (
37- t * testing.T , files map [string ]string ,
38- ) (string , string ) {
39- t .Helper ()
40- tmp := t .TempDir ()
41- legacyDir := filepath .Join (tmp , legacyDirName )
42- newDir := filepath .Join (tmp , newDirName )
43-
44- if err := os .MkdirAll (legacyDir , 0o755 ); err != nil {
45- t .Fatalf ("create legacy dir: %v" , err )
46- }
47-
48- for name , content := range files {
49- perm := os .FileMode (0o644 )
50- if name == "config.json" {
51- perm = 0o600
52- }
53- path := filepath .Join (legacyDir , name )
54- if err := os .WriteFile (
55- path , []byte (content ), perm ,
56- ); err != nil {
57- t .Fatalf ("write %s: %v" , name , err )
58- }
59- }
60- return tmp , newDir
61- }
62-
63- func assertFileContent (
64- t * testing.T , path , expected string ,
65- ) {
66- t .Helper ()
67- data , err := os .ReadFile (path )
68- if err != nil {
69- t .Fatalf ("read %s: %v" , filepath .Base (path ), err )
70- }
71- if string (data ) != expected {
72- t .Errorf (
73- "%s content = %q, want %q" ,
74- filepath .Base (path ), data , expected ,
75- )
76- }
77- }
78-
7927func writeConfig (t * testing.T , dir string , data any ) {
8028 t .Helper ()
8129 b , err := json .Marshal (data )
@@ -135,25 +83,6 @@ func configWithTmpDir(t *testing.T) (Config, string) {
13583 return Config {DataDir : dir }, dir
13684}
13785
138- // assertFilePerm checks that the file at path has permission bits
139- // matching the given mask and expected value.
140- func assertFilePerm (
141- t * testing.T , path string ,
142- mask , want os.FileMode ,
143- ) {
144- t .Helper ()
145- info , err := os .Stat (path )
146- if err != nil {
147- t .Fatalf ("stat %s: %v" , filepath .Base (path ), err )
148- }
149- if got := info .Mode ().Perm () & mask ; got != want {
150- t .Errorf (
151- "%s perm & %o = %o, want %o" ,
152- filepath .Base (path ), mask , got , want ,
153- )
154- }
155- }
156-
15786func loadConfigFromFlags (t * testing.T , args ... string ) (Config , error ) {
15887 t .Helper ()
15988 fs := flag .NewFlagSet ("test" , flag .ContinueOnError )
@@ -164,123 +93,6 @@ func loadConfigFromFlags(t *testing.T, args ...string) (Config, error) {
16493 return Load (fs )
16594}
16695
167- func TestMigrateFromLegacy (t * testing.T ) {
168- tests := []struct {
169- name string
170- legacyFiles map [string ]string
171- preCreateNew bool
172- wantFiles map [string ]string // Content to assert in new dir
173- wantMissing []string // Files that should NOT exist
174- }{
175- {
176- name : "CopiesGoDBAndConfig" ,
177- legacyFiles : map [string ]string {
178- "sessions-go.db" : "go-db-content" ,
179- "config.json" : `{"github_token": "secret"}` ,
180- },
181- wantFiles : map [string ]string {
182- "sessions.db" : "go-db-content" ,
183- "config.json" : `{"github_token": "secret"}` ,
184- },
185- },
186- {
187- name : "CopiesGoDBOnly" ,
188- legacyFiles : map [string ]string {
189- "sessions-go.db" : "just-db" ,
190- },
191- wantFiles : map [string ]string {
192- "sessions.db" : "just-db" ,
193- },
194- wantMissing : []string {"config.json" },
195- },
196- {
197- name : "IgnoresPythonDB" ,
198- legacyFiles : map [string ]string {
199- "sessions.db" : "python-db" ,
200- "config.json" : `{"github_token":"tok"}` ,
201- },
202- wantFiles : map [string ]string {
203- "config.json" : `{"github_token":"tok"}` ,
204- },
205- wantMissing : []string {"sessions.db" },
206- },
207- {
208- name : "SkipsIfNewDirExists" ,
209- legacyFiles : map [string ]string {
210- "sessions.db" : "db" ,
211- },
212- preCreateNew : true ,
213- wantMissing : []string {"sessions.db" },
214- },
215- {
216- name : "SkipsIfNoLegacyDir" ,
217- legacyFiles : nil ,
218- wantMissing : []string {"." },
219- },
220- }
221-
222- for _ , tt := range tests {
223- t .Run (tt .name , func (t * testing.T ) {
224- var tmp , newDir string
225- if tt .legacyFiles != nil {
226- tmp , newDir = setupLegacyEnv (t , tt .legacyFiles )
227- } else {
228- tmp = t .TempDir ()
229- newDir = filepath .Join (tmp , newDirName )
230- }
231-
232- if tt .preCreateNew {
233- if err := os .MkdirAll (newDir , 0o700 ); err != nil {
234- t .Fatal (err )
235- }
236- }
237-
238- t .Setenv ("HOME" , tmp )
239- MigrateFromLegacy (newDir )
240-
241- if tt .legacyFiles == nil {
242- if _ , err := os .Stat (newDir ); err == nil {
243- t .Error ("new dir should not be created without legacy dir" )
244- }
245- return
246- }
247-
248- for path , content := range tt .wantFiles {
249- assertFileContent (t , filepath .Join (newDir , path ), content )
250- }
251-
252- for _ , path := range tt .wantMissing {
253- if _ , err := os .Stat (filepath .Join (newDir , path )); err == nil {
254- t .Errorf ("file %s should not exist" , path )
255- }
256- }
257- })
258- }
259- }
260-
261- func TestMigrateFromLegacy_FilePermissions (t * testing.T ) {
262- if runtime .GOOS == "windows" {
263- t .Skip ("file permission checks not reliable on Windows" )
264- }
265-
266- tmp , newDir := setupLegacyEnv (t , map [string ]string {
267- "sessions-go.db" : "db" ,
268- "config.json" : `{"github_token":"x"}` ,
269- })
270-
271- t .Setenv ("HOME" , tmp )
272- MigrateFromLegacy (newDir )
273-
274- // Data dir must not be group/other accessible
275- assertFilePerm (t , newDir , 0o077 , 0 )
276-
277- // config.json must not be group/other readable
278- assertFilePerm (t , filepath .Join (newDir , "config.json" ), 0o077 , 0 )
279-
280- // sessions.db should be owner-accessible
281- assertFilePerm (t , filepath .Join (newDir , "sessions.db" ), 0o400 , 0o400 )
282- }
283-
28496func TestLoadEnv_OverridesDataDir (t * testing.T ) {
28597 custom , _ := setupConfigDir (t )
28698
@@ -434,37 +246,3 @@ func TestResolveDataDir_DefaultAndEnvOverride(t *testing.T) {
434246 }
435247}
436248
437- // TestMigrateThenLoad_GithubTokenAvailable verifies that the
438- // startup sequence (resolve data dir, migrate, load) makes
439- // legacy github_token immediately available without a second
440- // load.
441- func TestMigrateThenLoad_GithubTokenAvailable (t * testing.T ) {
442- cfgJSON , _ := json .Marshal (map [string ]string {
443- "github_token" : "legacy-secret" ,
444- })
445- tmp , newDir := setupLegacyEnv (t , map [string ]string {
446- "config.json" : string (cfgJSON ),
447- })
448-
449- t .Setenv ("HOME" , tmp )
450- t .Setenv ("AGENT_VIEWER_DATA_DIR" , newDir )
451-
452- // Simulate startup: resolve, migrate, then load
453- dataDir , err := ResolveDataDir ()
454- if err != nil {
455- t .Fatal (err )
456- }
457- MigrateFromLegacy (dataDir )
458-
459- cfg , err := LoadMinimal ()
460- if err != nil {
461- t .Fatal (err )
462- }
463-
464- if cfg .GithubToken != "legacy-secret" {
465- t .Errorf (
466- "GithubToken = %q, want %q" ,
467- cfg .GithubToken , "legacy-secret" ,
468- )
469- }
470- }
0 commit comments