package promptcontext import ( "encoding/json" "reflect" "testing" "gitea.maximumdirect.net/eric/audita/internal/core/schema" ) func TestMarshalTranscriptSectionJSONShape(t *testing.T) { transcript := &schema.Transcript{Segments: []schema.Segment{ {ID: 1, Speaker: "A", Start: 0.1, End: 1.2, Text: "alpha", Categories: []string{"session", "intro"}}, }} raw, err := MarshalTranscriptSectionJSON(transcript, 3) if err != nil { t.Fatalf("MarshalTranscriptSectionJSON error: %v", err) } var decoded map[string]any if err := json.Unmarshal(raw, &decoded); err != nil { t.Fatalf("unmarshal: %v", err) } if got := decoded["section_index"]; got != float64(3) { t.Fatalf("section_index: got=%v want=%v", got, 3) } segments, ok := decoded["segments"].([]any) if !ok || len(segments) != 1 { t.Fatalf("segments shape mismatch: %T %+v", decoded["segments"], decoded["segments"]) } first, ok := segments[0].(map[string]any) if !ok { t.Fatalf("segment shape mismatch: %T", segments[0]) } if first["id"] != float64(1) || first["speaker"] != "A" || first["start"] != 0.1 || first["end"] != 1.2 || first["text"] != "alpha" { t.Fatalf("unexpected segment fields: %+v", first) } cats, ok := first["categories"].([]any) if !ok || len(cats) != 2 || cats[0] != "session" || cats[1] != "intro" { t.Fatalf("unexpected categories: %+v", first["categories"]) } } func TestMarshalTranscriptSectionJSONEmptyTranscript(t *testing.T) { raw, err := MarshalTranscriptSectionJSON(nil, 0) if err != nil { t.Fatalf("MarshalTranscriptSectionJSON error: %v", err) } var decoded map[string]any if err := json.Unmarshal(raw, &decoded); err != nil { t.Fatalf("unmarshal: %v", err) } if got := decoded["section_index"]; got != float64(0) { t.Fatalf("section_index: got=%v want=%v", got, 0) } segments, ok := decoded["segments"].([]any) if !ok { t.Fatalf("segments shape mismatch: %T", decoded["segments"]) } if len(segments) != 0 { t.Fatalf("expected empty segments, got %d", len(segments)) } } func TestMarshalTranscriptSectionJSONCopiesCategories(t *testing.T) { transcript := &schema.Transcript{Segments: []schema.Segment{ {ID: 1, Speaker: "A", Start: 0, End: 1, Text: "alpha", Categories: []string{"kept"}}, }} raw, err := MarshalTranscriptSectionJSON(transcript, 1) if err != nil { t.Fatalf("MarshalTranscriptSectionJSON error: %v", err) } transcript.Segments[0].Categories[0] = "changed" var decoded struct { Segments []struct { Categories []string `json:"categories"` } `json:"segments"` } if err := json.Unmarshal(raw, &decoded); err != nil { t.Fatalf("unmarshal: %v", err) } if len(decoded.Segments) != 1 { t.Fatalf("expected one segment, got %d", len(decoded.Segments)) } if !reflect.DeepEqual(decoded.Segments[0].Categories, []string{"kept"}) { t.Fatalf("expected copied categories, got %v", decoded.Segments[0].Categories) } }