Files
magpie/internal/edit/toml_array_test.go
yetone 3fd7e16983 agent: PenguinHarness is installed from the Agents page and served by magpie with nothing to set up
rankyurky asked on Discord #feedback for PenguinHarness (npm
@prismshadow/penguin-cli, command penguin). Every value comes from
Penguin's own source (0.2.17):

- Config: $PENGUIN_HOME (else ~/.penguin/data, every OS) /
  $PENGUIN_PROJECT_ID (else default_project) /.project_config.toml.
  magpie writes [providers.magpie] (base_url, client_type, api_key),
  one [[models]] entry per magpie model (context_window, vision,
  max_tokens, [models.pricing] in usd_per_mtok) and default_model in
  Penguin's own inline spelling. Penguin's server reads the file again
  when its mtime changes, so no restart; a default naming no entry is
  refused by Penguin, so Check fails when one is missing.
- A file magpie creates is 0600 in a 0700 folder, as Penguin keeps it.
  Disconnect restores the user's own default_model (or none), drops a
  magpie vision model, and removes the file when magpie made it.
  Penguin's own entries, providers and command_policy rules are kept.
- A sync that changes nothing writes nothing (bytes and mtime kept), so
  Penguin's watcher isn't woken.
- Install: curl -fsSL https://penguin.ooo/install.sh | sh, or
  irm https://penguin.ooo/install.ps1 | iex on Windows; update: npm, or
  penguin update --yes for the tarball, except on Windows where
  update.ts refuses it.
- WSL: a penguin kind with no restart notice; the WSL test now expects
  an empty Notice for a kind with no restart.
- agentenv: PENGUIN_HOME, PENGUIN_PROJECT_ID; the project id is in
  NotPaths, or appdir dropped a value like "work" as a relative path.
- edit.SetTOMLArrayTables: replaces every [[name]] entry with key =
  value (with child tables) by blocks, or removes them; blank lines
  between replaced entries go with them, and an unchanged result isn't
  written.
- GUI: penguin-color.svg icon; README (en, zh-CN), docs/reference.md
  and docs/subsystems/agent-wiring.md.

Run (worktree at ca9b5048e, GOMODCACHE/GOCACHE pinned):
- go test -tags nogui -timeout 45m ./internal/agent ./internal/edit
  ./internal/agentenv ./internal/appdir ./internal/usage: all ok but
  TestWSLMoreAgentsPick (penguin has no restart notice), fixed in the
  test; rerun -run 'TestWSL|TestPenguin|TestEveryAgent|TestFolderVars|
  TestEveryNotice' ok.
- internal/gui: TestCoreScriptBudget, TestAssetTypesHoldAgainstTheRegistry,
  icon and i18n guards ok; no budget raised.
- GUI Playwright, chromium and webkit: i18n-keys, getting-started,
  agent-cli-missing, agent-update-all, gui-ja, gui-de, gui-zh-tw:
  123 tests, 123 pass. The icon renders in colour in both engines.
- gofmt -l internal/ clean; go vet -tags nogui ./... with GOOS linux,
  windows and darwin clean.
- Real Penguin 0.2.17 (npm, Node 25) in a sandbox HOME against a sandbox
  magpie serve and a fake upstream: penguin run -m "Say PONG" answered
  "PONG from fake" through magpie (usage agent "penguin",
  /v1/responses -> /v1/chat/completions); switching the model in magpie
  took effect with no Penguin restart; a no-op sync kept bytes and
  mtime; magpie penguin default restored the user's custom/my-own and
  left no magpie line; with no prior file it was created 0600 and
  removed on disconnect. The fixture is the file Penguin itself wrote.

- Rebased on origin/main d9a8148d7 and run again there: internal/agent -run
  'TestPenguin|TestWSL|TestEveryAgent|TestFolderVars|TestEveryNotice|
  TestSync', internal/edit, internal/agentenv and the internal/gui
  guards ok; gofmt clean.

Broken by hand, each went red: the no-op skip (mtime test), the 0600
create, the inline default_model spelling, the own-default restore,
removing magpie's file, dropping the vision model, the NotPaths entry,
and both SetTOMLArrayTables fixes (blank-line growth, unchanged write).

