Files
magpie/internal/gui/codex_titles_test.go
T
yetone 83368d9946 codex: thread descriptions can be turned off, as titles can (cybercaius)
cybercaius on Discord asked to turn Codex's thread descriptions off.
Titles and predictions had an Off; descriptions only had "as Codex
sends it" or a model.

Codex's settings on the Agents page now offer Off for "Thread
descriptions", worded as the titles' Off ("Off", "no description,
nothing sent", in zh, zh-TW, ja and de too). settings.CodexDescriptions
takes "off", /api/settings/codex-descriptions accepts it, and the
gateway answers a thread_description side turn itself through
sentNowhere, as titles' Off does: a finished response.completed with no
output, nothing sent upstream, and Routing/Usage recording it as
answered by magpie with the reason. Both routes take it (the Codex
backend path and /v1/responses).

Why an empty reply is enough: in ChatGPT.app's app.asar the
thread_description helper reads the reply's {"description"}; with no
message it records an "empty" result, not an error, and the thread's
description is left as it was.

The docs (gateway-routing.md) say each of titles and descriptions has
its own Off and neither turns off the other. No CLI change: titles' Off
isn't settable from the CLI either. docs/reference.md doesn't list
these settings.

Run:
- gofmt -l: nothing. go vet: darwin ./..., GOOS=linux -tags nogui
  ./..., GOOS=windows ./...: ok.
- go test -timeout 45m ./internal/gateway ./internal/gui
  ./internal/settings before the rebase: gui ok, settings ok; gateway
  had TestDisabledCodexModels ("no space left on device") and
  TestHistoryKeepsDaysAndDropsOld red during a moment the disk was
  full; both pass -count=5.
- Rebased on f0ffee34c: TestCodexDescriptions/Titles/Predictions in
  gateway, gui and settings ok; go vet of the three packages ok.
