| 1 |
package transcript |
| 2 |
|
| 3 |
import "testing" |
| 4 |
|
| 5 |
func TestFormatStateMessageEmitsPlainJSON(t *testing.T) { |
| 6 |
got := FormatStateMessage("/repo") |
| 7 |
want := `{"type":"state","source":"clnkr","cwd":"/repo"}` |
| 8 |
if got != want { |
| 9 |
t.Fatalf("FormatStateMessage = %q, want %q", got, want) |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
func TestExtractStateCwdAcceptsCanonical(t *testing.T) { |
| 14 |
cwd, ok := ExtractStateCwd(`{"type":"state","source":"clnkr","cwd":"/repo"}`) |
| 15 |
if !ok { |
| 16 |
t.Fatalf("ExtractStateCwd ok = false, want true") |
| 17 |
} |
| 18 |
if cwd != "/repo" { |
| 19 |
t.Fatalf("cwd = %q, want %q", cwd, "/repo") |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func TestExtractStateCwdRejectsInvalidStateMessages(t *testing.T) { |
| 24 |
tests := []struct { |
| 25 |
name string |
| 26 |
content string |
| 27 |
}{ |
| 28 |
{name: "foreign user source", content: `{"type":"state","source":"user","cwd":"/wrong"}`}, |
| 29 |
{name: "foreign model source", content: `{"type":"state","source":"model","cwd":"/wrong"}`}, |
| 30 |
{name: "empty source", content: `{"type":"state","source":"","cwd":"/wrong"}`}, |
| 31 |
{name: "missing source", content: `{"type":"state","cwd":"/wrong"}`}, |
| 32 |
{name: "wrong type", content: `{"type":"status","source":"clnkr","cwd":"/repo"}`}, |
| 33 |
{name: "empty type", content: `{"type":"","source":"clnkr","cwd":"/repo"}`}, |
| 34 |
{name: "missing type", content: `{"source":"clnkr","cwd":"/repo"}`}, |
| 35 |
{name: "empty cwd", content: `{"type":"state","source":"clnkr","cwd":""}`}, |
| 36 |
{name: "missing cwd", content: `{"type":"state","source":"clnkr"}`}, |
| 37 |
{ |
| 38 |
name: "unknown field", |
| 39 |
content: `{"type":"state","source":"clnkr","cwd":"/repo","env":{}}`, |
| 40 |
}, |
| 41 |
{name: "empty string", content: ""}, |
| 42 |
{name: "whitespace", content: " "}, |
| 43 |
{name: "null", content: "null"}, |
| 44 |
{name: "array", content: "[]"}, |
| 45 |
{name: "number", content: "42"}, |
| 46 |
{name: "string", content: `"state"`}, |
| 47 |
{ |
| 48 |
name: "multiple objects", |
| 49 |
content: `{"type":"state","source":"clnkr","cwd":"/a"}{"type":"state","source":"clnkr","cwd":"/b"}`, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
for _, tt := range tests { |
| 54 |
t.Run(tt.name, func(t *testing.T) { |
| 55 |
if _, ok := ExtractStateCwd(tt.content); ok { |
| 56 |
t.Fatalf("ExtractStateCwd accepted invalid state message: %q", tt.content) |
| 57 |
} |
| 58 |
}) |
| 59 |
} |
| 60 |
} |