Files
notarius/internal/modules/dnd/extract/spells/test_helpers_test.go

205 lines
6.7 KiB
Go
Raw Normal View History

package spells
import (
2026-07-17 06:50:08 +00:00
"context"
"encoding/json"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/assets"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
spellcatalog "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/spells/catalog"
)
func promptExtractionRequest() contracts.TypedExtractionRequest[dnd.SpellList] {
doc := promptSourceDocument()
chunk := &source.Chunk{
ID: "session-alpha:chunk:0",
SourceID: doc.ID,
Index: 0,
Ref: source.SourceRef{
SourceID: doc.ID,
StartUnitID: doc.Units[0].ID,
EndUnitID: doc.Units[len(doc.Units)-1].ID,
},
Content: []byte(`{"units":[1,2]}`),
MediaType: "application/json",
Units: append([]source.SourceUnit(nil), doc.Units...),
Metadata: map[string]any{"ignored": "chunk metadata"},
}
return contracts.TypedExtractionRequest[dnd.SpellList]{
Source: doc,
Chunk: chunk,
Operation: contracts.ProducerOperation[dnd.SpellList]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, Cache: contracts.CacheBypass}, ReuseEligible: true, Assess: func(context.Context, dnd.SpellList) (contracts.CandidateAssessment, error) {
return contracts.CandidateAssessment{}, nil
}},
}
}
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: 1,
Kind: "transcript_segment",
Text: "Aria raises her hand and casts Cure Wounds.",
2026-07-17 05:19:57 +00:00
Ref: source.SourceRef{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 1},
Metadata: map[string]any{
"speaker": "Alice",
"start": json.Number("1.25"),
"end": json.Number("3.5"),
"ignored": "not rendered",
},
},
{
ID: 2,
Kind: "transcript_segment",
Text: "The fighter's wounds begin to close.",
2026-07-17 05:19:57 +00:00
Ref: source.SourceRef{SourceID: "session-alpha", StartUnitID: 2, EndUnitID: 2},
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)
}
2026-07-17 06:50:08 +00:00
func responseSourceRefs(startUnitID int, endUnitID int) []spellSourceRefResponse {
return []spellSourceRefResponse{
{
2026-07-22 14:38:35 +00:00
StartUnitID: startUnitID,
EndUnitID: endUnitID,
},
}
}
2026-07-17 06:50:08 +00:00
const spellTranscriptJSON = `{"id":"session-alpha","segments":[{"id":1,"text":"Aria raises her hand and casts Cure Wounds."}]}`
func spellSourceInput() contracts.LLMInputMaterial {
return contracts.NewLLMInputMaterial("source", "application/json", []byte(spellTranscriptJSON), "sha256:transcript", "file:///session-alpha.json")
}
func spellChunkInput(chunk *source.Chunk) contracts.LLMInputMaterial {
return contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:chunk", "file:///session-alpha.json")
}
func extractionRequest() contracts.TypedExtractionRequest[dnd.SpellList] {
2026-07-17 06:50:08 +00:00
req := promptExtractionRequest()
req.SourceInput = spellChunkInput(req.Chunk)
req.SessionID = "session-123"
req.LLMProfile = "profile-spells"
return req
}
func mismatchedSourceInputRequest(req contracts.TypedExtractionRequest[dnd.SpellList]) contracts.TypedExtractionRequest[dnd.SpellList] {
2026-07-17 06:50:08 +00:00
req.SourceInput = spellSourceInput()
return req
}
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
2026-07-17 06:50:08 +00:00
t.Helper()
extractor, err := New(newSpellEngine(t, client), Options{}, references...)
2026-07-17 06:50:08 +00:00
if err != nil {
t.Fatalf("New() error = %v, want nil", err)
}
return extractor
}
func spellCatalogReference(content string) contracts.ReferenceSet {
return contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
spellcatalog.SpellCatalogReferenceSlot: {
Items: []contracts.ReferenceItem{{
SlotName: spellcatalog.SpellCatalogReferenceSlot,
MediaType: "application/json",
Content: []byte(content),
}},
},
}}
}
func overlaySpellCatalogReference() contracts.ReferenceSet {
return spellCatalogReference(`{"schema_version":"notarius.dnd.spell-catalog-overlay.v1","catalogs":[{"id":"campaign.example","ruleset":"dnd-5e-2014","source":{"title":"Private campaign source","version":"1","url":"file:///private-source.json","license":"private"},"spells":[{"name":"Aegis of Emberfall","aliases":["Emberfall Aegis"]}]}]}`)
}
2026-07-17 06:50:08 +00:00
type fakeSpellsLLMClient struct {
response extractionResponse
content []byte
err error
requests []promptkit.GenerateRequest
outputs []string
2026-07-17 06:50:08 +00:00
}
func (client *fakeSpellsLLMClient) Generate(_ context.Context, req promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
client.requests = append(client.requests, req)
2026-07-17 06:50:08 +00:00
if client.err != nil {
return nil, client.err
2026-07-17 06:50:08 +00:00
}
if len(client.outputs) > 0 {
content := client.outputs[0]
if len(client.outputs) > 1 {
client.outputs = client.outputs[1:]
2026-07-22 14:38:35 +00:00
}
return &promptkit.GenerateResponse{Content: content}, nil
}
content := client.content
if len(content) == 0 {
2026-07-17 06:50:08 +00:00
var err error
content, err = json.Marshal(client.response)
if err != nil {
return nil, err
2026-07-17 06:50:08 +00:00
}
}
return &promptkit.GenerateResponse{Content: string(content)}, nil
2026-07-17 06:50:08 +00:00
}
func promptText(request promptkit.GenerateRequest) string {
var text []string
for _, message := range request.Prompt.Messages {
text = append(text, message.Content)
}
return strings.Join(text, "\n")
}
func newSpellEngine(t *testing.T, client promptkit.LLMClient) *promptkit.Engine {
t.Helper()
type settings struct {
PromptKit promptkit.Settings `yaml:"promptkit"`
2026-07-17 06:50:08 +00:00
}
definition := promptkit.Configure(configkit.Definition[settings]{}, func(value *settings) *promptkit.Settings { return &value.PromptKit })
filesystem := configkit.NewFilesystem("Notarius assets", assets.FS())
config, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("spell provider fixture", []byte(`promptkit:
profiles:
- id: profile-spells
endpoint: https://example.invalid/v1
model: spell-test-model
api_key_required: false
`))}, configkit.LayeredOptions[settings]{Definition: definition, Underlays: []configkit.Underlay{
promptkit.BundledOpenRouterUnderlay(),
{Inputs: []configkit.Input{{Source: filesystem.File("notarius.yml"), Imports: true, Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: "."}}}},
}})
if err != nil {
t.Fatal(err)
}
engine, err := promptkit.NewFromConfig(config, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client))
if err != nil {
t.Fatal(err)
}
return engine
}