package app import ( "os" "path/filepath" "testing" ) // isolateConfig 把配置目录指到临时目录,避免测试污染真实的会话文件。 func isolateConfig(t *testing.T) { t.Helper() dir := t.TempDir() t.Setenv("AppData", dir) t.Setenv("XDG_CONFIG_HOME", dir) } func TestSessionRoundTrip(t *testing.T) { isolateConfig(t) root := t.TempDir() p1 := filepath.Join(root, "a.go") p2 := filepath.Join(root, "b.go") for _, p := range []string{p1, p2} { if err := os.WriteFile(p, []byte("package x\n"), 0o644); err != nil { t.Fatal(err) } } a := &App{ root: root, bps: map[string]map[int]breakpoint{p1: {3: {Enabled: true}, 7: {Enabled: true}}}, sidebar: 320, sidebarOn: false, panel: true, panelTab: 2, panelH: 260, panelKind: 1, light: true, shellIdx: 2, } a.docs = []*Doc{{path: p1}, {path: p2}} a.active = p2 a.saveSession() st, ok := loadSession(root) if !ok { t.Fatal("没有读到会话") } if !st.Panel || st.PanelTab != 2 || st.PanelH != 260 { t.Fatalf("面板状态不对: %+v", st) } if st.Sidebar || st.SidebarW != 320 || st.PanelKind != 1 { t.Fatalf("侧栏状态不对: %+v", st) } if len(st.Tabs) != 2 || st.Tabs[0] != p1 || st.Tabs[1] != p2 { t.Fatalf("标签不对: %+v", st.Tabs) } if st.Active != p2 { t.Fatalf("当前标签不对: %q", st.Active) } if len(st.Breakpoints[p1]) != 2 { t.Fatalf("断点不对: %+v", st.Breakpoints) } if !st.Light || st.ShellIdx != 2 { t.Fatalf("主题/外壳不对: %+v", st) } } func TestLastWorkspace(t *testing.T) { isolateConfig(t) root := t.TempDir() a := &App{root: root, bps: map[string]map[int]breakpoint{}} a.saveSession() got := lastWorkspace() if filepath.Clean(got) != filepath.Clean(root) { t.Fatalf("上次工作区应是 %q,实际 %q", root, got) } } // TestLastWorkspaceGone 确认目录被删后不会返回失效路径。 func TestLastWorkspaceGone(t *testing.T) { isolateConfig(t) root := t.TempDir() a := &App{root: root, bps: map[string]map[int]breakpoint{}} a.saveSession() if err := os.RemoveAll(root); err != nil { t.Fatal(err) } if got := lastWorkspace(); got != "" { t.Fatalf("目录不存在时不该返回 %q", got) } } func TestSessionMissing(t *testing.T) { isolateConfig(t) if _, ok := loadSession(filepath.Join(t.TempDir(), "nope")); ok { t.Fatal("不存在的会话不该读成功") } }