Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222package transcript_test
import ( "encoding/json" "os" "path/filepath" "strings" "testing"
"git.sr.ht/~jcmuller/claude-plugins/transcript" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require")
// writeTranscript writes JSONL lines to a temp file and returns its path.func writeTranscript(t *testing.T, lines []any) string { t.Helper() f, err := os.CreateTemp(t.TempDir(), "transcript-*.jsonl") require.NoError(t, err) defer f.Close() //nolint:errcheck
enc := json.NewEncoder(f) for _, line := range lines { require.NoError(t, enc.Encode(line)) } return f.Name()}
func assistantMsg(text string) map[string]any { return map[string]any{ "type": "assistant", "message": map[string]any{ "content": []any{ map[string]any{"type": "text", "text": text}, }, }, }}
const defaultWrap = 50
func TestExtract_EmptyPathReturnsEmpty(t *testing.T) { t.Parallel() assert.Empty(t, transcript.Extract("", ".go", defaultWrap))}
func TestExtract_NonExistentFileReturnsEmpty(t *testing.T) { t.Parallel() assert.Empty(t, transcript.Extract("/nonexistent/path.jsonl", ".go", defaultWrap))}
func TestExtract_ExtractsLastAssistantText(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("First message"), assistantMsg("Second message"), }) result := transcript.Extract(path, ".go", defaultWrap) assert.Contains(t, result, "Second message")}
func TestExtract_StripsMarkdownBold(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("**Important** change to the file"), }) result := transcript.Extract(path, ".go", defaultWrap) assert.NotContains(t, result, "**") assert.Contains(t, result, "Important")}
func TestExtract_StripsMarkdownBackticks(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("Updating `foo.go` with fix"), }) result := transcript.Extract(path, ".go", defaultWrap) assert.NotContains(t, result, "`")}
func TestExtract_SkipsInsightLines(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("★ Insight ───\nsome insight\n───\nActual description"), }) result := transcript.Extract(path, ".go", defaultWrap) assert.NotContains(t, result, "★") assert.NotContains(t, result, "───")}
func TestExtract_WrapsLongLines(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("This is a very long description that should be wrapped into multiple shorter lines for display"), }) result := transcript.Extract(path, ".go", defaultWrap) for _, line := range splitLines(result) { assert.LessOrEqual(t, len(line), 60, "line too long: %q", line) }}
func TestExtract_RespectsWidthLarge(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("This is a long single-line description that fits in one wide pane"), }) result := transcript.Extract(path, ".go", 200) assert.Equal(t, 1, countBodyLines(result), "wide pane should produce one body line, got %q", result)}
func TestExtract_RespectsWidthSmall(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("This is a long single-line description that must wrap in a very narrow pane"), }) result := transcript.Extract(path, ".go", 20) assert.GreaterOrEqual(t, countBodyLines(result), 3, "narrow pane should wrap into many body lines, got %q", result)}
// countBodyLines returns the number of lines in result that are not// sentinel marker lines, so width assertions can ignore the begin/end// markers that wrap every context block.func countBodyLines(result string) int { n := 0 for _, line := range splitLines(result) { if strings.HasSuffix(line, transcript.BeginMarker) || strings.HasSuffix(line, transcript.EndMarker) { continue } n++ } return n}
func TestExtract_WrapsContextInSentinelMarkers(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("Adding error handling"), }) result := transcript.Extract(path, ".go", defaultWrap) lines := splitLines(result) require.GreaterOrEqual(t, len(lines), 3, "need begin + body + end, got %q", result) assert.True(t, strings.HasSuffix(lines[0], transcript.BeginMarker), "first line should end with begin marker, got %q", lines[0]) assert.True(t, strings.HasSuffix(lines[len(lines)-1], transcript.EndMarker), "last line should end with end marker, got %q", lines[len(lines)-1]) assert.Contains(t, result, "[context] Adding error handling")}
func TestExtract_PrefixesWithCommentStyle(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ assistantMsg("Fixing the bug"), })
goResult := transcript.Extract(path, ".go", defaultWrap) assert.Contains(t, goResult, "// [context]")
rbResult := transcript.Extract(path, ".rb", defaultWrap) assert.Contains(t, rbResult, "# [context]")
sqlResult := transcript.Extract(path, ".sql", defaultWrap) assert.Contains(t, sqlResult, "-- [context]")}
func TestExtract_EmptyTextContentReturnsEmpty(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ map[string]any{ "type": "assistant", "message": map[string]any{ "content": []any{ map[string]any{"type": "tool_use", "id": "x"}, }, }, }, }) assert.Empty(t, transcript.Extract(path, ".go", defaultWrap))}
func TestExtract_NoAssistantMessagesReturnsEmpty(t *testing.T) { t.Parallel() path := writeTranscript(t, []any{ map[string]any{"type": "human", "message": "hello"}, }) assert.Empty(t, transcript.Extract(path, ".go", defaultWrap))}
func splitLines(s string) []string { var lines []string start := 0 for i, c := range s { if c == '\n' { lines = append(lines, s[start:i]) start = i + 1 } } if start < len(s) { lines = append(lines, s[start:]) } return lines}
func TestExtract_WritesFixtureForManualInspection(t *testing.T) { t.Parallel() dir := t.TempDir() path := filepath.Join(dir, "transcript.jsonl")
lines := []any{ assistantMsg("**Updating** `foo.go` to fix the bug in `parseDate`"), } data, err := os.Create(path) require.NoError(t, err) enc := json.NewEncoder(data) for _, l := range lines { require.NoError(t, enc.Encode(l)) } data.Close() //nolint:errcheck
result := transcript.Extract(path, ".go", defaultWrap) assert.NotEmpty(t, result) t.Logf("extracted context:\n%s", result)}