- Whole GUI suite (916 files, both engines, every language,
  --test-concurrency=4, box at load 100 to 200 from other sessions):
  13328 tests, 13144 pass, 184 fail. The 33 red files were run again
  on main 508911e43: the same tests red there (quota-refresh-order
  #3679, routing-* #3408 #3409 #3410 #3411 #3412 #3799 #1612 #2658,
  gateway-caller-keys #3797, menu-input-scroll #3402, key-replace
  #3476, phone-web #3180, old-webkit #3403, header-centre #3479,
  quota-count-wrap #3404, icon-tile #3858 new, provider-detect-draft
  #3859 new, ...). The rest, run again at the rebased head: green
  except s3-sync (#3413), search-first (#3795), usage-agents-hide
  (#3414), usage-fold (#3415), usage-ledger, all red the same on
  f0ffee34c, and library-row-remove 1 of 3 (#3099). codex-descriptions,
  codex-titles, codex-predictions and i18n-keys green at the rebased
  head.

Broken by hand:
- the gateway's off branch removed: gateway TestCodexDescriptionsSetting
  red (magpie knows no model "off", no response.completed);
- the API's "off" check removed: gui TestCodexDescriptionsSetting red
  (400);
- settings validation without "off": gui and gateway tests red (400 /
  "must be a model's id ... not \"off\"");
- the picker's Off option removed: codex-descriptions.test.cjs red in
  chromium and webkit, en and zh.

Not tried: a real Codex app session with descriptions Off; the app's
side is read from its minified code only.
2026-10-27 15:30:31 +08:00

154 lines
6.8 KiB
Go

package gui
import (
"encoding/json"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/agentenv"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// Settings' Codex thread titles (#705) is set on its own: off, a model
// magpie serves (one it doesn't is refused), or back to Codex's own; the
// Settings page's other saves, which never send it, keep it; and the page
// is told the models it may name.
func TestCodexTitlesSetting(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
for _, v := range agentenv.Vars {
t.Setenv(v, "")
}
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: "http://127.0.0.1:9/v1"}); err != nil {
t.Fatal(err)
}
call := func(path, body string) (int, map[string]any) {
t.Helper()
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
var out map[string]any
json.Unmarshal(rec.Body.Bytes(), &out)
return rec.Code, out
}
code, out := call("/api/settings/codex-titles", `{"model":"off"}`)
if code != 200 || out["codexTitles"] != "off" || settings.Load().CodexTitles != "off" {
t.Fatalf("off: %d %v", code, out["codexTitles"])
}
found := false
for _, m := range out["titleModels"].([]any) {
if m.(map[string]any)["id"] == "fake/m1" {
found = true
}
}
if !found {
t.Errorf("titleModels %v has no fake/m1", out["titleModels"])
}
if code, _ := call("/api/settings", `{"theme":"dark"}`); code != 200 || settings.Load().CodexTitles != "off" {
t.Errorf("another save (%d) lost the setting: %q", code, settings.Load().CodexTitles)
}
if code, _ := call("/api/settings/codex-titles", `{"model":"nope/x"}`); code < 400 || settings.Load().CodexTitles != "off" {
t.Errorf("a model magpie doesn't serve: %d, %q", code, settings.Load().CodexTitles)
}
if code, out := call("/api/settings/codex-titles", `{"model":"fake/m1"}`); code != 200 || out["codexTitles"] != "fake/m1" {
t.Errorf("a model: %d %v", code, out["codexTitles"])
}
if code, _ := call("/api/settings/codex-titles", `{"model":""}`); code != 200 || settings.Load().CodexTitles != "" {
t.Errorf("Codex's own: %d %q", code, settings.Load().CodexTitles)
}
}
func TestCodexDescriptionsSetting(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
for _, v := range agentenv.Vars {
t.Setenv(v, "")
}
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: "http://127.0.0.1:9/v1"}); err != nil {
t.Fatal(err)
}
call := func(path, body string) (int, map[string]any) {
t.Helper()
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
var out map[string]any
json.Unmarshal(rec.Body.Bytes(), &out)
return rec.Code, out
}
code, out := call("/api/settings/codex-descriptions", `{"model":"fake/m1"}`)
if code != 200 || out["codexDescriptions"] != "fake/m1" || settings.Load().CodexDescriptions != "fake/m1" {
t.Fatalf("model: %d %v", code, out["codexDescriptions"])
}
if code, _ := call("/api/settings", `{"theme":"dark"}`); code != 200 || settings.Load().CodexDescriptions != "fake/m1" {
t.Errorf("another save lost the setting: %q", settings.Load().CodexDescriptions)
}
if code, _ := call("/api/settings/codex-descriptions", `{"model":"nope/x"}`); code < 400 || settings.Load().CodexDescriptions != "fake/m1" {
t.Errorf("unknown model: %d %q", code, settings.Load().CodexDescriptions)
}
// off, as titles' (cybercaius on Discord), kept by another save
if code, out := call("/api/settings/codex-descriptions", `{"model":"off"}`); code != 200 || out["codexDescriptions"] != "off" || settings.Load().CodexDescriptions != "off" {
t.Errorf("off: %d %v %q", code, out["codexDescriptions"], settings.Load().CodexDescriptions)
}
if code, _ := call("/api/settings", `{"theme":"light"}`); code != 200 || settings.Load().CodexDescriptions != "off" {
t.Errorf("another save (%d) lost off: %q", code, settings.Load().CodexDescriptions)
}
if code, _ := call("/api/settings/codex-descriptions", `{"model":""}`); code != 200 || settings.Load().CodexDescriptions != "" {
t.Errorf("Codex's own: %d %q", code, settings.Load().CodexDescriptions)
}
}
// Codex's composer predictions (#1970): a model, off, or Codex's own,
// kept by a save of the rest of Settings.
func TestCodexPredictionsSetting(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
for _, v := range agentenv.Vars {
t.Setenv(v, "")
}
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: "http://127.0.0.1:9/v1"}); err != nil {
t.Fatal(err)
}
call := func(path, body string) (int, map[string]any) {
t.Helper()
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
var out map[string]any
json.Unmarshal(rec.Body.Bytes(), &out)
return rec.Code, out
}
code, out := call("/api/settings/codex-predictions", `{"model":"fake/m1"}`)
if code != 200 || out["codexPredictions"] != "fake/m1" || settings.Load().CodexPredictions != "fake/m1" {
t.Fatalf("model: %d %v", code, out["codexPredictions"])
}
if code, _ := call("/api/settings", `{"theme":"dark"}`); code != 200 || settings.Load().CodexPredictions != "fake/m1" {
t.Errorf("another save lost the setting: %q", settings.Load().CodexPredictions)
}
if code, _ := call("/api/settings/codex-predictions", `{"model":"nope/x"}`); code < 400 || settings.Load().CodexPredictions != "fake/m1" {
t.Errorf("unknown model: %d %q", code, settings.Load().CodexPredictions)
}
if code, _ := call("/api/settings/codex-predictions", `{"model":"off"}`); code != 200 || settings.Load().CodexPredictions != "off" {
t.Errorf("off: %d %q", code, settings.Load().CodexPredictions)
}
if code, _ := call("/api/settings/codex-predictions", `{"model":""}`); code != 200 || settings.Load().CodexPredictions != "" {
t.Errorf("Codex's own: %d %q", code, settings.Load().CodexPredictions)
}
}