Files
skillshare/internal/server/handler_instructions_regression_test.go
Willie 7baf716c63 feat(instructions): translate dashboard warnings and errors
Mode changes, attach and the other AGENTS.md actions returned English
sentences, so a zh-TW dashboard showed "backed up ... before replacing
it" and "... is a directory" untranslated. Warnings are now
{code, params, message} and every instructions error carries a code,
so the dashboard shows them from i18n and falls back to the English
message for unknown codes. CLI output is unchanged.

Also from the Windows re-test:
- replacing a user's junction says so and where it pointed;
- status, doctor and sync agree on identical unowned copies (kept, not
  linked), and an identical copy no longer suggests --force;
- the targets list shows a Developer Mode tooltip when file links are
  unavailable;
- file names are taken from Windows paths too.

Refs #299
2026-09-28 16:12:59 +08:00

195 lines
8.5 KiB
Go

package server
import (
"net/http"
"os"
"path/filepath"
"skillshare/internal/config"
syncpkg "skillshare/internal/sync"
"strings"
"testing"
)
func TestInstructionsSetupRejectsExistingDirectory(t *testing.T) {
s, home := newInstructionsServer(t, "codex")
os.MkdirAll(filepath.Join(home, ".codex"), 0755)
for _, path := range []string{"~/.codex", "~/.codex/"} {
rr := instructionsRequest(t, s, http.MethodPut, "/api/targets/codex/instructions/setup", `{"path":"`+path+`"}`)
if rr.Code != http.StatusBadRequest || !strings.Contains(rr.Body.String(), "must name a file, not a directory") {
t.Fatalf("%s: %d %s", path, rr.Code, rr.Body.String())
}
}
}
func TestInstructionsModeReturnsWarnings(t *testing.T) {
s, home := newInstructionsServer(t, "codex")
attachShared(t, s, "codex")
instructionsRequest(t, s, http.MethodPut, "/api/instructions/team/targets/codex/mode", `{"mode":"copy"}`)
os.WriteFile(filepath.Join(home, ".codex", "AGENTS.md"), []byte("edited"), 0644)
rr := instructionsRequest(t, s, http.MethodPut, "/api/instructions/team/targets/codex/mode", `{"mode":"symlink"}`)
if rr.Code != 200 || !strings.Contains(rr.Body.String(), `"code":"backed_up"`) {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
warnings := decodeBody[struct {
Warnings []syncpkg.FileWarning `json:"warnings"`
}](t, rr).Warnings
path := filepath.Join(home, ".codex", "AGENTS.md")
if len(warnings) != 1 || warnings[0].Params["path"] != path || warnings[0].Message != "backed up "+path+" before replacing it" {
t.Fatalf("warnings: %+v", warnings)
}
}
func TestInstructionsAssignRejectsMixedOwnership(t *testing.T) {
for _, mode := range []string{"symlink", "copy"} {
t.Run(mode, func(t *testing.T) {
s, _ := newInstructionsServer(t, "claude")
attachShared(t, s, "claude")
instructionsRequest(t, s, http.MethodPut, "/api/instructions/team/targets/claude/mode", `{"mode":"`+mode+`"}`)
instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"other","content":"other"}`)
rr := instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["claude"],"extras":["team","other"]}`)
if rr.Code != 409 || !strings.Contains(rr.Body.String(), "team") || !strings.Contains(rr.Body.String(), `"error_code":"instructions_target_held"`) {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
extra, _ := s.sharedExtra("team")
if got := readFile(t, filepath.Join(s.extrasSourceDir(extra), extra.File)); got != "team\n" {
t.Fatalf("source corrupted: %q", got)
}
})
}
}
func TestInstructionsAssignRejectsUntrackedSharedLink(t *testing.T) {
s, home := newInstructionsServer(t, "claude")
instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"personal","content":"personal"}`)
instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"other","content":"other"}`)
extra, _ := s.sharedExtra("personal")
os.MkdirAll(filepath.Join(home, ".claude"), 0755)
os.Symlink(filepath.Join(s.extrasSourceDir(extra), extra.File), filepath.Join(home, ".claude", "CLAUDE.md"))
rr := instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["claude"],"extras":["other"]}`)
if rr.Code != 409 || !strings.Contains(rr.Body.String(), "personal") {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
list := instructionsRequest(t, s, http.MethodGet, "/api/instructions", "")
if !strings.Contains(list.Body.String(), `"linked_shared":"personal"`) {
t.Fatal(list.Body.String())
}
}
func TestInstructionsModeConflictNamesOtherSharedFile(t *testing.T) {
s, _ := newInstructionsServer(t, "claude")
attachShared(t, s, "claude")
instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"other","content":"other"}`)
rr := instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["claude"],"extras":["team","other"]}`)
if rr.Code != 200 {
t.Fatal(rr.Body.String())
}
for _, name := range []string{"team", "other"} {
rr = instructionsRequest(t, s, http.MethodPut, "/api/instructions/"+name+"/targets/claude/mode", `{"mode":"copy"}`)
body := decodeBody[struct {
Code string `json:"error_code"`
Params map[string]string `json:"error_params"`
}](t, rr)
other := "team"
if name == "team" {
other = "other"
}
if rr.Code != 409 || body.Code != "instructions_target_held" || body.Params["name"] != other || body.Params["target"] != "claude" {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
}
}
func TestInstructionsAssignIncludesWarnings(t *testing.T) {
s, home := newInstructionsServer(t, "codex")
writeHome(t, home, ".codex/AGENTS.md", "mine")
instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"team","content":"team"}`)
rr := instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["codex"],"extras":["team"]}`)
if rr.Code != 200 || !strings.Contains(rr.Body.String(), `"code":"backed_up"`) {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
warnings := decodeBody[struct {
Warnings []syncpkg.FileWarning `json:"warnings"`
}](t, rr).Warnings
path := filepath.Join(home, ".codex", "AGENTS.md")
if len(warnings) != 1 || warnings[0].Params["path"] != path || warnings[0].Message != "backed up "+path+" before replacing it" {
t.Fatalf("warnings: %+v", warnings)
}
rr = instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["codex"],"extras":["team"]}`)
if rr.Code != 200 || !strings.Contains(rr.Body.String(), `"warnings":[]`) {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
rr = instructionsRequest(t, s, http.MethodPut, "/api/instructions/team/targets/codex/mode", `{"mode":"symlink"}`)
if rr.Code != 200 || !strings.Contains(rr.Body.String(), `"warnings":[]`) {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
}
func TestInstructionsDeleteKeepsConfigOnRestoreFailure(t *testing.T) {
s, home := newInstructionsServer(t, "codex")
writeHome(t, home, ".codex/AGENTS.md", "original")
attachShared(t, s, "codex")
entries, err := os.ReadDir(filepath.Join(config.StateDir(), "extras", "backups"))
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
path := filepath.Join(config.StateDir(), "extras", "backups", e.Name(), "restore")
if _, err := os.Stat(path); err == nil {
if err := os.Remove(path); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(path, 0700); err != nil {
t.Fatal(err)
}
}
}
before := readFile(t, config.ConfigPath())
rr := instructionsRequest(t, s, http.MethodDelete, "/api/extras/team", "")
if rr.Code != http.StatusInternalServerError {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
if got := readFile(t, config.ConfigPath()); got != before {
t.Fatal("failed restore removed config")
}
}
func TestInstructionsCodedErrors(t *testing.T) {
s, home := newInstructionsServer(t, "codex")
cases := []struct{ method, url, body, code, key, value string }{
{"PUT", "/api/targets/codex/instructions/setup", `{"path":"~/.codex/"}`, "instructions_path_directory", "path", "~/.codex/"},
{"PUT", "/api/targets/codex/instructions/setup", `{"path":"relative.md"}`, "instructions_path_absolute", "path", "relative.md"},
{"PUT", "/api/instructions/team/targets/missing/mode", `{"mode":"copy"}`, "instructions_target_not_found", "target", "missing"},
{"PUT", "/api/instructions/team/targets/codex/mode", `{"mode":"invalid"}`, "instructions_invalid_mode", "mode", "invalid"},
{"POST", "/api/instructions/team/restore", `{}`, "instructions_target_required", "", ""},
{"POST", "/api/targets/codex/instructions/convert", `{"method":"invalid"}`, "instructions_method_unavailable", "method", "invalid"},
}
for _, c := range cases {
rr := instructionsRequest(t, s, c.method, c.url, c.body)
body := decodeBody[struct {
Code string `json:"error_code"`
Params map[string]string `json:"error_params"`
}](t, rr)
if rr.Code < 400 || body.Code != c.code || c.key != "" && body.Params[c.key] != c.value {
t.Fatalf("%s: %d %s", c.url, rr.Code, rr.Body.String())
}
}
attachShared(t, s, "codex")
path := filepath.Join(home, ".codex", "AGENTS.md")
if err := os.Remove(path); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(path, 0755); err != nil {
t.Fatal(err)
}
rr := instructionsRequest(t, s, "PUT", "/api/instructions/team/targets/codex/mode", `{"mode":"copy"}`)
body := decodeBody[struct {
Code string `json:"error_code"`
Params map[string]string `json:"error_params"`
}](t, rr)
if rr.Code != 409 || body.Code != "instructions_target_directory" || body.Params["path"] != path {
t.Fatalf("%d %s", rr.Code, rr.Body.String())
}
}