mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
* feat(hooks): read hook code and scripts from files next to the config
Code Agent extensions and command scripts lived as strings in
config.yaml, so a longer Pi extension or shell script was edited inside
a YAML block scalar with no editor support, and nothing type-checked or
linted it. A breaking change in an Agent's extension API went unnoticed
because the generated file is not part of any tsconfig.
A binding now takes `codeFile: path` instead of `code`, and a `files`
value may be `{from: path}` instead of the contents. Paths resolve
against the folder of the config that holds the entry and are read
every time the config is read, so the regular typecheck or shellcheck
can cover the real source. The revision and drift checks use the
resolved contents, so a file edited after a preview fails the apply as
stale.
Saving keeps the references: enable/disable, `hooks edit` and the
dashboard write `codeFile` and `{from}` back instead of inlining the
contents. JSON carries the contents plus `codeFile`/`filesFrom`, and an
entry echoed back with both must match the file, so an edit is refused
rather than silently dropped. Paths must stay inside the config folder
without symlinks, so a cloned project cannot pull in files from
elsewhere. References are taken from the YAML tree before one strict
decode, which keeps unknown-field checks and anchors shared across
bindings working.
Closes #585
* fix(hooks): refuse an emptied file sent with its reference
An empty files value stood for "contents not sent", so a file cleared in
the dashboard's Git YAML editor while its filesFrom reference stayed was
treated as unsent: the file was read again from disk and the save
reported success with the user's edit discarded.
A value read from {from: path} now keeps a dedicated mark until it is
resolved, so an empty value is compared with the file like any other
contents and refused when they differ. Code keeps using empty as unsent,
since a code binding can never be empty.
* fix(hooks): render referenced files as they are on disk
RenderNative rendered the contents the caller sent. The dashboard sends
the entry it loaded, so after a referenced file changed, View
configuration still showed the old contents while describing them as
what sync would write.
Render now drops sent contents for codeFile and {from} references and
reads every reference again, like preview and save. Draft, render and
config decoding are the only paths that take an entry, and all three now
resolve references.
* fix(hooks): accept file references in JSON entries and anchored values
Two inputs that config.yaml accepts failed to load elsewhere.
A `{from}` value carrying its own YAML anchor, aliased from another
binding, lost its anchor because the value node was replaced instead of
rewritten, so the alias no longer resolved. The node is now rewritten in
place with its anchor kept.
An entry JSON file (`hooks add|edit --file x.json`) could not write a
files value as `{"from": path}`, because JSON decoded straight into
string contents. Binding now decodes JSON files values as contents or a
reference, strictly, like YAML, so both entry formats and the Web API
take the same shapes.
196 lines
9.1 KiB
Go
196 lines
9.1 KiB
Go
package hooks
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const piCodeFileConfig = "targets: {}\nhooks:\n entries:\n lint:\n bindings:\n pi:\n codeFile: hooks/lint.pi.ts\n"
|
|
|
|
func TestCodeFile_SyncsFileContentsAndSavesTheReference(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, piCodeFileConfig)
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "lint.pi.ts"), "export default function () {}\n")
|
|
sync(t, e.service)
|
|
out := filepath.Join(e.home, ".pi", "agent", "extensions", "skillshare-lint.ts")
|
|
if got := read(t, out); got != "export default function () {}\n" {
|
|
t.Fatalf("extension = %q", got)
|
|
}
|
|
|
|
source, err := LoadSource(e.config)
|
|
must(t, err)
|
|
disabled := source.Entries["lint"]
|
|
off := false
|
|
disabled.Enabled = &off
|
|
save(t, e.service, Mutation{Name: "lint", Entry: &disabled})
|
|
if got := read(t, e.config); !strings.Contains(got, "codeFile: hooks/lint.pi.ts") || strings.Contains(got, "code:") {
|
|
t.Fatalf("saving must keep codeFile and leave out the code:\n%s", got)
|
|
}
|
|
}
|
|
|
|
func TestCodeFile_ChangedFileChangesRevision(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, piCodeFileConfig)
|
|
code := filepath.Join(filepath.Dir(e.config), "hooks", "lint.pi.ts")
|
|
write(t, code, "export default function () {}\n")
|
|
before, err := e.service.PreviewMutation(Mutation{})
|
|
must(t, err)
|
|
write(t, code, "export default function (pi) {}\n")
|
|
if _, err := e.service.Mutate(Mutation{}, before.Revision, true); err != ErrStaleRevision {
|
|
t.Fatalf("err = %v, want ErrStaleRevision", err)
|
|
}
|
|
}
|
|
|
|
func TestCodeFile_RejectsInvalidReferences(t *testing.T) {
|
|
for name, binding := range map[string]string{
|
|
"both": "code: x\n codeFile: hooks/lint.pi.ts",
|
|
"escape": "codeFile: ../lint.pi.ts",
|
|
"missing": "codeFile: hooks/missing.ts",
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, strings.Replace(piCodeFileConfig, "codeFile: hooks/lint.pi.ts", binding, 1))
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "lint.pi.ts"), "export default function () {}\n")
|
|
if _, err := LoadSource(e.config); err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCodeFile_RefusesSymlinks(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, piCodeFileConfig)
|
|
secret := filepath.Join(e.home, "secret")
|
|
write(t, secret, "token\n")
|
|
dir := filepath.Join(filepath.Dir(e.config), "hooks")
|
|
must(t, os.MkdirAll(dir, 0755))
|
|
must(t, os.Symlink(secret, filepath.Join(dir, "lint.pi.ts")))
|
|
if _, err := LoadSource(e.config); err == nil || !strings.Contains(err.Error(), "symlink") {
|
|
t.Fatalf("err = %v, want a symlink refusal", err)
|
|
}
|
|
}
|
|
|
|
func TestCodeFile_EchoedEntryMustMatchTheFile(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, piCodeFileConfig)
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "lint.pi.ts"), "export default function () {}\n")
|
|
// list --json and the dashboard carry both code and codeFile.
|
|
echoed := entry(t, `{"bindings":{"pi":{"codeFile":"hooks/lint.pi.ts","code":"export default function () {}\n"}}}`)
|
|
save(t, e.service, Mutation{Name: "lint", Entry: echoed})
|
|
edited := entry(t, `{"bindings":{"pi":{"codeFile":"hooks/lint.pi.ts","code":"edited"}}}`)
|
|
if _, err := e.service.PreviewMutation(Mutation{Name: "lint", Entry: edited}); err == nil || !strings.Contains(err.Error(), "differ") {
|
|
t.Fatalf("err = %v, want code that differs from the file refused", err)
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_SyncsContentsAndSavesTheReference(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, "targets: {}\nhooks:\n entries:\n guard:\n bindings:\n cursor:\n events:\n beforeShellExecution:\n - command: ./guard.sh\n files:\n guard.sh:\n from: hooks/guard.sh\n inline.sh: \"echo inline\\n\"\n")
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "guard.sh"), "#!/bin/sh\necho guard\n")
|
|
sync(t, e.service)
|
|
if got := read(t, filepath.Join(e.home, ".cursor", "hooks", "skillshare", "guard", "guard.sh")); got != "#!/bin/sh\necho guard\n" {
|
|
t.Fatalf("script = %q", got)
|
|
}
|
|
|
|
source, err := LoadSource(e.config)
|
|
must(t, err)
|
|
disabled := source.Entries["guard"]
|
|
off := false
|
|
disabled.Enabled = &off
|
|
save(t, e.service, Mutation{Name: "guard", Entry: &disabled})
|
|
if got := read(t, e.config); !strings.Contains(got, "guard.sh:\n from: hooks/guard.sh") || strings.Contains(got, "echo guard") || !strings.Contains(got, "echo inline") {
|
|
t.Fatalf("saving must keep the reference and the inline file:\n%s", got)
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_RejectsUnknownReferenceFields(t *testing.T) {
|
|
_, err := ParseEntry([]byte("bindings:\n cursor:\n events: {beforeShellExecution: [{command: x}]}\n files:\n guard.sh: {path: hooks/guard.sh}\n"))
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_KeepsAnchorsSharedAcrossBindings(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, "targets: {}\nhooks:\n entries:\n guard:\n bindings:\n claude:\n events: &ev\n Stop:\n - hooks:\n - {type: command, command: echo done}\n codex:\n events: *ev\n files:\n guard.sh: {from: hooks/guard.sh}\n")
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "guard.sh"), "echo guard\n")
|
|
source, err := LoadSource(e.config)
|
|
must(t, err)
|
|
if b := source.Entries["guard"].Bindings["codex"]; len(b.Events) != 1 || b.Files["guard.sh"] != "echo guard\n" {
|
|
t.Fatalf("codex binding = %+v", b)
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_DropsTheReferenceOfARemovedFile(t *testing.T) {
|
|
e := newEnv(t)
|
|
removed := entry(t, `{"bindings":{"cursor":{"events":{"stop":[{"command":"echo"}]},"filesFrom":{"guard.sh":"hooks/guard.sh"}}}}`)
|
|
save(t, e.service, Mutation{Name: "guard", Entry: removed})
|
|
if got := read(t, e.config); strings.Contains(got, "guard.sh") {
|
|
t.Fatalf("a removed file must not come back:\n%s", got)
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_KeepsTheReferenceInAnAliasedBinding(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, "targets: {}\nhooks:\n entries:\n a:\n bindings:\n cursor: &b\n events: {stop: [{command: ./check.sh}]}\n files:\n check.sh: {from: hooks/check.sh}\n b:\n bindings:\n cursor: *b\n")
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "check.sh"), "echo check\n")
|
|
source, err := LoadSource(e.config)
|
|
must(t, err)
|
|
if got := source.Entries["b"].Bindings["cursor"].Files["check.sh"]; got != "echo check\n" {
|
|
t.Fatalf("aliased check.sh = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_RefusesAnEchoedEmptyFile(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "guard.sh"), "echo guard\n")
|
|
cleared := entry(t, `{"bindings":{"cursor":{"events":{"stop":[{"command":"echo"}]},"files":{"guard.sh":""},"filesFrom":{"guard.sh":"hooks/guard.sh"}}}}`)
|
|
if _, err := e.service.PreviewMutation(Mutation{Name: "guard", Entry: cleared}); err == nil || !strings.Contains(err.Error(), "differ") {
|
|
t.Fatalf("err = %v, want a cleared file refused", err)
|
|
}
|
|
}
|
|
|
|
func TestRenderNative_ReadsReferencesInsteadOfSentContents(t *testing.T) {
|
|
e := newEnv(t)
|
|
dir := filepath.Dir(e.config)
|
|
write(t, filepath.Join(dir, "hooks", "lint.pi.ts"), "export default function (pi) {}\n")
|
|
write(t, filepath.Join(dir, "hooks", "guard.sh"), "echo new\n")
|
|
// The dashboard sends contents cached before the files changed.
|
|
stale := entry(t, `{"bindings":{"pi":{"codeFile":"hooks/lint.pi.ts","code":"old"},"cursor":{"events":{"stop":[{"command":"echo"}]},"files":{"guard.sh":"echo old\n"},"filesFrom":{"guard.sh":"hooks/guard.sh"}}}}`)
|
|
files, err := e.service.RenderNative(Mutation{Name: "lint", Entry: stale})
|
|
must(t, err)
|
|
var got []string
|
|
for _, f := range files {
|
|
got = append(got, f.Content)
|
|
}
|
|
if all := strings.Join(got, "\n"); !strings.Contains(all, "export default function (pi) {}") || !strings.Contains(all, "echo new") || strings.Contains(all, "old") {
|
|
t.Fatalf("rendered %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_KeepsAnAnchoredReference(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, e.config, "targets: {}\nhooks:\n entries:\n a:\n bindings:\n cursor:\n events: {stop: [{command: ./check.sh}]}\n files:\n check.sh: &check {from: hooks/check.sh}\n claude:\n events: {Stop: [{hooks: [{type: command, command: ./check.sh}]}]}\n files:\n check.sh: *check\n")
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "check.sh"), "echo check\n")
|
|
source, err := LoadSource(e.config)
|
|
must(t, err)
|
|
if got := source.Entries["a"].Bindings["claude"].Files["check.sh"]; got != "echo check\n" {
|
|
t.Fatalf("aliased check.sh = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFilesFrom_AcceptsReferencesInJSONEntries(t *testing.T) {
|
|
e := newEnv(t)
|
|
write(t, filepath.Join(filepath.Dir(e.config), "hooks", "guard.sh"), "echo guard\n")
|
|
save(t, e.service, Mutation{Name: "guard", Entry: entry(t, `{"bindings":{"cursor":{"events":{"stop":[{"command":"echo"}]},"files":{"guard.sh":{"from":"hooks/guard.sh"}}}}}`)})
|
|
if got := read(t, e.config); !strings.Contains(got, "from: hooks/guard.sh") {
|
|
t.Fatalf("config:\n%s", got)
|
|
}
|
|
if _, err := ParseEntry([]byte(`{"bindings":{"cursor":{"files":{"guard.sh":{"path":"x"}}}}}`)); err == nil {
|
|
t.Fatal("an unknown reference field must be refused")
|
|
}
|
|
}
|