|
| 1 | +package spec |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +func TestLoadResourceFile_validKinds(t *testing.T) { |
| 15 | + cases := []struct { |
| 16 | + name string |
| 17 | + doc any |
| 18 | + }{ |
| 19 | + {"Project", sampleProject()}, |
| 20 | + {"Agent", sampleAgent()}, |
| 21 | + {"Tool_MCP", sampleToolMCP()}, |
| 22 | + {"Tool_HTTP", sampleToolHTTP()}, |
| 23 | + {"Workflow", sampleWorkflow()}, |
| 24 | + {"Policy", samplePolicy()}, |
| 25 | + {"Environment", sampleEnvironment()}, |
| 26 | + } |
| 27 | + |
| 28 | + for _, tc := range cases { |
| 29 | + t.Run(tc.name, func(t *testing.T) { |
| 30 | + data, err := yaml.Marshal(tc.doc) |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + dir := t.TempDir() |
| 35 | + p := filepath.Join(dir, "res.yaml") |
| 36 | + if err := os.WriteFile(p, data, 0o600); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + |
| 40 | + got, err := LoadResourceFile(p) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("LoadResourceFile: %v", err) |
| 43 | + } |
| 44 | + if got.Path != p { |
| 45 | + t.Fatalf("Path = %q, want %q", got.Path, p) |
| 46 | + } |
| 47 | + if !reflect.DeepEqual(got.Resource, tc.doc) { |
| 48 | + t.Fatalf("resource mismatch\n got %#v\nwant %#v", got.Resource, tc.doc) |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestParseResourceFromBytes_missingMetadataName(t *testing.T) { |
| 55 | + const y = ` |
| 56 | +apiVersion: agentic.dev/v0 |
| 57 | +kind: Agent |
| 58 | +metadata: {} |
| 59 | +spec: {} |
| 60 | +` |
| 61 | + _, err := ParseResourceFromBytes([]byte(y), "/tmp/agent.yaml") |
| 62 | + if err == nil { |
| 63 | + t.Fatal("expected error") |
| 64 | + } |
| 65 | + var le *LoadError |
| 66 | + if !errors.As(err, &le) { |
| 67 | + t.Fatalf("want *LoadError, got %T: %v", err, err) |
| 68 | + } |
| 69 | + if !strings.Contains(le.Error(), "/tmp/agent.yaml") { |
| 70 | + t.Fatalf("error should mention path: %q", le.Error()) |
| 71 | + } |
| 72 | + if !strings.Contains(le.Error(), "metadata.name") { |
| 73 | + t.Fatalf("error should mention metadata.name: %q", le.Error()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestLoadResourceFile_invalidYAML_wrapsPath(t *testing.T) { |
| 78 | + dir := t.TempDir() |
| 79 | + p := filepath.Join(dir, "bad.yaml") |
| 80 | + // Line 2: invalid indentation triggers a parser error with a line hint. |
| 81 | + content := "apiVersion: x\n kind: Agent\nmetadata: {name: a}\nspec: {}\n" |
| 82 | + if err := os.WriteFile(p, []byte(content), 0o600); err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + |
| 86 | + _, err := LoadResourceFile(p) |
| 87 | + if err == nil { |
| 88 | + t.Fatal("expected error") |
| 89 | + } |
| 90 | + var le *LoadError |
| 91 | + if !errors.As(err, &le) { |
| 92 | + t.Fatalf("want *LoadError, got %T: %v", err, err) |
| 93 | + } |
| 94 | + if le.Path != p { |
| 95 | + t.Fatalf("LoadError.Path = %q, want %q", le.Path, p) |
| 96 | + } |
| 97 | + if !strings.Contains(le.Error(), p) { |
| 98 | + t.Fatalf("error string should contain path: %q", le.Error()) |
| 99 | + } |
| 100 | + _ = le.Line // best-effort from yaml error text; message always includes path |
| 101 | +} |
| 102 | + |
| 103 | +func TestParseResourceFromBytes_multipleDocuments(t *testing.T) { |
| 104 | + y := ` |
| 105 | +apiVersion: agentic.dev/v0 |
| 106 | +kind: Project |
| 107 | +metadata: {name: a} |
| 108 | +spec: {} |
| 109 | +--- |
| 110 | +apiVersion: agentic.dev/v0 |
| 111 | +kind: Project |
| 112 | +metadata: {name: b} |
| 113 | +spec: {} |
| 114 | +` |
| 115 | + _, err := ParseResourceFromBytes([]byte(y), "multi.yaml") |
| 116 | + if err == nil { |
| 117 | + t.Fatal("expected error") |
| 118 | + } |
| 119 | + if !errors.Is(err, ErrMultipleDocuments) { |
| 120 | + t.Fatalf("want ErrMultipleDocuments in chain, got %v", err) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestParseResourceFromBytes_unknownKind(t *testing.T) { |
| 125 | + y := ` |
| 126 | +apiVersion: agentic.dev/v0 |
| 127 | +kind: NotAKind |
| 128 | +metadata: {name: x} |
| 129 | +spec: {} |
| 130 | +` |
| 131 | + _, err := ParseResourceFromBytes([]byte(y), "x.yaml") |
| 132 | + if err == nil { |
| 133 | + t.Fatal("expected error") |
| 134 | + } |
| 135 | + if !errors.Is(err, ErrUnknownKind) { |
| 136 | + t.Fatalf("want ErrUnknownKind in chain, got %v", err) |
| 137 | + } |
| 138 | +} |
0 commit comments