refactor: testing from the beginning. a journey in 150k lines of tests to none

This commit is contained in:
sam committed 2026-10-03 21:16:25 +10:00
1 parent ba37e37e53
commit ff424b70ac
651 files changed
-146443

No files matched your search

-170
View File
@@ -1,170 +0,0 @@
package skill
import (
"os"
"path/filepath"
"strings"
"testing"
)
// ── progressive disclosure: catalog tier ─────────────────────────────
func TestCatalogReturnsNameAndDescriptionOnly(t *testing.T) {
// Catalog tier (~100 tokens per skill): name + description loaded at
// daemon startup. Full body is Tier 2, loaded on skill activation.
// Catalog() must return only the catalog entries without bodies.
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "archie-wf-tdd")
if err := os.MkdirAll(skillsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
name: archie-wf-tdd
description: TDD bugfix workflow -- reproduce, prove, fix.
version: 1.0.0
metadata:
archie:
tools: [go]
engine: any
---
This is the full SKILL.md body with detailed stage instructions.
It should NOT appear in the catalog entry.
`), 0o644); err != nil {
t.Fatal(err)
}
entries, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("got %d catalog entries, want 1", len(entries))
}
e := entries[0]
if e.Name != "archie-wf-tdd" {
t.Errorf("Name = %q, want archie-wf-tdd", e.Name)
}
if e.Description == "" {
t.Error("Description is empty -- catalog must include the description")
}
if strings.Contains(e.Description, "full SKILL.md body") {
t.Error("Catalog entry contains the body text. " +
"Catalog must only contain name + description (~100 tokens). " +
"The full body is Tier 2, loaded on skill activation.")
}
// The full body must still be available via LoadBody.
body := LoadBody(dir, "archie-wf-tdd")
if !strings.Contains(body, "full SKILL.md body") {
t.Error("LoadBody did not return the full body. " +
"Catalog and LoadBody must be separate tiers: " +
"Catalog = name+description (startup), LoadBody = full SKILL.md (activation).")
}
}
func TestCatalogMissingDirReturnsEmpty(t *testing.T) {
entries, err := Catalog(t.TempDir())
if err != nil {
t.Fatal(err)
}
if entries != nil {
t.Errorf("expected nil for missing dir, got %v", entries)
}
}
func TestCatalogRootsUsesPrecedenceAndTracksRoot(t *testing.T) {
project := t.TempDir()
global := t.TempDir()
writeSkill := func(root, name, description string) {
dir := filepath.Join(root, ".agents", "skills", name)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
content := "---\nname: " + name + "\ndescription: " + description + "\n---\nBody.\n"
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
writeSkill(project, "shared-name", "project copy")
writeSkill(project, "project-only", "project skill")
writeSkill(global, "shared-name", "global copy")
writeSkill(global, "global-only", "global skill")
entries, err := CatalogRoots(project, global)
if err != nil {
t.Fatal(err)
}
if len(entries) != 3 {
t.Fatalf("got %d catalog entries, want 3", len(entries))
}
for _, entry := range entries {
if entry.Name == "shared-name" {
if entry.Description != "project copy" || entry.Root != project {
t.Fatalf("shared-name = %#v, want project entry", entry)
}
}
}
}
// ── archie-wf-* naming convention ────────────────────────────────────
func TestSkillsUseArchiewfNaming(t *testing.T) {
// PRD section 5: skills follow archie-wf-* naming convention.
// The directory name must match the workflow: tdd→archie-wf-tdd,
// implement→archie-wf-implement, feasibility→archie-wf-feasibility.
//
// Currently skill directories are named tdd-bugfix, ecosystem-node, etc.
dir := t.TempDir()
skills := map[string]string{
"archie-wf-tdd": "TDD bugfix workflow",
"archie-wf-implement": "Implement workflow",
"archie-wf-feasibility": "Feasibility workflow",
}
for name, desc := range skills {
skillDir := filepath.Join(dir, ".agents", "skills", name)
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
content := `---
name: ` + name + `
description: ` + desc + `
version: 1.0.0
---
Body.
`
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
entries, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 3 {
t.Fatalf("got %d catalog entries, want 3", len(entries))
}
for _, e := range entries {
if !strings.HasPrefix(e.Name, "archie-wf-") {
t.Errorf("skill %q does not follow archie-wf-* naming convention. "+
"Per PRD section 5, all workflow skills must use the archie-wf-* prefix.", e.Name)
}
}
}
// ── CatalogEntry type ────────────────────────────────────────────────
func TestCatalogEntryHasRequiredFields(t *testing.T) {
// CatalogEntry must have Name, Description, and a way to load the
// full body on activation.
e := CatalogEntry{Name: "archie-wf-tdd", Description: "TDD workflow"}
if e.Name == "" || e.Description == "" {
t.Error("CatalogEntry must have non-empty Name and Description")
}
}
-40
View File
@@ -1,40 +0,0 @@
package skill
import "testing"
// ── regression: Gap 8 -- frontmatter missing plugins key ─────────────
func TestFrontmatterMetadataArchieHasPluginsField(t *testing.T) {
// Gap 8: Frontmatter.Metadata.Archie has Tools and Engine only.
// PRD section 5 shows a plugins array in metadata.archie:
// plugins: [plugins/custom-gate.go, plugins/gosec-check.go]
fm, _, err := Parse([]byte(`---
name: test-skill
description: desc
version: 1.0.0
metadata:
archie:
tools: [go]
engine: any
plugins:
- plugins/custom-gate.go
- plugins/gosec-check.go
---
Body.
`))
if err != nil {
t.Fatal(err)
}
if fm.Metadata.Archie == nil {
t.Fatal("Metadata.Archie is nil")
}
if len(fm.Metadata.Archie.Plugins) != 2 {
t.Error("Gap 8: Frontmatter.Metadata.Archie has no Plugins field, or " +
"it didn't parse the plugins list. Add a Plugins []string field to " +
"the Metadata.Archie struct per PRD section 5.")
}
if fm.Metadata.Archie.Plugins[0] != "plugins/custom-gate.go" {
t.Errorf("Plugins[0] = %q, want plugins/custom-gate.go", fm.Metadata.Archie.Plugins[0])
}
}
-153
View File
@@ -1,153 +0,0 @@
package skill
import (
"os"
"path/filepath"
"testing"
)
func TestPluginRun(t *testing.T) {
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "runner")
pluginsDir := filepath.Join(skillsDir, "plugins")
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
name: runner
description: desc
version: 1.0.0
---
Body.
`), 0o644); err != nil {
t.Fatal(err)
}
src := `package main
func Run(input string) string {
return "result: " + input
}
`
if err := os.WriteFile(filepath.Join(pluginsDir, "echo.go"), []byte(src), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := LoadPlugins(dir, "runner", nil)
if err != nil {
t.Fatal(err)
}
if len(plugins) != 1 {
t.Fatalf("got %d plugins, want 1", len(plugins))
}
// Run the plugin via Yaegi and verify the output.
out, err := plugins[0].Run("hello")
if err != nil {
t.Fatal(err)
}
if out != "result: hello" {
t.Errorf("Run() = %q, want %q", out, "result: hello")
}
}
func TestLoadPluginsWithAllowlist(t *testing.T) {
// When allowed is non-empty, only those plugins are loaded in declared order.
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "ordered")
pluginsDir := filepath.Join(skillsDir, "plugins")
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
t.Fatal(err)
}
// Write three plugin files.
for _, name := range []string{"a.go", "b.go", "c.go"} {
src := `package main
func Run(input string) string {
return "` + name + `"
}
`
if err := os.WriteFile(filepath.Join(pluginsDir, name), []byte(src), 0o644); err != nil {
t.Fatal(err)
}
}
// Allowlist only b.go and a.go (with plugins/ prefix), in that order.
plugins, err := LoadPlugins(dir, "ordered", []string{"plugins/b.go", "plugins/a.go"})
if err != nil {
t.Fatal(err)
}
if len(plugins) != 2 {
t.Fatalf("got %d plugins, want 2", len(plugins))
}
if plugins[0].Name != "b" {
t.Errorf("plugin[0].Name = %q, want b", plugins[0].Name)
}
if plugins[1].Name != "a" {
t.Errorf("plugin[1].Name = %q, want a", plugins[1].Name)
}
}
func TestLoadPluginsWithAllowlistMissingFile(t *testing.T) {
// When allowed references a file not on disk, return an error.
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "broken")
pluginsDir := filepath.Join(skillsDir, "plugins")
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(pluginsDir, "a.go"), []byte(`package main
func Run(input string) string { return "ok" }
`), 0o644); err != nil {
t.Fatal(err)
}
_, err := LoadPlugins(dir, "broken", []string{"a.go", "nonexistent.go"})
if err == nil {
t.Fatal("expected error for missing plugin file, got nil")
}
}
func TestLoadPluginsEmptyAllowlist(t *testing.T) {
// Nil or empty allowlist falls back to glob-all.
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "glob")
pluginsDir := filepath.Join(skillsDir, "plugins")
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
t.Fatal(err)
}
for _, name := range []string{"b.go", "a.go"} {
if err := os.WriteFile(filepath.Join(pluginsDir, name), []byte(`package main
func Run(input string) string { return "x" }
`), 0o644); err != nil {
t.Fatal(err)
}
}
// nil allowed → glob all, sorted alphabetically.
plugins, err := LoadPlugins(dir, "glob", nil)
if err != nil {
t.Fatal(err)
}
if len(plugins) != 2 {
t.Fatalf("got %d plugins, want 2", len(plugins))
}
if plugins[0].Name != "a" {
t.Errorf("plugin[0].Name = %q, want a (alphabetical)", plugins[0].Name)
}
if plugins[1].Name != "b" {
t.Errorf("plugin[1].Name = %q, want b (alphabetical)", plugins[1].Name)
}
// Empty allowed → same as nil.
plugins2, err := LoadPlugins(dir, "glob", []string{})
if err != nil {
t.Fatal(err)
}
if len(plugins2) != 2 {
t.Fatalf("got %d plugins (empty allowlist), want 2", len(plugins2))
}
}
-91
View File
@@ -1,91 +0,0 @@
package skill
import (
"os"
"path/filepath"
"testing"
)
func TestParse(t *testing.T) {
src := []byte(`---
name: tdd-bugfix
description: Fix bugs with TDD
version: 1.0.0
metadata:
archie:
tools: [go, golangci-lint]
engine: any
---
## Stage 1: Analyse
Read the code and find the bug.
`)
fm, body, err := Parse(src)
if err != nil {
t.Fatal(err)
}
if fm.Name != "tdd-bugfix" {
t.Errorf("Name: got %q, want tdd-bugfix", fm.Name)
}
if fm.Version != "1.0.0" {
t.Errorf("Version: got %q, want 1.0.0", fm.Version)
}
if fm.Metadata.Archie == nil {
t.Fatal("Metadata.Archie is nil")
}
if len(fm.Metadata.Archie.Tools) != 2 || fm.Metadata.Archie.Tools[0] != "go" {
t.Errorf("Tools: got %v", fm.Metadata.Archie.Tools)
}
if fm.Metadata.Archie.Engine != "any" {
t.Errorf("Engine: got %q, want any", fm.Metadata.Archie.Engine)
}
if body != "## Stage 1: Analyse\n\nRead the code and find the bug." {
t.Errorf("Body: got %q", body)
}
}
func TestParseNoFrontmatter(t *testing.T) {
src := []byte(`# Just markdown
No YAML frontmatter here.
`)
fm, body, err := Parse(src)
if err != nil {
t.Fatal(err)
}
if fm.Name != "" {
t.Errorf("Name should be empty, got %q", fm.Name)
}
if body != string(src) {
t.Errorf("Body should be the full content")
}
}
func TestLoadBody(t *testing.T) {
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "tdd-bugfix")
if err := os.MkdirAll(skillsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
name: tdd-bugfix
description: desc
version: 1.0.0
---
Body content.
`), 0o644); err != nil {
t.Fatal(err)
}
body := LoadBody(dir, "tdd-bugfix")
if body != "Body content." {
t.Errorf("got %q, want 'Body content.'", body)
}
// Non-existent skill returns empty.
body = LoadBody(dir, "nonexistent")
if body != "" {
t.Errorf("expected empty, got %q", body)
}
}
-181
View File
@@ -1,181 +0,0 @@
package skill
import (
"context"
"os"
"path/filepath"
"testing"
)
// ── progressive disclosure: catalog → tool bridge ────────────────────
//
// A chat turn's tool set should offer ONE tool (not one per skill) that
// lets the model activate any cataloged skill by name -- mirroring the
// two-tier disclosure Catalog/LoadBody already implement: the tool's
// schema enumerates every catalog entry's name+description (Tier 1,
// cheap), and calling it returns the full SKILL.md body (Tier 2, loaded
// only on activation).
func writeSkill(t *testing.T, dir, name, description, body string) {
t.Helper()
skillDir := filepath.Join(dir, ".agents", "skills", name)
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
content := "---\nname: " + name + "\ndescription: " + description + "\n---\n" + body + "\n"
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestActivateToolNameAndSchema(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "alpha body")
writeSkill(t, dir, "beta", "does beta things", "beta body")
catalog, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(dir, catalog)
if entry.Name != "skill_activate" {
t.Errorf("Name = %q, want skill_activate", entry.Name)
}
if entry.Toolset != "skill" {
t.Errorf("Toolset = %q, want skill", entry.Toolset)
}
if entry.Handler == nil {
t.Fatal("Handler is nil")
}
enumRaw, ok := enumFromSchema(entry.Schema)
if !ok {
t.Fatal("schema does not expose a name enum")
}
if len(enumRaw) != 2 {
t.Fatalf("schema enum has %d entries, want 2", len(enumRaw))
}
}
func TestActivateToolHandlerReturnsFullBody(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "This is the full alpha body.")
catalog, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(dir, catalog)
out, err := entry.Handler(context.Background(), map[string]any{"name": "alpha"})
if err != nil {
t.Fatal(err)
}
body, ok := out.(string)
if !ok {
t.Fatalf("output type = %T, want string", out)
}
if body != "This is the full alpha body." {
t.Errorf("body = %q", body)
}
}
func TestActivateToolHandlerLoadsFromCatalogEntryRoot(t *testing.T) {
global := t.TempDir()
writeSkill(t, global, "global-only", "global skill", "global body")
catalog, err := CatalogRoots(global)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(t.TempDir(), catalog)
out, err := entry.Handler(context.Background(), map[string]any{"name": "global-only"})
if err != nil {
t.Fatal(err)
}
if out != "global body" {
t.Errorf("body = %v, want global body", out)
}
}
func TestActivateToolHandlerRejectsUnknownSkill(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "alpha body")
catalog, _ := Catalog(dir)
entry := ActivateTool(dir, catalog)
_, err := entry.Handler(context.Background(), map[string]any{"name": "nonexistent"})
if err == nil {
t.Fatal("expected error for unknown skill name")
}
}
func TestActivateToolHandlerRejectsMissingName(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "alpha body")
catalog, _ := Catalog(dir)
entry := ActivateTool(dir, catalog)
_, err := entry.Handler(context.Background(), map[string]any{})
if err == nil {
t.Fatal("expected error for missing name argument")
}
}
func TestActivateToolWithEmptyCatalogReturnsNilEntry(t *testing.T) {
// No skills configured -- don't clutter the tool set with a dead tool.
dir := t.TempDir()
entry := ActivateTool(dir, nil)
if entry != nil {
t.Fatalf("expected nil entry for empty catalog, got %+v", entry)
}
}
func TestActivateToolHandlerUsesSkillDirNotDisplayName(t *testing.T) {
// CatalogEntry.Name comes from frontmatter and CatalogEntry.Dir is the
// directory name -- they can differ. LoadBody keys off the directory
// name, so the tool's activation argument must match what the catalog
// exposes as selectable (Name), and the handler must resolve it back
// to the correct Dir internally.
dir := t.TempDir()
skillDir := filepath.Join(dir, ".agents", "skills", "dir-name-differs")
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
content := "---\nname: Display Name\ndescription: d\n---\nfull body here\n"
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
catalog, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(dir, catalog)
out, err := entry.Handler(context.Background(), map[string]any{"name": "Display Name"})
if err != nil {
t.Fatal(err)
}
if out != "full body here" {
t.Errorf("body = %v", out)
}
}
// enumFromSchema digs the "name" property's enum out of a JSON-Schema-shaped
// map, tolerating the concrete map/slice types produced by our own
// ActivateTool (no external JSON round-trip in this test).
func enumFromSchema(schema map[string]any) ([]any, bool) {
props, ok := schema["properties"].(map[string]any)
if !ok {
return nil, false
}
nameProp, ok := props["name"].(map[string]any)
if !ok {
return nil, false
}
enum, ok := nameProp["enum"].([]any)
return enum, ok
}