* Add German translation and harden paths against unset env vars Add a German locale to the GUI, tray menu, usage alerts and website home page, and refuse to start when the home folder is unset or relative so nothing is written under the working folder; the pi plugin no longer adopts the user's PI_CODING_AGENT_DIR, and an inherited APPIMAGE no longer selects portable mode. Scripts refuse to run from the wrong folder or empty a shared out folder, and a new internal/testenv sandboxes every test package from the user's real files, which also fixes the Claude picker and Termux installer tests on Windows. * Makefile: know magpie's folder by its go.mod, so paths with spaces work The check compared $(CURDIR) with $(dir $(abspath …)), which make splits on spaces, so a checkout under 'My Projects' or 'John Doe' refused every target. It now reads go.mod relative to the working folder and wants magpie's module there: make -C and a symlinked Makefile work, make -f from another folder still stops before any recipe runs. * i18n: German for the strings main added since, with a parity test 75 strings main added (group order, patterns, slow-start wait, usage caps, RTK savings …) get their German, and two that main dropped go. gui-de.test.cjs checks, as gui-ja does, that de has every zh string with its placeholders and tags, and that the window reads in German when picked or on a German system. * appdir: ignore relative folder variables when reading, keep them for children CheckEnv no longer unsets anything. magpie reads the XDG folders, APPDATA/LOCALAPPDATA and every agent's folder variable through appdir.Getenv/LookupEnv, which pass over a relative value; the environment stays as it is, so the agents magpie starts get the variable as the user set it. That covers what proc.UserPath lends from the login shell too, which is set after main's check and reached the desktop app unchecked. On Windows a path rooted on the current drive (\Users\x, Git Bash's /c/x) counts as usable, and magpie version and help answer without a home, before run makes any folder. * Sandbox the tests main added since internal/wslrun gets testenv's TestMain, and four tests main added that set HOME alone set their home with testenv.SetHome, so on Windows they don't read the runner's profile through USERPROFILE. * agentenv: PI_CONFIG_DIR and OPENCODE_DB take relative names on purpose omp puts $PI_CONFIG_DIR under the home and OpenCode a relative $OPENCODE_DB in its data folder, so neither depends on the working folder: appdir no longer passes them over. * i18n: German for the Library's multi-select and System One's decision API The 19 strings main added since (#791's skill multi-select, the decision API snippets) get their German; the one main replaced (four APIs → five APIs) goes. * appdir: tidy the import into magpie's group; say what Go makes of /c/x The appdir import joins the group of magpie's own imports where a file has one. rooted's comment now says that Go takes a Git Bash path like /c/x as \c\x on the current drive, not C:\x.
113 lines
4.4 KiB
Go
113 lines
4.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// claudeWorkDirs runs two Claude subscription requests through a script
|
|
// standing in for Claude Code, with a temp folder of the test's own and a
|
|
// home kept in git, and gives the folders the two runs worked in and the
|
|
// folder they should share.
|
|
func claudeWorkDirs(t *testing.T, prepare func(work string)) (dirs []string, work string) {
|
|
t.Helper()
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("a shell script stands in for Claude Code")
|
|
}
|
|
home := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(home, ".git"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testenv.SetHome(t, home)
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("TMPDIR", t.TempDir())
|
|
work = filepath.Join(os.TempDir(), claudeWorkName())
|
|
if prepare != nil {
|
|
prepare(work)
|
|
}
|
|
dir := t.TempDir()
|
|
script := `#!/bin/sh
|
|
pwd -P >> ` + dir + `/dirs
|
|
while read -r line; do
|
|
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}'
|
|
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
|
|
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
|
|
done
|
|
`
|
|
os.WriteFile(filepath.Join(dir, "claude"), []byte(script), 0o755)
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
|
|
s := New()
|
|
p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}}
|
|
for _, said := range []string{"one", "two"} {
|
|
body := `{"model":"claude-sonnet-5","max_tokens":100,"system":"rules","messages":[{"role":"user","content":"` + said + `"}]}`
|
|
rec := httptest.NewRecorder()
|
|
var u Usage
|
|
if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u); code != 200 {
|
|
t.Fatalf("%d %s", code, msg)
|
|
}
|
|
}
|
|
b, _ := os.ReadFile(filepath.Join(dir, "dirs"))
|
|
return strings.Fields(string(b)), evalSymlinks(work)
|
|
}
|
|
|
|
// Every run works in the same folder: Claude Code puts its working
|
|
// directory in the system prompt, ahead of the conversation, so a folder of
|
|
// each run's own left nothing past Claude Code's own part of the prompt to
|
|
// be read from the cache. The folder is the user's own, made for them
|
|
// alone, in the temp folder: not under a home kept in git, whose status
|
|
// Claude Code would put in the prompt.
|
|
func TestClaudeRunsShareAWorkDir(t *testing.T) {
|
|
dirs, work := claudeWorkDirs(t, nil)
|
|
if len(dirs) != 2 || dirs[0] != work || dirs[1] != work {
|
|
t.Fatalf("working directories: %q, want %s", dirs, work)
|
|
}
|
|
if home := evalSymlinks(os.Getenv("HOME")); strings.HasPrefix(work, home+string(filepath.Separator)) || !strings.HasPrefix(work, evalSymlinks(os.TempDir())+string(filepath.Separator)) {
|
|
t.Fatalf("work folder %s: in the home %s, or not in the temp folder", work, home)
|
|
}
|
|
fi, err := os.Stat(work)
|
|
if err != nil || fi.Mode().Perm() != 0o700 {
|
|
t.Fatalf("work folder: %v %v", fi.Mode(), err)
|
|
}
|
|
}
|
|
|
|
// A work folder others can write to is not worked in, as anything put
|
|
// there (a CLAUDE.md) would be read by every run: each run works in a
|
|
// folder of its own instead.
|
|
func TestClaudeWorkDirOthersCanWriteIsAvoided(t *testing.T) {
|
|
dirs, work := claudeWorkDirs(t, func(work string) {
|
|
if err := os.MkdirAll(work, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Chmod(work, 0o777)
|
|
})
|
|
if len(dirs) != 2 || dirs[0] == work || dirs[1] == work || dirs[0] == dirs[1] {
|
|
t.Fatalf("working directories: %q, shared %s", dirs, work)
|
|
}
|
|
}
|
|
|
|
// A symlink where the work folder goes, to a folder anyone could have
|
|
// filled, is not followed: each run works in a folder of its own.
|
|
func TestClaudeWorkDirSymlinkIsAvoided(t *testing.T) {
|
|
var target string
|
|
dirs, work := claudeWorkDirs(t, func(work string) {
|
|
target = t.TempDir()
|
|
if err := os.Symlink(target, work); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
target = evalSymlinks(target)
|
|
if len(dirs) != 2 || dirs[0] == target || dirs[1] == target || dirs[0] == dirs[1] {
|
|
t.Fatalf("working directories: %q, linked %s (%s)", dirs, target, work)
|
|
}
|
|
}
|