package describe import ( "strings" "testing" ) func TestExtractGoSource_SingleBlock(t *testing.T) { answer := "Here's the code:\n\n```go\npackage main\n\nfunc main() {}\n```\n\nThat should work." got := ExtractGoSource(answer) if !strings.HasPrefix(got, "package main") { t.Errorf("expected package main, got: %q", got) } if !strings.Contains(got, "func main()") { t.Errorf("expected func main(), got: %q", got) } } func TestExtractGoSource_MultipleBlocks(t *testing.T) { answer := "Short example:\n```go\npackage p\n```\n\nFull code:\n```go\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"hello\")\n}\n```\n" got := ExtractGoSource(answer) // Should return the longer block. if !strings.Contains(got, "fmt.Println") { t.Errorf("expected longer block with fmt.Println, got: %q", got) } } func TestExtractGoSource_NoBlocks(t *testing.T) { // When response has no code blocks but starts with "package". answer := "package main\n\nfunc main() {}\n" got := ExtractGoSource(answer) if !strings.HasPrefix(got, "package main") { t.Errorf("expected raw source fallback, got: %q", got) } } func TestExtractGoSource_Empty(t *testing.T) { got := ExtractGoSource("No code here at all.") if got != "" { t.Errorf("expected empty, got: %q", got) } } func TestExtractGoSource_PlainBackticks(t *testing.T) { answer := "Here:\n```\npackage main\n\nfunc main() {}\n```\n" got := ExtractGoSource(answer) if !strings.HasPrefix(got, "package main") { t.Errorf("expected fallback to plain backticks, got: %q", got) } } func TestComposeCodeQuery_ContainsDescription(t *testing.T) { desc := Description{Text: "Add a function named Foo that returns 42"} query := ComposeCodeQuery(desc, "Types: Bar\nFunctions: Baz\n") if !strings.Contains(query, "Foo") { t.Error("query should contain the function name from description") } if !strings.Contains(query, "Bar") { t.Error("query should contain self-knowledge type") } if !strings.Contains(query, "Baz") { t.Error("query should contain self-knowledge function") } } func TestComposeCodeQuery_WithTargetPkg(t *testing.T) { desc := Description{Text: "some code", TargetPkg: "lattice"} query := ComposeCodeQuery(desc, "") if !strings.Contains(query, "package lattice") { t.Error("query should mention target package") } } func TestExtractExpectedNames(t *testing.T) { tests := []struct { desc string expected []string }{ {"Add a function named CountNodes that returns int", []string{"CountNodes"}}, {"Define a type called Summary with fields", []string{"Summary"}}, {"Create a function Foo that does something and a type Bar", []string{"Foo", "Bar"}}, {"Add a method named Process", []string{"Process"}}, {"No names here", nil}, } for _, tt := range tests { got := extractExpectedNames(tt.desc) if len(got) != len(tt.expected) { t.Errorf("desc=%q: got %v, want %v", tt.desc, got, tt.expected) continue } for i := range got { if got[i] != tt.expected[i] { t.Errorf("desc=%q: got[%d]=%q, want %q", tt.desc, i, got[i], tt.expected[i]) } } } } func TestExtractDeclarations(t *testing.T) { src := `package main type Foo struct{} type Bar interface{} func Baz() {} func (f Foo) Qux() {} ` decls := extractDeclarations(src) expected := map[string]bool{ "package:main": true, "type:Foo": true, "type:Bar": true, "func:Baz": true, "method:Qux": true, } got := make(map[string]bool) for _, d := range decls { got[d] = true } for k := range expected { if !got[k] { t.Errorf("missing declaration: %s", k) } } } func TestExtractSelfKnowledge_Nil(t *testing.T) { got := ExtractSelfKnowledge(nil) if got != "" { t.Errorf("expected empty for nil lattice, got: %q", got) } } func TestJoinKeys(t *testing.T) { m := map[string]bool{"c": true, "a": true, "b": true} got := joinKeys(m) if got != "a, b, c" { t.Errorf("expected sorted keys, got: %q", got) } }