228 lines
8.9 KiB
Go
228 lines
8.9 KiB
Go
package integration_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/evidencecontext"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
locationoccurrencecodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/locationoccurrences"
|
|
locationcodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/locationregistry"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func TestLocationRegistryHandoffProducesOccurrencesAndEvidence(t *testing.T) {
|
|
registries := productionNPCRegistries(t)
|
|
resolved := resolveLocationPipeline(t, registries)
|
|
client := &locationHandoffLLMClient{}
|
|
output, err := runPreparedPipeline(t, registries, resolved, client, pipeline.RunInput{RawInput: readNPCFixture(t)})
|
|
if err != nil {
|
|
t.Fatalf("Run() error = %v", err)
|
|
}
|
|
if len(output.OutputFiles) != 3 {
|
|
t.Fatalf("files=%#v, want two lanes and evidence", output.OutputFiles)
|
|
}
|
|
|
|
registryOutput := publishedLane(t, output, "locations")
|
|
registry, err := locationcodec.New().Decode(registryOutput.Bytes)
|
|
if err != nil {
|
|
t.Fatalf("Decode(registry) error = %v", err)
|
|
}
|
|
if len(registry.Locations) != 2 || registry.Locations[0].Name != "Moon Gate" || registry.Locations[1].Name != "Moon Gate" || registry.Locations[0].ID == registry.Locations[1].ID {
|
|
t.Fatalf("registry = %#v, want same-name locations distinguished by source-derived IDs", registry)
|
|
}
|
|
|
|
occurrenceOutput := publishedLane(t, output, "occurrences")
|
|
occurrences, err := locationoccurrencecodec.New().Decode(occurrenceOutput.Bytes)
|
|
if err != nil {
|
|
t.Fatalf("Decode(occurrences) error = %v", err)
|
|
}
|
|
if len(occurrences.Occurrences) != 2 || occurrences.Occurrences[0].LocationID == occurrences.Occurrences[1].LocationID || occurrences.Occurrences[0].LocationID != registry.Locations[0].ID || occurrences.Occurrences[1].LocationID != registry.Locations[1].ID {
|
|
t.Fatalf("occurrences = %#v, want exact registry ID grounding", occurrences)
|
|
}
|
|
for _, occurrence := range occurrences.Occurrences {
|
|
if occurrence.SourceRefs[0].SourceID != "npc-session" {
|
|
t.Fatalf("occurrence evidence = %#v, want current source evidence only", occurrence.SourceRefs)
|
|
}
|
|
}
|
|
|
|
request := client.requestFor(t, "occurrences")
|
|
|
|
registryInput, err := nativeJSONInput(request, "locations")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(registryInput), "source_refs") || strings.Contains(string(registryInput), registry.Locations[0].ID) || strings.Contains(string(registryInput), registry.Locations[1].ID) || !strings.Contains(string(registryInput), "registry_refs") {
|
|
t.Fatalf("registry input=%s, want contextual source-free selectors", registryInput)
|
|
}
|
|
|
|
contextArtifact := outputFileContent(t, output.OutputFiles, "evidence-context.json")
|
|
evidence, err := evidencecontext.New().Decode(contextArtifact)
|
|
if err != nil {
|
|
t.Fatalf("Decode(evidence context) error = %v", err)
|
|
}
|
|
if actual := locationEvidenceUnitIDs(evidence); !reflect.DeepEqual(actual, []int{1, 2, 3, 4, 5}) {
|
|
t.Fatalf("evidence context = %#v, want deduplicated registry and occurrence source-unit evidence", evidence)
|
|
}
|
|
}
|
|
|
|
func locationEvidenceUnitIDs(document evidencecontext.Document) []int {
|
|
ids := make([]int, len(document))
|
|
for index, unit := range document {
|
|
ids[index] = unit.ID
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func TestLocationOccurrenceConsumerDoesNotRunAfterRejectedRegistry(t *testing.T) {
|
|
registries := productionNPCRegistries(t)
|
|
client := &locationHandoffLLMClient{rejectRegistry: true}
|
|
_, err := runPreparedPipeline(t, registries, resolveLocationPipeline(t, registries), client, pipeline.RunInput{RawInput: readNPCFixture(t)})
|
|
if err == nil {
|
|
t.Fatal("Run() error = nil, want missing accepted location registry")
|
|
}
|
|
if client.requestCount("occurrences") != 0 {
|
|
t.Fatalf("occurrence requests = %d, want none after rejected registry", client.requestCount("occurrences"))
|
|
}
|
|
}
|
|
|
|
func TestLocationOccurrenceConfigurationRequiresEarlierRegistry(t *testing.T) {
|
|
registries := productionNPCRegistries(t)
|
|
cfg := loadIntegrationConfigText(t, strings.Replace(locationPipelineConfig, "step: identify-locations", "step: track-location-occurrences", 1))
|
|
_, err := cfg.Resolve(config.ResolveInput{PipelineID: "dnd-locations-fixture", Catalog: moduleCatalog(registries)})
|
|
if err == nil || !strings.Contains(err.Error(), "earlier step") {
|
|
t.Fatalf("Resolve() error = %v, want an earlier generated registry requirement", err)
|
|
}
|
|
}
|
|
|
|
func resolveLocationPipeline(t *testing.T, registries pipeline.Registries) pipeline.ResolvedPipeline {
|
|
t.Helper()
|
|
cfg := loadIntegrationConfigText(t, locationPipelineConfig)
|
|
effective, err := cfg.Resolve(config.ResolveInput{PipelineID: "dnd-locations-fixture", Catalog: moduleCatalog(registries)})
|
|
if err != nil {
|
|
t.Fatalf("Resolve() error = %v", err)
|
|
}
|
|
resolved, warnings, err := pipeline.MaterializeReferences(effective.ResolvedPipeline, moduleCatalog(registries), pipeline.ReferenceMaterializationOptions{})
|
|
if err != nil || len(warnings) != 0 {
|
|
t.Fatalf("MaterializeReferences() error = %v warnings = %#v", err, warnings)
|
|
}
|
|
return resolved
|
|
}
|
|
|
|
const locationPipelineConfig = `version: 6
|
|
pipelines:
|
|
dnd-locations-fixture:
|
|
input:
|
|
module: seriatim
|
|
output:
|
|
module: json
|
|
options:
|
|
evidence_context:
|
|
enabled: true
|
|
lanes: [locations, occurrences]
|
|
steps:
|
|
- id: identify-locations
|
|
artifacts:
|
|
locations:
|
|
extract: {module: dnd/location-registry}
|
|
merge: {module: appendorder}
|
|
normalize: {module: dnd/location-registry}
|
|
- id: track-location-occurrences
|
|
references:
|
|
location_registry:
|
|
artifact:
|
|
step: identify-locations
|
|
lane: locations
|
|
artifacts:
|
|
occurrences:
|
|
extract: {module: dnd/location-occurrences}
|
|
merge: {module: appendorder}
|
|
normalize: {module: dnd/location-occurrences}
|
|
`
|
|
|
|
type locationHandoffLLMClient struct {
|
|
mu sync.Mutex
|
|
requests []promptkit.GenerateRequest
|
|
rejectRegistry bool
|
|
}
|
|
|
|
func (client *locationHandoffLLMClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
client.mu.Lock()
|
|
client.requests = append(client.requests, request)
|
|
client.mu.Unlock()
|
|
var payload any
|
|
switch {
|
|
case nativeSchemaHasProperty(request, "locations"):
|
|
name := "Moon Gate"
|
|
if client.rejectRegistry {
|
|
name = ""
|
|
}
|
|
payload = map[string]any{"locations": []any{map[string]any{"name": name, "source_refs": []any{map[string]int{"start_unit_id": 1, "end_unit_id": 1}}}, map[string]any{"name": name, "source_refs": []any{map[string]int{"start_unit_id": 3, "end_unit_id": 3}}}}}
|
|
case nativeSchemaHasProperty(request, "duplicate_groups"):
|
|
payload = map[string]any{"duplicate_groups": []any{}}
|
|
case nativeSchemaHasProperty(request, "occurrences"):
|
|
var projection struct {
|
|
Locations []struct {
|
|
Name string `json:"name"`
|
|
RegistryRefs []struct {
|
|
StartUnitID int `json:"start_unit_id"`
|
|
EndUnitID int `json:"end_unit_id"`
|
|
} `json:"registry_refs"`
|
|
} `json:"locations"`
|
|
}
|
|
content, err := nativeJSONInput(request, "locations")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := json.Unmarshal(content, &projection); err != nil {
|
|
return nil, fmt.Errorf("decode location registry: %w", err)
|
|
}
|
|
if len(projection.Locations) != 2 {
|
|
return nil, fmt.Errorf("generated location registry has %d locations, want 2", len(projection.Locations))
|
|
}
|
|
payload = map[string]any{"occurrences": []any{map[string]any{"name": projection.Locations[0].Name, "registry_refs": projection.Locations[0].RegistryRefs, "kind": "visited", "source_refs": []any{map[string]int{"start_unit_id": 1, "end_unit_id": 1}}}, map[string]any{"name": projection.Locations[1].Name, "registry_refs": projection.Locations[1].RegistryRefs, "kind": "mentioned", "source_refs": []any{map[string]int{"start_unit_id": 3, "end_unit_id": 3}}}}}
|
|
default:
|
|
return nil, fmt.Errorf("unexpected native location output contract")
|
|
}
|
|
content, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &promptkit.GenerateResponse{Content: string(content)}, nil
|
|
}
|
|
|
|
func (client *locationHandoffLLMClient) requestFor(t *testing.T, property string) promptkit.GenerateRequest {
|
|
t.Helper()
|
|
client.mu.Lock()
|
|
defer client.mu.Unlock()
|
|
for _, request := range client.requests {
|
|
if nativeSchemaHasProperty(request, property) {
|
|
return request
|
|
}
|
|
}
|
|
t.Fatalf("requests = %#v, missing %q", client.requests, property)
|
|
return promptkit.GenerateRequest{}
|
|
}
|
|
func (client *locationHandoffLLMClient) requestCount(property string) int {
|
|
client.mu.Lock()
|
|
defer client.mu.Unlock()
|
|
count := 0
|
|
for _, request := range client.requests {
|
|
if nativeSchemaHasProperty(request, property) {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
var _ promptkit.LLMClient = (*locationHandoffLLMClient)(nil)
|