| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/agent" |
| 9 | "reasonix/internal/config" |
| 10 | "reasonix/internal/control" |
| 11 | ) |
| 12 | |
| 13 | func TestOpenCodeGoCLIResumeAndExplicitSelection(t *testing.T) { |
| 14 | dir := t.TempDir() |
| 15 | configPath := filepath.Join(dir, "config.toml") |
| 16 | if err := os.WriteFile(configPath, []byte(`config_version = 9 |
| 17 | [[providers]] |
| 18 | name = "go" |
| 19 | kind = "anthropic" |
| 20 | base_url = "https://opencode.ai/zen/go" |
| 21 | api_key_env = "CLI_SELECTION_KEY" |
| 22 | model = "deepseek-v4-flash" |
| 23 | `), 0600); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if _, err := config.ApplyUserConfigUpgradesOnStartup(configPath); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | cfg := config.LoadForEdit(configPath) |
| 30 | cfg.Providers[0].NoProxy = true |
| 31 | const ref = "go/deepseek-v4-flash" |
| 32 | path := filepath.Join(dir, "session.jsonl") |
| 33 | s := agent.NewSession("fixture") |
| 34 | if err := s.Save(path); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if err := agent.SetBranchModelPreserveUpdated(path, ref); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if _, err := modelForResumePath("", path, cfg); err == nil { |
| 41 | t.Fatal("implicit CLI resume adopted changed connection") |
| 42 | } |
| 43 | if got, err := modelForResumePath(ref, path, cfg); err != nil || got != ref { |
| 44 | t.Fatalf("explicit CLI selection blocked: %q, %v", got, err) |
| 45 | } |
| 46 | ctrl := newOwnedTestController(t, control.Options{ModelRef: ref, ModelIdentity: cfg.ModelSelectionIdentity(ref)}) |
| 47 | defer ctrl.Close() |
| 48 | ctrl.Resume(s, path) |
| 49 | if err := persistCLIModelSelection(ctrl); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if got, err := modelForResumePath("", path, cfg); err != nil || got != ref { |
| 53 | t.Fatalf("acknowledged CLI resume failed: %q, %v", got, err) |
| 54 | } |
| 55 | copyPath, err := copySessionForWriting(path) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if got, err := modelForResumePath("", copyPath, cfg); err != nil || got != ref { |
| 60 | t.Fatalf("copied session lost acknowledgement: %q, %v", got, err) |
| 61 | } |
| 62 | cfg.Providers[0].APIKeyEnv = "CHANGED_AGAIN" |
| 63 | if _, err := modelForResumePath("", copyPath, cfg); err == nil { |
| 64 | t.Fatal("copy silently adopted a later account change") |
| 65 | } |
| 66 | } |
| 67 |