Files
zzet--gortex/cmd/gortex/instructions_test.go
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

92 lines
2.7 KiB
Go

package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/zzet/gortex/internal/profiles"
)
// sandboxInstructionsEnv points the profile dir (XDG_DATA_HOME) and the
// home dir (skills sync, pointer nudge) at temp dirs, and neutralises
// the profile env override so on-disk state decides.
func sandboxInstructionsEnv(t *testing.T) (home, dataDir string) {
t.Helper()
home = t.TempDir()
dataDir = t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_DATA_HOME", dataDir)
t.Setenv(profiles.ActiveEnv, "")
return home, dataDir
}
func captureCmd() (*cobra.Command, *bytes.Buffer, *bytes.Buffer) {
cmd := &cobra.Command{}
out, errOut := &bytes.Buffer{}, &bytes.Buffer{}
cmd.SetOut(out)
cmd.SetErr(errOut)
return cmd, out, errOut
}
func TestInstructionsSwitchListShowRegen(t *testing.T) {
_, _ = sandboxInstructionsEnv(t)
dir := profiles.DefaultDir()
// switch → files land, state recorded, caveat printed.
cmd, out, _ := captureCmd()
if err := runInstructionsSwitch(cmd, []string{"localization"}); err != nil {
t.Fatalf("switch: %v", err)
}
text := out.String()
if !strings.Contains(text, "NEW sessions only") {
t.Errorf("switch output must state the next-session caveat, got:\n%s", text)
}
if !strings.Contains(text, "gortex install") {
t.Errorf("switch on an uninstalled machine should nudge toward `gortex install`, got:\n%s", text)
}
active, err := os.ReadFile(filepath.Join(dir, profiles.ActiveFileName))
if err != nil {
t.Fatalf("active.md missing after switch: %v", err)
}
loc, _ := profiles.ByName("localization")
if string(active) != loc.Body() {
t.Error("active.md is not the switched profile body")
}
// list → active marker on the switched profile.
cmd, out, _ = captureCmd()
if err := runInstructionsList(cmd, nil); err != nil {
t.Fatalf("list: %v", err)
}
if !strings.Contains(out.String(), "* localization") {
t.Errorf("list must mark the active profile, got:\n%s", out.String())
}
// show → exact body on stdout.
cmd, out, _ = captureCmd()
if err := runInstructionsShow(cmd, []string{"core"}); err != nil {
t.Fatalf("show: %v", err)
}
core, _ := profiles.ByName("core")
if out.String() != core.Body() {
t.Error("show did not print the exact profile body")
}
if err := runInstructionsShow(cmd, []string{"nope"}); err == nil {
t.Error("show of an unknown profile must error")
}
// regen → keeps the active selection.
cmd, out, _ = captureCmd()
if err := runInstructionsRegen(cmd, nil); err != nil {
t.Fatalf("regen: %v", err)
}
if !strings.Contains(out.String(), "active: localization") {
t.Errorf("regen must preserve the active selection, got:\n%s", out.String())
}
}