package codex import ( "bytes" "encoding/json" "os" "testing" ) func TestCodexToolVisibility(t *testing.T) { raw, e := os.ReadFile("testdata/probe.jsonl") if e != nil { t.Fatal(e) } found := false for _, line := range bytes.Split(bytes.TrimSpace(raw), []byte("\n")) { events, e := Decode(line) if e != nil { t.Fatal(e) } for _, ev := range events { if ev.Kind == "tool.completed" && bytes.Contains(ev.Payload, []byte("Permission denied")) { found = true } } } if !found { t.Fatal("missing command execution output inside code-mode invocation") } } func TestCodexUsageBeforeCompletion(t *testing.T) { raw, _ := os.ReadFile("testdata/probe.jsonl") var totals []int completed := false for _, line := range bytes.Split(bytes.TrimSpace(raw), []byte("\n")) { events, e := Decode(line) if e != nil { t.Fatal(e) } for _, ev := range events { if ev.Kind == "usage" { var v struct { Params struct { TokenUsage struct{ Total struct{ InputTokens int } } } } json.Unmarshal(ev.Payload, &v) totals = append(totals, v.Params.TokenUsage.Total.InputTokens) } if ev.Kind == "turn.completed" { completed = true if len(totals) != 2 { t.Fatal("completion lost prior usage") } } } } if !completed || len(totals) != 2 || totals[0] != 14054 || totals[1] != 28254 { t.Fatal(totals, completed) } } func TestCodexMalformedFrames(t *testing.T) { for _, raw := range []string{`{`, `{"method":"x","method":"y","params":{}}`, `null`, `{"id":null,"result":{}}`, `{"id":1,"result":{},"error":{}}`, `{"method":"turn/completed","params":{"threadId":"t","turn":{"id":"u"}}}`} { if _, e := Decode([]byte(raw)); e == nil { t.Fatalf("accepted %s", raw) } } if _, e := Decode(append(append([]byte(`{"method":"x","params":{"large":"`), bytes.Repeat([]byte("x"), MaxFrameBytes)...), []byte(`"}}`)...)); e == nil { t.Fatal("oversized accepted") } } func TestCodexFrameIdentity(t *testing.T) { for _, raw := range []string{`{"id":1.5,"result":{}}`, `{"id":9223372036854775808,"result":{}}`, `{"method":"turn/started","params":{"turn":{"id":"u"}}}`, `{"method":"item/completed","params":{"threadId":"t","item":{"type":"commandExecution"}}}`, `{"method":"thread/tokenUsage/updated","params":{"turnId":"u"}}}`} { if _, e := Decode([]byte(raw)); e == nil { t.Fatalf("accepted invalid frame identity %s", raw) } } for _, id := range []string{`1`, `"1"`, `-9223372036854775808`} { raw := []byte(`{"id":` + id + `,"method":"item/tool/requestUserInput","params":{"threadId":"t","turnId":"u"}}`) ev, e := Decode(raw) if e != nil || len(ev) != 1 || ev[0].RequestID != id { t.Fatal("server request ID type lost", id, ev, e) } } }