Not tried: the full ./... suite (machine load ~190-200, as asked); a
Windows or WSL real binary; Penguin's desktop app; a real vendor key
through Penguin. Penguin sends the openai SDK's User-Agent, so the
agent is named from its key, not its UA.
2026-10-27 01:38:45 +08:00

238 lines
7.1 KiB
Go

package edit
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestTOMLArrayTableEditsOnlyNamedEntry(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
before := `# global
default_model = "native/a"
[[providers]]
name = "native"
models = [
"a", # keep
"b",
]
[providers.headers]
x = "keep"
[[ 'providers' ]]
name = 'magpie'
models = ["old"]
[providers.model_overrides."old"]
context_window = 10
note = """
[[providers]]
name = "fake"
"""
# keep the next section's comment
[[providers]]
name = "last"
models = ["z"]
[ui]
theme = "dark"
`
if err := os.WriteFile(path, []byte(before), 0o600); err != nil {
t.Fatal(err)
}
block := "[[providers]]\nname = \"magpie\"\nmodels = [\"new\"]\n[providers.model_overrides.\"new\"]\ncontext_window = 100\n"
if err := SetTOMLArrayTable(path, "providers", "name", "magpie", block); err != nil {
t.Fatal(err)
}
got, _ := os.ReadFile(path)
keep := before[:strings.Index(before, "[[ 'providers' ]]")]
tail := before[strings.Index(before, "# keep the next"):]
if !strings.HasPrefix(string(got), keep) || !strings.HasSuffix(string(got), tail) || strings.Contains(string(got), "context_window = 10\n") {
t.Fatalf("neighboring data or the owned subtree was edited incorrectly:\n%s", got)
}
if err := DelTOMLArrayTable(path, "providers", "name", "magpie"); err != nil {
t.Fatal(err)
}
got, _ = os.ReadFile(path)
if !strings.HasPrefix(string(got), keep) || !strings.HasSuffix(string(got), tail) || strings.Contains(string(got), `name = "magpie"`) {
t.Fatalf("delete changed another entry:\n%s", got)
}
}
func TestTOMLArrayTableRejectsAmbiguityAndInvalidInput(t *testing.T) {
for _, input := range []string{
"[[providers]]\nname = \"magpie\"\n[[providers]]\nname = \"magpie\"\n",
"[[providers]]\nname = \"other\"\nname = \"duplicate\"\n",
"[[providers]]\nname = [\n",
"providers = [{ name = \"native\" }]\n",
} {
t.Run(input, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
os.WriteFile(path, []byte(input), 0o600)
if err := SetTOMLArrayTable(path, "providers", "name", "magpie", "[[providers]]\nname = \"magpie\"\n"); err == nil {
t.Fatal("accepted unsafe input")
}
got, _ := os.ReadFile(path)
if string(got) != input {
t.Fatalf("failed edit changed the file: %s", got)
}
})
}
path := filepath.Join(t.TempDir(), "config.toml")
if err := SetTOMLArrayTable(path, "providers", "name", "magpie", "[[providers]]\nname = \"foreign\"\n"); err == nil {
t.Fatal("accepted a mismatching replacement")
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatal("invalid replacement created a file")
}
}
func TestTOMLTopKeepsCommentAndSpacing(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
os.WriteFile(path, []byte("default_model = 'native/a' # chosen by me\n[ui]\ntheme = 'dark'\n"), 0o600)
if err := SetTOMLTopPreserving(path, KV{Path: "default_model", Value: "magpie/group/code"}); err != nil {
t.Fatal(err)
}
got, _ := os.ReadFile(path)
if string(got) != "default_model = \"magpie/group/code\" # chosen by me\n[ui]\ntheme = 'dark'\n" {
t.Fatalf("lost formatting: %s", got)
}
}
func TestTOMLArrayTableInterleavedChildren(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
input := "[[providers]]\nname = \"magpie\"\n[ui]\ntheme = \"dark\"\n[providers.model_overrides.\"old\"]\ncontext_window = 10\n[[providers]]\nname = \"native\"\n"
os.WriteFile(path, []byte(input), 0o600)
if err := SetTOMLArrayTable(path, "providers", "name", "magpie", "[[providers]]\nname = \"magpie\"\nmodels = [\"new\"]\n"); err != nil {
t.Fatal(err)
}
got, _ := os.ReadFile(path)
if strings.Contains(string(got), "context_window = 10") || !strings.Contains(string(got), "[ui]\ntheme = \"dark\"") || !strings.Contains(string(got), "name = \"native\"") {
t.Fatalf("interleaved subtree: %s", got)
}
}
// SetTOMLArrayTables replaces every matching entry in one place, keeps the
// others and what lies between them, and a second run with the same blocks
// leaves the file's bytes and time alone.
func TestTOMLArrayTablesReplaceEveryMatch(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
before := `default_model = { provider = "custom", model_id = "own" }
[[models]]
provider = "magpie"
model_id = "old-1"
[models.pricing]
output = 1.0
[[models]]
provider = "magpie"
model_id = "old-2"
[[models]]
provider = "custom"
model_id = "own"
[models.pricing]
output = 2.0
[[models]]
provider = "magpie"
model_id = "old-3"
[[command_policy.rules]]
pattern = "rm"
`
if err := os.WriteFile(path, []byte(before), 0o600); err != nil {
t.Fatal(err)
}
blocks := []string{
"[[models]]\nprovider = \"magpie\"\nmodel_id = \"new-1\"\n\n[models.pricing]\noutput = 3.0\n",
"[[models]]\nprovider = \"magpie\"\nmodel_id = \"new-2\"\n",
}
if err := SetTOMLArrayTables(path, "models", "provider", "magpie", blocks); err != nil {
t.Fatal(err)
}
want := `default_model = { provider = "custom", model_id = "own" }
[[models]]
provider = "magpie"
model_id = "new-1"
[models.pricing]
output = 3.0
[[models]]
provider = "magpie"
model_id = "new-2"
[[models]]
provider = "custom"
model_id = "own"
[models.pricing]
output = 2.0
[[command_policy.rules]]
pattern = "rm"
`
got, _ := os.ReadFile(path)
if string(got) != want {
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
}
old := time.Now().Add(-time.Hour).Truncate(time.Second)
if err := os.Chtimes(path, old, old); err != nil {
t.Fatal(err)
}
if err := SetTOMLArrayTables(path, "models", "provider", "magpie", blocks); err != nil {
t.Fatal(err)
}
got, _ = os.ReadFile(path)
if string(got) != want {
t.Fatalf("a second run changed the file:\n%s", got)
}
if st, err := os.Stat(path); err != nil || !st.ModTime().Equal(old) {
t.Fatalf("a second run wrote the file again: %v %v", st.ModTime(), err)
}
if err := SetTOMLArrayTables(path, "models", "provider", "magpie", nil); err != nil {
t.Fatal(err)
}
want = `default_model = { provider = "custom", model_id = "own" }
[[models]]
provider = "custom"
model_id = "own"
[models.pricing]
output = 2.0
[[command_policy.rules]]
pattern = "rm"
`
got, _ = os.ReadFile(path)
if string(got) != want {
t.Fatalf("after removal got:\n%s\nwant:\n%s", got, want)
}
}
func TestTOMLArrayTablesAppendAndRefuse(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte("x = 1\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := SetTOMLArrayTables(path, "models", "provider", "magpie", []string{"[[models]]\nprovider = \"magpie\"\nmodel_id = \"a\"\n"}); err != nil {
t.Fatal(err)
}
if got, _ := os.ReadFile(path); string(got) != "x = 1\n\n[[models]]\nprovider = \"magpie\"\nmodel_id = \"a\"\n" {
t.Fatalf("append got %q", got)
}
for _, bad := range []string{
"[[models]]\nprovider = \"other\"\n",
"[[models]]\nprovider = \"magpie\"\n[[models]]\nprovider = \"magpie\"\n",
"[[models]]\nprovider = \"magpie\"\n[ui]\nx = 1\n",
"y = 2\n[[models]]\nprovider = \"magpie\"\n",
"[models]\nprovider = \"magpie\"\n",
} {
if err := SetTOMLArrayTables(path, "models", "provider", "magpie", []string{bad}); err == nil {
t.Errorf("block %q was taken", bad)
}
}
}