| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestOpenCodeGoInferencePreviousReaderRoundTrip(t *testing.T) { |
| 13 | checkout := os.Getenv("REASONIX_OPENCODE_PREVIOUS_CHECKOUT") |
| 14 | if checkout == "" { |
| 15 | t.Skip("set REASONIX_OPENCODE_PREVIOUS_CHECKOUT for schema-v12 reader acceptance") |
| 16 | } |
| 17 | checkout, err := filepath.EvalSymlinks(checkout) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | dir := t.TempDir() |
| 22 | t.Setenv("REASONIX_HOME", dir) |
| 23 | path := filepath.Join(dir, "config.toml") |
| 24 | raw := `config_version = 12 |
| 25 | default_model = "go/deepseek-v4-pro" |
| 26 | [[providers]] |
| 27 | name = "go" |
| 28 | kind = "openai" |
| 29 | base_url = "https://opencode.ai/zen/go/v1" |
| 30 | request_url = "https://opencode.ai/zen/go/v1/chat/completions" |
| 31 | models_url = "https://opencode.ai/zen/go/v1/models" |
| 32 | models = ["deepseek-v4-pro"] |
| 33 | api_key_env = "USER_GO_KEY" |
| 34 | price = { input = 9.0, output = 27.0, cache_hit = 0.3, currency = "¥" } |
| 35 | ` |
| 36 | if err := os.WriteFile(path, []byte(raw), 0600); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if _, err := ApplyUserConfigUpgradesOnStartup(path); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | probe := filepath.Join(dir, "previous_reader_test.go") |
| 43 | source := `package config |
| 44 | import ("os"; "slices"; "testing") |
| 45 | func TestOpenCodeInferencePreviousReaderProbe(t *testing.T) { |
| 46 | if Default().ConfigVersion != 12 { t.Fatal("probe requires schema-v12 reader") } |
| 47 | path := os.Getenv("REASONIX_COMPAT_CONFIG") |
| 48 | c := LoadForEdit(path) |
| 49 | p,ok := c.Provider("go") |
| 50 | if !ok || c.ConfigVersion != 13 || p.BaseURL != "https://opencode.ai/inference/go/openai/v1" || p.DefaultModel() != "deepseek-v4-pro" { t.Fatal("old reader lost route, selection or marker") } |
| 51 | if p.RequestURL != p.BaseURL+"/chat/completions" || p.ModelsURL != "https://opencode.ai/inference/go/v1/models" { t.Fatal("old reader lost request/discovery endpoint") } |
| 52 | if os.Getenv("REASONIX_COMPAT_DELETE_FLASH") == "1" { |
| 53 | p.Models = slices.DeleteFunc(p.Models,func(m string) bool { return m == "deepseek-v4.1-flash" }) |
| 54 | } else if !p.HasModel("deepseek-v4.1-flash") { t.Fatal("old reader lost new model") } |
| 55 | p.DisplayName = "old-reader-edited" |
| 56 | if err := c.SaveToScope(path,RenderScopeFull); err != nil { t.Fatal(err) } |
| 57 | }` |
| 58 | if err := os.WriteFile(probe, []byte(source), 0600); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | data, err := json.Marshal(map[string]any{"Replace": map[string]string{filepath.Join(checkout, "internal/config/mimo_catalog_downgrade_test.go"): probe}}) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | overlay := filepath.Join(dir, "overlay.json") |
| 66 | if err := os.WriteFile(overlay, data, 0600); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | for _, deleted := range []string{"0", "1"} { |
| 70 | cmd := exec.Command("go", "test", "-overlay", overlay, "./internal/config", "-run", "^TestOpenCodeInferencePreviousReaderProbe$", "-v", "-count=1") |
| 71 | cmd.Dir = checkout |
| 72 | cmd.Env = append(os.Environ(), "REASONIX_COMPAT_CONFIG="+path, "REASONIX_COMPAT_DELETE_FLASH="+deleted) |
| 73 | output, err := cmd.CombinedOutput() |
| 74 | if err != nil || !strings.Contains(string(output), "--- PASS: TestOpenCodeInferencePreviousReaderProbe") { |
| 75 | t.Fatalf("old reader: %v\n%s", err, output) |
| 76 | } |
| 77 | if changed, err := ApplyUserConfigUpgradesOnStartup(path); err != nil || changed { |
| 78 | t.Fatalf("old writer lost marker: %v %v", changed, err) |
| 79 | } |
| 80 | c := LoadForEdit(path) |
| 81 | p, ok := c.Provider("go") |
| 82 | if !ok || p.DisplayName != "old-reader-edited" || p.HasModel("deepseek-v4.1-flash") != (deleted == "0") || p.PriceForModel("deepseek-v4.1-flash").Input != 0.3 || p.APIKeyEnv != "USER_GO_KEY" || c.DefaultModel != "go/deepseek-v4-pro" || c.ConfigVersion != openCodeGoInferenceUpgradeVersion { |
| 83 | t.Fatalf("old writer lost fields: %+v", p) |
| 84 | } |
| 85 | // All fields introduced by this migration already exist in schema 12. |
| 86 | // Unknown-field preservation during migration is covered separately; |
| 87 | // the previous full renderer never retained arbitrary unknown fields. |
| 88 | } |
| 89 | } |
| 90 |