156 lines
4.3 KiB
Go
156 lines
4.3 KiB
Go
package scenes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/prompt"
|
|
)
|
|
|
|
func TestBuildPromptDataFromSourceDocument(t *testing.T) {
|
|
req := promptChunkRequest()
|
|
|
|
data, err := buildPromptData(req)
|
|
if err != nil {
|
|
t.Fatalf("buildPromptData() error = %v, want nil", err)
|
|
}
|
|
|
|
if data.SourceID != "session-alpha" {
|
|
t.Fatalf("SourceID = %q, want session-alpha", data.SourceID)
|
|
}
|
|
if len(data.Units) != 2 {
|
|
t.Fatalf("len(Units) = %d, want 2", len(data.Units))
|
|
}
|
|
first := data.Units[0]
|
|
if first.ID != "seg-001" || first.Text != "Aria and Bram discuss whether to enter the ruins." {
|
|
t.Fatalf("first unit = %#v, want source unit data", first)
|
|
}
|
|
wantMetadata := []promptMetadata{
|
|
{Key: "speaker", Value: "Alice"},
|
|
{Key: "start", Value: "1.25"},
|
|
{Key: "end", Value: "3.5"},
|
|
}
|
|
if !reflect.DeepEqual(first.Metadata, wantMetadata) {
|
|
t.Fatalf("first.Metadata = %#v, want %#v", first.Metadata, wantMetadata)
|
|
}
|
|
if len(data.Units[1].Metadata) != 0 {
|
|
t.Fatalf("second.Metadata = %#v, want no selected metadata", data.Units[1].Metadata)
|
|
}
|
|
}
|
|
|
|
func TestBuildPromptDataDoesNotMutateRequest(t *testing.T) {
|
|
req := promptChunkRequest()
|
|
beforeSource := mustJSON(t, req.Source)
|
|
beforeRequest := mustJSON(t, req)
|
|
|
|
if _, err := buildPromptData(req); err != nil {
|
|
t.Fatalf("buildPromptData() error = %v, want nil", err)
|
|
}
|
|
afterSource := mustJSON(t, req.Source)
|
|
afterRequest := mustJSON(t, req)
|
|
if beforeSource != afterSource || beforeRequest != afterRequest {
|
|
t.Fatalf(
|
|
"request mutated:\nsource before: %s\nsource after: %s\nrequest before: %s\nrequest after: %s",
|
|
beforeSource,
|
|
afterSource,
|
|
beforeRequest,
|
|
afterRequest,
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestRenderPromptIncludesSourceUnitsAndMetadata(t *testing.T) {
|
|
system, user, metadata, err := renderPrompt(promptChunkRequest())
|
|
if err != nil {
|
|
t.Fatalf("renderPrompt() error = %v, want nil", err)
|
|
}
|
|
|
|
if !strings.Contains(system, prompt.HardeningText()) {
|
|
t.Fatalf("system prompt = %q, want hardening text", system)
|
|
}
|
|
for _, want := range []string{
|
|
"session-alpha",
|
|
"seg-001",
|
|
"seg-002",
|
|
"Aria and Bram discuss whether to enter the ruins.",
|
|
"The goblins rush out and initiative begins.",
|
|
"speaker: Alice",
|
|
"start: 1.25",
|
|
"end: 3.5",
|
|
"start_unit_id",
|
|
"end_unit_id",
|
|
"primary_mode",
|
|
"boundary_confidence",
|
|
"Recap, Discussion, Combat, or Narrative",
|
|
"High, Medium, or Low",
|
|
"no gaps",
|
|
"do not overlap",
|
|
} {
|
|
if !strings.Contains(user, want) {
|
|
t.Fatalf("user prompt = %q, want substring %q", user, want)
|
|
}
|
|
}
|
|
if metadata.PromptID != PromptID {
|
|
t.Fatalf("metadata.PromptID = %q, want %q", metadata.PromptID, PromptID)
|
|
}
|
|
if metadata.PromptVersion != ResponseSchemaVersion {
|
|
t.Fatalf("metadata.PromptVersion = %q, want %q", metadata.PromptVersion, ResponseSchemaVersion)
|
|
}
|
|
if metadata.EmbeddedPath != "assets/prompts" {
|
|
t.Fatalf("metadata.EmbeddedPath = %q, want assets/prompts", metadata.EmbeddedPath)
|
|
}
|
|
}
|
|
|
|
func TestBuildPromptDataRejectsMissingSourceContext(t *testing.T) {
|
|
if _, err := buildPromptData(contracts.ChunkRequest{}); err == nil || !strings.Contains(err.Error(), "source") {
|
|
t.Fatalf("buildPromptData() error = %v, want source error", err)
|
|
}
|
|
}
|
|
|
|
func promptChunkRequest() contracts.ChunkRequest {
|
|
return contracts.ChunkRequest{
|
|
Source: promptSourceDocument(),
|
|
}
|
|
}
|
|
|
|
func promptSourceDocument() *source.SourceDocument {
|
|
return &source.SourceDocument{
|
|
ID: "session-alpha",
|
|
Kind: "transcript",
|
|
Format: "application/vnd.seriatim.minimal+json",
|
|
Digest: "sha256:test",
|
|
Units: []source.SourceUnit{
|
|
{
|
|
ID: "seg-001",
|
|
Kind: "transcript_segment",
|
|
Text: "Aria and Bram discuss whether to enter the ruins.",
|
|
Metadata: map[string]any{
|
|
"speaker": "Alice",
|
|
"start": json.Number("1.25"),
|
|
"end": json.Number("3.5"),
|
|
"ignored": "not rendered",
|
|
},
|
|
},
|
|
{
|
|
ID: "seg-002",
|
|
Kind: "transcript_segment",
|
|
Text: "The goblins rush out and initiative begins.",
|
|
Metadata: map[string]any{"ignored": "not rendered"},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func mustJSON(t *testing.T, value any) string {
|
|
t.Helper()
|
|
encoded, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Fatalf("Marshal() error = %v, want nil", err)
|
|
}
|
|
return string(encoded)
|
|
}
|