diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 80590719b..64c219be9 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -137,6 +137,11 @@ jobs: ./internal/validate \ ./internal/version + # Run this Windows-only regression; the rest of oplog still has failures tracked in #344. + - name: Windows legacy config log regression + shell: bash + run: go test -v -count=1 ./internal/oplog -run "^TestLogDir_WindowsLegacySelectedGlobal$" + docker-sandbox-offline: if: *not-release-duplicate runs-on: ubuntu-latest diff --git a/cmd/skillshare/log.go b/cmd/skillshare/log.go index 393a45e10..ed3bf340a 100644 --- a/cmd/skillshare/log.go +++ b/cmd/skillshare/log.go @@ -11,9 +11,7 @@ import ( "skillshare/internal/config" "skillshare/internal/oplog" - "skillshare/internal/projectdir" "skillshare/internal/ui" - "skillshare/internal/utils" ) func cmdLog(args []string) error { @@ -93,7 +91,7 @@ func runLog(args []string, configPath string) error { } modeLabel := "global" - if isProjectLogConfig(configPath) { + if oplog.IsProjectConfig(configPath) { modeLabel = "project" } @@ -158,7 +156,7 @@ func printLogSection(configPath, filename, label string, limit int, f oplog.Filt logPath = abs } mode := "global" - if isProjectLogConfig(configPath) { + if oplog.IsProjectConfig(configPath) { mode = "project" } @@ -263,16 +261,6 @@ func runLogStats(configPath string, auditOnly bool, f oplog.Filter) error { return nil } -func isProjectLogConfig(configPath string) bool { - configDir := filepath.Dir(configPath) - if !projectdir.IsName(filepath.Base(configDir)) { - return false - } - // The visible project directory shares its name with the global config - // directory, so exclude the global location by full path. - return !utils.PathsEqual(configDir, config.BaseDir()) -} - // runLogTUIDispatch launches the interactive TUI with async log loading. func runLogTUIDispatch(configPath string, auditOnly bool, limit int, filter oplog.Filter, modeLabel string) error { if auditOnly { diff --git a/internal/oplog/oplog.go b/internal/oplog/oplog.go index ae2792722..6ba408e46 100644 --- a/internal/oplog/oplog.go +++ b/internal/oplog/oplog.go @@ -44,18 +44,26 @@ func LogDir(configPath string) string { // // The visible project directory shares its name with the global config // directory (/skillshare), so a name match alone is ambiguous and -// the global location is excluded by full path. +// the canonical and selected global locations are excluded by full path. func projectConfigDir(configPath string) (string, bool) { configDir := filepath.Dir(configPath) if !projectdir.IsName(filepath.Base(configDir)) { return "", false } - if utils.PathsEqual(configDir, config.BaseDir()) { + if utils.PathsEqual(configDir, config.BaseDir()) || + utils.PathsEqual(configPath, config.ReadOnlyConfigPath()) { return "", false } return configDir, true } +// IsProjectConfig reports whether a config routes to project-local logs. +// Both readers and writers must use the same global/selected-config exclusions. +func IsProjectConfig(configPath string) bool { + _, ok := projectConfigDir(configPath) + return ok +} + // Write appends a single JSONL entry to the named log file. func Write(configPath, filename string, e Entry) error { dir := LogDir(configPath) diff --git a/internal/oplog/oplog_test.go b/internal/oplog/oplog_test.go index d24f280f5..a580f5a6c 100644 --- a/internal/oplog/oplog_test.go +++ b/internal/oplog/oplog_test.go @@ -7,6 +7,8 @@ import ( "strings" "testing" "time" + + "skillshare/internal/config" ) func tempConfigPath(t *testing.T) string { @@ -593,3 +595,55 @@ func TestWriteWithLimit_HysteresisAvoidsTruncation(t *testing.T) { t.Errorf("got %d entries, want 11 (hysteresis should prevent truncation)", len(entries)) } } + +func TestLogDir_SelectedGlobalConfigRoundTrip(t *testing.T) { + for _, dir := range []string{"shared/skillshare", "shared/.skillshare", ".config/skillshare"} { + t.Run(dir, func(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg")) + t.Setenv("XDG_STATE_HOME", filepath.Join(home, "state")) + selected := filepath.Join(home, filepath.FromSlash(dir), "config.yaml") + t.Setenv("SKILLSHARE_CONFIG", selected) + canonical := filepath.Join(config.BaseDir(), "config.yaml") + want := filepath.Join(config.StateDir(), "logs") + if got := LogDir(selected); got != want { + t.Errorf("selected global logs=%q want %q", got, want) + } + if err := Write(canonical, OpsFile, Entry{Command: "project", Status: "ok"}); err != nil { + t.Fatal(err) + } + entries, err := Read(selected, OpsFile, 0) + if err != nil || len(entries) != 1 { + t.Errorf("global reader lost canonical write: %v %v", entries, err) + } + if err := Write(selected, AuditFile, Entry{Command: "audit", Status: "ok"}); err != nil { + t.Fatal(err) + } + for _, name := range []string{"logs", ".gitignore"} { + if _, err := os.Stat(filepath.Join(filepath.Dir(selected), name)); !os.IsNotExist(err) { + t.Fatalf("global config treated as a project: %s exists (%v)", name, err) + } + } + project := filepath.Join(home, "app", "skillshare", "config.yaml") + if err := Write(project, OpsFile, Entry{Command: "sync", Status: "ok"}); err != nil { + t.Fatal(err) + } + if got := LogDir(project); got != filepath.Join(filepath.Dir(project), "logs") { + t.Fatalf("real project moved: %s", got) + } + if err := Clear(selected, OpsFile); err != nil { + t.Fatal(err) + } + entries, err = Read(canonical, OpsFile, 0) + if err != nil || len(entries) != 0 { + t.Errorf("global clear missed canonical write: %v %v", entries, err) + } + entries, err = Read(project, OpsFile, 0) + if err != nil || len(entries) != 1 { + t.Errorf("global clear changed real project: %v %v", entries, err) + } + }) + } +} diff --git a/internal/oplog/oplog_windows_test.go b/internal/oplog/oplog_windows_test.go new file mode 100644 index 000000000..0f74400d5 --- /dev/null +++ b/internal/oplog/oplog_windows_test.go @@ -0,0 +1,34 @@ +package oplog + +import ( + "os" + "path/filepath" + "testing" + + "skillshare/internal/config" +) + +func TestLogDir_WindowsLegacySelectedGlobal(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg")) + t.Setenv("XDG_STATE_HOME", filepath.Join(home, "state")) + t.Setenv("SKILLSHARE_CONFIG", "") + legacy := filepath.Join(home, ".config", "skillshare", "config.yaml") + if err := os.MkdirAll(filepath.Dir(legacy), 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(legacy, []byte("source: skills\n"), 0644); err != nil { + t.Fatal(err) + } + if got := config.ReadOnlyConfigPath(); got != legacy { + t.Fatalf("legacy selection=%q want %q", got, legacy) + } + if got, want := LogDir(legacy), filepath.Join(config.StateDir(), "logs"); got != want { + t.Fatalf("legacy-selected global logs=%q want %q", got, want) + } + if IsProjectConfig(legacy) { + t.Fatal("legacy-selected global classified as project") + } +} diff --git a/internal/server/handler_log_test.go b/internal/server/handler_log_test.go index 97cd4d799..1ba78ac62 100644 --- a/internal/server/handler_log_test.go +++ b/internal/server/handler_log_test.go @@ -6,6 +6,9 @@ import ( "net/http/httptest" "strings" "testing" + + "skillshare/internal/config" + "skillshare/internal/oplog" ) func TestHandleListLog_Empty(t *testing.T) { @@ -82,3 +85,34 @@ func TestHandleClearLog(t *testing.T) { t.Error("expected success true") } } + +func TestHandleLog_ListStatsAndClear(t *testing.T) { + s, _ := newTestServer(t) + cfgPath := config.ConfigPath() + if err := oplog.Write(cfgPath, oplog.OpsFile, oplog.Entry{Command: "sync", Status: "ok"}); err != nil { + t.Fatal(err) + } + for _, url := range []string{"/api/log", "/api/log/stats"} { + rr := projectRequest(t, s, http.MethodGet, url, "") + var payload struct { + Total int `json:"total"` + } + if rr.Code != http.StatusOK { + t.Fatalf("%s: %d %s", url, rr.Code, rr.Body) + } + if err := json.Unmarshal(rr.Body.Bytes(), &payload); err != nil { + t.Fatal(err) + } + if payload.Total != 1 { + t.Errorf("%s missed global log entry: %s", url, rr.Body) + } + } + rr := projectRequest(t, s, http.MethodDelete, "/api/log", "") + if rr.Code != http.StatusOK { + t.Fatalf("clear: %d %s", rr.Code, rr.Body) + } + entries, err := oplog.Read(cfgPath, oplog.OpsFile, 0) + if err != nil || len(entries) != 0 { + t.Fatalf("clear missed canonical log: %v %v", entries, err) + } +} diff --git a/tests/integration/log_config_override_test.go b/tests/integration/log_config_override_test.go new file mode 100644 index 000000000..74f091aa9 --- /dev/null +++ b/tests/integration/log_config_override_test.go @@ -0,0 +1,28 @@ +//go:build !online + +package integration + +import ( + "path/filepath" + "testing" + + "skillshare/internal/testutil" +) + +// Global commands share the selected config path and label its log as global. +func TestLog_GlobalConfigOverrideUsesStateDir(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.CreateSkill("test-skill", map[string]string{"SKILL.md": "# Test\n\nTest."}) + targetPath := sb.CreateTarget("claude") + sb.ConfigPath = filepath.Join(sb.Home, "shared", "skillshare", "config.yaml") + sb.WriteConfig("source: " + sb.SourcePath + "\nmode: merge\ntargets:\n claude:\n path: " + targetPath + "\n") + + sb.RunCLI("sync", "-g").AssertSuccess(t) + + list := sb.RunCLI("log", "-g", "--no-tui") + list.AssertSuccess(t) + list.AssertOutputContains(t, "sync") + list.AssertOutputNotContains(t, "ยท project") +}