102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestWriteCodexProfile(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "config.toml")
|
|
if err := writeCodexProfile(path, "http://127.0.0.1:3456/v1/"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content := string(b)
|
|
for _, want := range []string{
|
|
"[profiles.ocgo-launch]",
|
|
`openai_base_url = "http://127.0.0.1:3456/v1/"`,
|
|
`forced_login_method = "api"`,
|
|
`model_provider = "ocgo-launch"`,
|
|
`model_catalog_json = `,
|
|
"[model_providers.ocgo-launch]",
|
|
`name = "OpenCode Go"`,
|
|
`base_url = "http://127.0.0.1:3456/v1/"`,
|
|
`wire_api = "responses"`,
|
|
} {
|
|
if !strings.Contains(content, want) {
|
|
t.Fatalf("missing %q in:\n%s", want, content)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWriteCodexProfileReplacesExistingSections(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "config.toml")
|
|
existing := "[profiles.ocgo-launch]\nopenai_base_url = \"http://old/v1/\"\n\n[other]\nkey = \"value\"\n\n[model_providers.ocgo-launch]\nbase_url = \"http://old/v1/\"\n"
|
|
if err := os.WriteFile(path, []byte(existing), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := writeCodexProfile(path, "http://new/v1/"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := os.ReadFile(path)
|
|
content := string(b)
|
|
if strings.Contains(content, "http://old") {
|
|
t.Fatalf("old profile was not replaced:\n%s", content)
|
|
}
|
|
if strings.Count(content, "[profiles.ocgo-launch]") != 1 || strings.Count(content, "[model_providers.ocgo-launch]") != 1 {
|
|
t.Fatalf("profile sections should be unique:\n%s", content)
|
|
}
|
|
if !strings.Contains(content, "[other]") || !strings.Contains(content, `key = "value"`) {
|
|
t.Fatalf("unrelated section was not preserved:\n%s", content)
|
|
}
|
|
}
|
|
|
|
func TestWriteCodexModelCatalog(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "ocgo-models.json")
|
|
if err := writeCodexModelCatalog(path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content := string(b)
|
|
for _, want := range []string{`"models"`, `"slug": "deepseek-v4-pro"`, `"context_window": 128000`, `"truncation_policy"`} {
|
|
if !strings.Contains(content, want) {
|
|
t.Fatalf("missing %q in:\n%s", want, content)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCompareVersions(t *testing.T) {
|
|
if compareVersions("0.80.9", "0.81.0") >= 0 {
|
|
t.Fatal("0.80.9 should be older")
|
|
}
|
|
if compareVersions("0.81.0", "0.81.0") != 0 {
|
|
t.Fatal("same versions should compare equal")
|
|
}
|
|
if compareVersions("codex-cli", "0.81.0") >= 0 {
|
|
t.Fatal("invalid version should compare as old")
|
|
}
|
|
if compareVersions("0.87.0", "0.81.0") <= 0 {
|
|
t.Fatal("0.87.0 should be newer")
|
|
}
|
|
}
|
|
|
|
func TestResponsesInputToMessages(t *testing.T) {
|
|
messages := responsesInputToMessages([]byte(`[{"type":"message","role":"developer","content":"rules"},{"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]}]`))
|
|
if len(messages) != 2 {
|
|
t.Fatalf("got %d messages", len(messages))
|
|
}
|
|
if messages[0].Role != "system" || messages[0].Content != "rules" {
|
|
t.Fatalf("bad developer conversion: %+v", messages[0])
|
|
}
|
|
if messages[1].Role != "user" || messages[1].Content != "hello" {
|
|
t.Fatalf("bad user conversion: %+v", messages[1])
|
|
}
|
|
}
|