|
| 1 | +// Copyright 2026 Jordan Sherer <hi@jordansherer.com> |
| 2 | +// SPDX-License-Identifier: gpl |
| 3 | + |
| 4 | +package configyaml |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "reflect" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/factorly-dev/factorly/internal/config" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +func TestRenderTool_CLI(t *testing.T) { |
| 18 | + tc := config.ToolConfig{ |
| 19 | + Type: "cli", |
| 20 | + Description: "list files in cwd", |
| 21 | + Command: "ls", |
| 22 | + Args: []string{"-la"}, |
| 23 | + } |
| 24 | + out, err := RenderTool("ls.list", tc) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("RenderTool: %v", err) |
| 27 | + } |
| 28 | + got := string(out) |
| 29 | + if !strings.HasPrefix(got, "tools:") { |
| 30 | + t.Errorf("expected top-level tools: key, got:\n%s", got) |
| 31 | + } |
| 32 | + if !strings.Contains(got, "ls.list:") { |
| 33 | + t.Errorf("expected tool name as nested key, got:\n%s", got) |
| 34 | + } |
| 35 | + if !strings.Contains(got, "command: ls") { |
| 36 | + t.Errorf("expected command field, got:\n%s", got) |
| 37 | + } |
| 38 | + |
| 39 | + // Round-trip back through the loader and assert the nested ToolConfig |
| 40 | + // matches the original. |
| 41 | + var doc struct { |
| 42 | + Tools map[string]config.ToolConfig `yaml:"tools"` |
| 43 | + } |
| 44 | + if err := yaml.Unmarshal(out, &doc); err != nil { |
| 45 | + t.Fatalf("round-trip unmarshal: %v", err) |
| 46 | + } |
| 47 | + if !reflect.DeepEqual(doc.Tools["ls.list"], tc) { |
| 48 | + t.Errorf("round-trip mismatch\nwant: %+v\ngot: %+v", tc, doc.Tools["ls.list"]) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRenderTool_REST(t *testing.T) { |
| 53 | + tc := config.ToolConfig{ |
| 54 | + Type: "rest", |
| 55 | + Description: "list issues", |
| 56 | + BaseURL: "https://api.linear.app", |
| 57 | + Method: "POST", |
| 58 | + Path: "/graphql", |
| 59 | + Headers: map[string]string{"Content-Type": "application/json"}, |
| 60 | + } |
| 61 | + out, err := RenderTool("linear.issues", tc) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("RenderTool: %v", err) |
| 64 | + } |
| 65 | + var doc struct { |
| 66 | + Tools map[string]config.ToolConfig `yaml:"tools"` |
| 67 | + } |
| 68 | + if err := yaml.Unmarshal(out, &doc); err != nil { |
| 69 | + t.Fatalf("round-trip unmarshal: %v", err) |
| 70 | + } |
| 71 | + if !reflect.DeepEqual(doc.Tools["linear.issues"], tc) { |
| 72 | + t.Errorf("round-trip mismatch\nwant: %+v\ngot: %+v", tc, doc.Tools["linear.issues"]) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestRenderTool_Workflow(t *testing.T) { |
| 77 | + tc := config.ToolConfig{ |
| 78 | + Type: "workflow", |
| 79 | + Description: "morning prep", |
| 80 | + Steps: []config.StepConfig{ |
| 81 | + {Tool: "factorly.fetch", Params: map[string]string{"url": "https://example.com"}, Store: "data"}, |
| 82 | + }, |
| 83 | + } |
| 84 | + out, err := RenderTool("prep.daily", tc) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("RenderTool: %v", err) |
| 87 | + } |
| 88 | + if !strings.Contains(string(out), "type: workflow") { |
| 89 | + t.Errorf("expected type: workflow, got:\n%s", out) |
| 90 | + } |
| 91 | + |
| 92 | + var doc struct { |
| 93 | + Tools map[string]config.ToolConfig `yaml:"tools"` |
| 94 | + } |
| 95 | + if err := yaml.Unmarshal(out, &doc); err != nil { |
| 96 | + t.Fatalf("round-trip unmarshal: %v", err) |
| 97 | + } |
| 98 | + if !reflect.DeepEqual(doc.Tools["prep.daily"], tc) { |
| 99 | + t.Errorf("round-trip mismatch\nwant: %+v\ngot: %+v", tc, doc.Tools["prep.daily"]) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestRenderTool_EmptyName(t *testing.T) { |
| 104 | + if _, err := RenderTool("", config.ToolConfig{Type: "cli"}); err == nil { |
| 105 | + t.Error("expected error for empty name") |
| 106 | + } |
| 107 | + if _, err := RenderTool(" ", config.ToolConfig{Type: "cli"}); err == nil { |
| 108 | + t.Error("expected error for whitespace-only name") |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestRenderBlueprint_RawBytes(t *testing.T) { |
| 113 | + dir := t.TempDir() |
| 114 | + bpDir := filepath.Join(dir, ".factorly", "blueprints") |
| 115 | + if err := os.MkdirAll(bpDir, 0o755); err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + cfgPath := filepath.Join(dir, ".factorly", "factorly.yaml") |
| 119 | + |
| 120 | + // A blueprint file with a comment + custom field ordering — RenderBlueprint |
| 121 | + // must return these bytes verbatim, not re-marshal them. |
| 122 | + source := []byte(`# my custom comment |
| 123 | +name: gmail |
| 124 | +version: 1.0.0 |
| 125 | +tools: |
| 126 | + gmail.list: |
| 127 | + type: rest |
| 128 | + description: list messages |
| 129 | +`) |
| 130 | + if err := os.WriteFile(filepath.Join(bpDir, "gmail.yaml"), source, 0o644); err != nil { |
| 131 | + t.Fatal(err) |
| 132 | + } |
| 133 | + |
| 134 | + got, err := RenderBlueprint(cfgPath, "gmail") |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("RenderBlueprint: %v", err) |
| 137 | + } |
| 138 | + if string(got) != string(source) { |
| 139 | + t.Errorf("blueprint not returned byte-for-byte\nwant:\n%s\ngot:\n%s", source, got) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestRenderBlueprint_NotInstalled(t *testing.T) { |
| 144 | + dir := t.TempDir() |
| 145 | + cfgPath := filepath.Join(dir, ".factorly", "factorly.yaml") |
| 146 | + if _, err := RenderBlueprint(cfgPath, "nope"); err == nil { |
| 147 | + t.Error("expected error for missing blueprint") |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestRenderBlueprint_RejectsTraversal(t *testing.T) { |
| 152 | + dir := t.TempDir() |
| 153 | + cfgPath := filepath.Join(dir, ".factorly", "factorly.yaml") |
| 154 | + cases := []string{"../etc/passwd", "..", "", ".", "foo/bar", `foo\bar`} |
| 155 | + for _, name := range cases { |
| 156 | + if _, err := RenderBlueprint(cfgPath, name); err == nil { |
| 157 | + t.Errorf("expected rejection for %q", name) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestBlueprintsDir(t *testing.T) { |
| 163 | + // cfgPath inside .factorly/ → use sibling blueprints/ |
| 164 | + got := BlueprintsDir("/proj/.factorly/factorly.yaml") |
| 165 | + if got != "/proj/.factorly/blueprints" { |
| 166 | + t.Errorf("inside .factorly: got %q", got) |
| 167 | + } |
| 168 | + // cfgPath outside .factorly/ → add .factorly/blueprints/ |
| 169 | + got = BlueprintsDir("/proj/factorly.yaml") |
| 170 | + if got != "/proj/.factorly/blueprints" { |
| 171 | + t.Errorf("outside .factorly: got %q", got) |
| 172 | + } |
| 173 | +} |
0 commit comments