package codex import ( "io" "strings" "testing" "time" ) func TestBadLines(t *testing.T) { bad := []string{} input := strings.Join([]string{ `{"method":"turn/started","params":{"threadId":"t","turn":{"id":"u","items":[],"status":"inProgress"}}}`, ``, `warning: something on stdout`, `{"method":"turn/started",`, `{"method":"mystery/event","params":{}}`, `{"method":"turn/completed","params":{"threadId":"t","turn":{"id":"u","items":[],"status":"completed"}}}`, }, "\n") before := time.Now() r, w := io.Pipe() s := newSession(Options{OnBadLine: func(line []byte, reason string) { bad = append(bad, string(line)+" | "+reason) }}, w, strings.NewReader(input)) methods := []string{} for ev := range s.events { methods = append(methods, ev.Method) } r.Close() want := []string{ `warning: something on stdout | not a JSON object`, `{"method":"turn/started", | unexpected end of JSON input`, `{"method":"mystery/event","params":{}} | unrecognised method "mystery/event"`, } if strings.Join(bad, "\n") != strings.Join(want, "\n") { t.Errorf("bad lines:\n%s\nwant:\n%s", strings.Join(bad, "\n"), strings.Join(want, "\n")) } if strings.Join(methods, ",") != "turn/started,mystery/event,turn/completed" { t.Errorf("events = %v", methods) } if s.TurnID() != "" { t.Errorf("turn %q still tracked after turn/completed", s.TurnID()) } if s.LastLine().Before(before) { t.Errorf("last line time %v not updated", s.LastLine()) } if !s.Exited() { t.Errorf("session not exited after stdout closed") } }