| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "reasonix/internal/config" |
| 7 | "reasonix/internal/control" |
| 8 | ) |
| 9 | |
| 10 | func TestDefaultTopicTitleLocalizesAtAPIBoundary(t *testing.T) { |
| 11 | app := &App{} |
| 12 | for _, tt := range []struct { |
| 13 | locale string |
| 14 | want string |
| 15 | }{ |
| 16 | {locale: "en-US", want: defaultTopicTitleEn}, |
| 17 | {locale: "zh-CN", want: defaultTopicTitle}, |
| 18 | {locale: "zh-TW", want: defaultTopicTitleZhTW}, |
| 19 | } { |
| 20 | app.setDesktopLocale(tt.locale) |
| 21 | if got := app.localizedTopicTitle(defaultTopicTitle, topicTitleSourceAuto); got != tt.want { |
| 22 | t.Fatalf("locale %q title = %q, want %q", tt.locale, got, tt.want) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestManualDefaultTopicTitleIsNotLocalized(t *testing.T) { |
| 28 | app := &App{} |
| 29 | for _, tt := range []struct { |
| 30 | locale string |
| 31 | title string |
| 32 | }{ |
| 33 | {locale: "zh-CN", title: defaultTopicTitleEn}, |
| 34 | {locale: "en-US", title: defaultTopicTitle}, |
| 35 | {locale: "zh-CN", title: defaultTopicTitleZhTW}, |
| 36 | } { |
| 37 | app.setDesktopLocale(tt.locale) |
| 38 | if got := app.localizedTopicTitle(tt.title, topicTitleSourceManual); got != tt.title { |
| 39 | t.Fatalf("locale %q manual title = %q, want %q", tt.locale, got, tt.title) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestCreateTopicPreservesManualDefaultTitle(t *testing.T) { |
| 45 | isolateDesktopUserDirs(t) |
| 46 | app := &App{} |
| 47 | app.projectTreeChangedHook = func() {} |
| 48 | app.setDesktopLocale("zh-CN") |
| 49 | |
| 50 | manual, err := app.CreateTopic("global", "", defaultTopicTitleEn) |
| 51 | if err != nil { |
| 52 | t.Fatalf("CreateTopic manual: %v", err) |
| 53 | } |
| 54 | if manual.Title != defaultTopicTitleEn { |
| 55 | t.Fatalf("manual title = %q, want %q", manual.Title, defaultTopicTitleEn) |
| 56 | } |
| 57 | if got := loadTopicTitleSource("", manual.ID); got != topicTitleSourceManual { |
| 58 | t.Fatalf("manual title source = %q, want %q", got, topicTitleSourceManual) |
| 59 | } |
| 60 | tree := app.ListProjectTree() |
| 61 | if len(tree) == 0 || len(tree[0].Children) == 0 || tree[0].Children[0].Label != defaultTopicTitleEn { |
| 62 | t.Fatalf("manual project-tree title was localized: %+v", tree) |
| 63 | } |
| 64 | |
| 65 | automatic, err := app.CreateTopic("global", "", "") |
| 66 | if err != nil { |
| 67 | t.Fatalf("CreateTopic automatic: %v", err) |
| 68 | } |
| 69 | if automatic.Title != defaultTopicTitle { |
| 70 | t.Fatalf("automatic title = %q, want localized %q", automatic.Title, defaultTopicTitle) |
| 71 | } |
| 72 | if err := app.RenameTopic(automatic.ID, defaultTopicTitleEn); err != nil { |
| 73 | t.Fatalf("RenameTopic manual default: %v", err) |
| 74 | } |
| 75 | if got := loadTopicTitleSource("", automatic.ID); got != topicTitleSourceManual { |
| 76 | t.Fatalf("renamed title source = %q, want %q", got, topicTitleSourceManual) |
| 77 | } |
| 78 | tree = app.ListProjectTree() |
| 79 | if len(tree) == 0 || len(tree[0].Children) == 0 || tree[0].Children[0].Label != defaultTopicTitleEn { |
| 80 | t.Fatalf("renamed project-tree title was localized: %+v", tree) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestDefaultTopicTitleVariantsRemainPersistenceSentinels(t *testing.T) { |
| 85 | for _, title := range []string{defaultTopicTitle, defaultTopicTitleEn, defaultTopicTitleZhTW} { |
| 86 | if !isDefaultTopicTitle(title) { |
| 87 | t.Fatalf("title %q was not recognized as a default sentinel", title) |
| 88 | } |
| 89 | } |
| 90 | if got := topicTitleFromText(defaultTopicTitleEn); got != "" { |
| 91 | t.Fatalf("English default title became a generated title: %q", got) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestForkTopicTitleUsesDesktopLocale(t *testing.T) { |
| 96 | app := &App{} |
| 97 | app.setDesktopLocale("en") |
| 98 | if got := app.forkTopicTitle("New session"); got != "Forked session" { |
| 99 | t.Fatalf("English fork title = %q", got) |
| 100 | } |
| 101 | app.setDesktopLocale("zh-TW") |
| 102 | if got := app.forkTopicTitle(defaultTopicTitle); got != "分叉會話" { |
| 103 | t.Fatalf("Traditional Chinese fork title = %q", got) |
| 104 | } |
| 105 | app.desktopLocale.Store(desktopLocaleUnknown) |
| 106 | if got := app.forkTopicTitle(""); got != "分叉会话" { |
| 107 | t.Fatalf("legacy fallback fork title = %q", got) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestSetTrayLocaleSchedulesAutoCurrencyRefresh(t *testing.T) { |
| 112 | isolateDesktopUserDirs(t) |
| 113 | cfg := config.Default() |
| 114 | if err := cfg.SaveTo(config.UserConfigPath()); err != nil { |
| 115 | t.Fatalf("save auto config: %v", err) |
| 116 | } |
| 117 | |
| 118 | app := NewApp() |
| 119 | app.projectTreeChangedHook = func() {} |
| 120 | activeCtrl := control.New(control.Options{Label: "active"}) |
| 121 | inactiveCtrl := control.New(control.Options{Label: "inactive"}) |
| 122 | t.Cleanup(activeCtrl.Close) |
| 123 | t.Cleanup(inactiveCtrl.Close) |
| 124 | app.tabs = map[string]*WorkspaceTab{ |
| 125 | "active": {ID: "active", Ctrl: activeCtrl}, |
| 126 | "inactive": {ID: "inactive", Ctrl: inactiveCtrl}, |
| 127 | } |
| 128 | app.activeTabID = "active" |
| 129 | app.setDesktopLocale("en") |
| 130 | |
| 131 | if err := app.SetTrayLocale("zh-CN"); err != nil { |
| 132 | t.Fatalf("SetTrayLocale: %v", err) |
| 133 | } |
| 134 | for _, tabID := range []string{"active", "inactive"} { |
| 135 | if !app.deferredRebuildPending(tabID) { |
| 136 | t.Fatalf("currency refresh for %q was not scheduled", tabID) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestSetTrayLocaleKeepsExplicitCurrencyRuntime(t *testing.T) { |
| 142 | isolateDesktopUserDirs(t) |
| 143 | cfg := config.Default() |
| 144 | cfg.Desktop.Currency = "USD" |
| 145 | cfg.ApplyDeepSeekOfficialDefaultPricing() |
| 146 | if err := cfg.SaveTo(config.UserConfigPath()); err != nil { |
| 147 | t.Fatalf("save explicit currency config: %v", err) |
| 148 | } |
| 149 | |
| 150 | app := NewApp() |
| 151 | app.projectTreeChangedHook = func() {} |
| 152 | ctrl := control.New(control.Options{Label: "active"}) |
| 153 | t.Cleanup(ctrl.Close) |
| 154 | app.tabs = map[string]*WorkspaceTab{"active": {ID: "active", Ctrl: ctrl}} |
| 155 | app.activeTabID = "active" |
| 156 | app.setDesktopLocale("en") |
| 157 | |
| 158 | if err := app.SetTrayLocale("zh-CN"); err != nil { |
| 159 | t.Fatalf("SetTrayLocale: %v", err) |
| 160 | } |
| 161 | if app.deferredRebuildPending("active") { |
| 162 | t.Fatal("explicit USD currency scheduled an automatic locale refresh") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestDesktopEffectivePricingCurrencyUsesLocaleOnlyForAuto(t *testing.T) { |
| 167 | app := NewApp() |
| 168 | app.setDesktopLocale("zh-CN") |
| 169 | |
| 170 | cfg := config.Default() |
| 171 | if got := app.desktopEffectivePricingCurrency(cfg); got != "CNY" { |
| 172 | t.Fatalf("auto pricing currency = %q, want CNY", got) |
| 173 | } |
| 174 | |
| 175 | cfg.Desktop.Language = "en" |
| 176 | cfg.ApplyDeepSeekOfficialDefaultPricing() |
| 177 | if got := app.desktopEffectivePricingCurrency(cfg); got != "USD" { |
| 178 | t.Fatalf("explicit desktop language pricing currency = %q, want USD", got) |
| 179 | } |
| 180 | |
| 181 | cfg.Desktop.Language = "" |
| 182 | cfg.Language = "en" |
| 183 | cfg.ApplyDeepSeekOfficialDefaultPricing() |
| 184 | if got := app.desktopEffectivePricingCurrency(cfg); got != "USD" { |
| 185 | t.Fatalf("explicit CLI language pricing currency = %q, want USD", got) |
| 186 | } |
| 187 | |
| 188 | cfg.Language = "" |
| 189 | cfg.Desktop.Currency = "USD" |
| 190 | cfg.ApplyDeepSeekOfficialDefaultPricing() |
| 191 | if got := app.desktopEffectivePricingCurrency(cfg); got != "USD" { |
| 192 | t.Fatalf("explicit currency pricing currency = %q, want USD", got) |
| 193 | } |
| 194 | } |
| 195 |