Expose native session planning and artifact catalogs

This commit is contained in:
2026-11-20 05:15:43 +00:00
parent 0238f82847
commit 97cced5d88
15 changed files with 493 additions and 462 deletions

View File

@@ -3,201 +3,105 @@ package app
import (
"bytes"
"context"
"errors"
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
)
const artifactCatalogHeader = "key\tsource\tdefinitions\tenabled\toutput\tdependencies\tfamily\tcharacter\tfamily_source\tparty_source\n"
func catalogReportForTest(t *testing.T, args []string) *artifactCatalogReport {
t.Helper()
output := nativeInspectionForTest(t, append([]string{"artifacts", "list", "--format", "json"}, args...))
var report artifactCatalogReport
if err := json.Unmarshal([]byte(output), &report); err != nil {
t.Fatal(err)
}
return &report
}
func TestConfigArtifactsReportsOrdinaryCatalogWithoutCampaign(t *testing.T) {
func TestArtifactCatalogCommandWithoutCampaignPreservesDependencies(t *testing.T) {
dir := t.TempDir()
pipelinePath := filepath.Join(dir, "pipeline.yml")
mustWriteTestFile(t, pipelinePath, `workspace:
pipeline := filepath.Join(dir, "pipeline.yml")
mustWriteTestFile(t, pipeline, `workspace:
root: `+filepath.Join(dir, "workspace")+`
whisperx:
transcribe_url: https://transcription.example.com/transcribe
whisperx: {transcribe_url: https://transcription.example.test/transcribe}
scriptorium:
binary: scriptorium
artifacts:
zeta:
enabled: false
output_path: artifacts/zeta.md
zeta: {enabled: false, output_path: artifacts/zeta.md}
alpha:
enabled: false
output_path: artifacts/alpha.md
inputs:
upstream: {source: narratio.artifact.zeta, required: true}
`)
var out bytes.Buffer
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath}, &out); err != nil {
t.Fatalf("ConfigArtifacts() error = %v", err)
report := catalogReportForTest(t, []string{"--config", pipeline})
found := map[string]artifactDefinitionReport{}
for _, entry := range report.Artifacts {
found[entry.Source] = entry
}
lines := strings.Split(strings.TrimSuffix(out.String(), "\n"), "\n")
if len(lines) != 3 || !strings.HasPrefix(lines[1], "alpha\t") || !strings.HasPrefix(lines[2], "zeta\t") {
t.Fatalf("catalog order/output = %q", out.String())
alpha := found["narratio.artifact.alpha"]
if alpha.Producer != "analyze" || alpha.Enabled == nil || *alpha.Enabled || !reflect.DeepEqual(alpha.Dependencies, []string{"zeta"}) || !reflect.DeepEqual(alpha.DefinitionSources, []string{pipeline}) {
t.Fatalf("alpha=%#v", alpha)
}
alpha := strings.Split(lines[1], "\t")
if len(alpha) != 10 {
t.Fatalf("alpha fields = %#v", alpha)
if found["narratio.transcript.base"].Producer != "merge" {
t.Fatal("catalog omitted built-in producer")
}
if alpha[1] != "narratio.artifact.alpha" || alpha[2] != pipelinePath || alpha[3] != "false" || alpha[4] != "artifacts/alpha.md" || alpha[5] != "zeta" {
t.Fatalf("alpha record = %#v", alpha)
if _, err := os.Stat(filepath.Join(dir, "workspace")); !os.IsNotExist(err) {
t.Fatalf("listing created workspace: %v", err)
}
}
func TestConfigArtifactsEmptyCatalogWritesOnlyHeader(t *testing.T) {
dir := t.TempDir()
pipelinePath := filepath.Join(dir, "pipeline.yml")
mustWriteTestFile(t, pipelinePath, "workspace:\n root: "+filepath.Join(dir, "workspace")+"\nwhisperx:\n transcribe_url: https://transcription.example.com/transcribe\n")
var out bytes.Buffer
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath}, &out); err != nil {
func TestArtifactCatalogCommandUsesExplicitRosterAndNativeConfiguration(t *testing.T) {
pipeline, _ := writeInspectionFamilyConfig(t)
overlay := filepath.Join(filepath.Dir(pipeline), "overlays", "testing.yml")
mustWriteTestFile(t, overlay, "scriptorium:\n artifact_families:\n character_note:\n prompt_id: dnd.character_note.testing\n")
roster := filepath.Join(t.TempDir(), "selected.yml")
mustWriteTestFile(t, roster, "schema_version: narratio.party.v1\ncharacters:\n synthetic_character:\n player: {name: Synthetic Player}\n character: {name: Synthetic Character, classes: [{name: ranger}]}\n")
selected := "testing"
loaded, err := config.LoadPipelineWithOptions(pipeline, config.PipelineLoadOptions{Overlay: &selected})
if err != nil {
t.Fatal(err)
}
if out.String() != artifactCatalogHeader {
t.Fatalf("empty catalog = %q, want header only", out.String())
}
}
func TestConfigArtifactsReportsGeneratedFamilyProvenance(t *testing.T) {
pipelinePath, campaignPath := writeInspectionFamilyConfig(t)
var out bytes.Buffer
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath, "--campaign-file", campaignPath}, &out); err != nil {
var before, after bytes.Buffer
if err := loaded.ConfigKitResult().WriteEffective(&before); err != nil {
t.Fatal(err)
}
records := catalogRecordsByKey(t, out.String())
meta := records["character_meta_arannis"]
note := records["character_note_arannis"]
partyPath := filepath.Join(filepath.Dir(campaignPath), "party.yml")
if len(meta) != 10 || meta[6] != "character_meta" || meta[7] != "arannis" || meta[8] != pipelinePath || meta[9] != partyPath {
t.Fatalf("generated meta record = %#v", meta)
}
if len(note) != 10 || note[5] != "character_meta_arannis" || note[6] != "character_note" {
t.Fatalf("generated note record = %#v", note)
}
}
func TestConfigArtifactsReportsOverlayFamilyContributors(t *testing.T) {
pipelinePath, campaignPath := writeInspectionFamilyConfig(t)
overlayPath := filepath.Join(filepath.Dir(pipelinePath), "overlays", "testing.yml")
mustWriteTestFile(t, overlayPath, `scriptorium:
artifact_families:
character_note:
prompt_id: dnd.character_note.testing
`)
args := []string{"--config", pipelinePath, "--campaign-file", campaignPath, "--overlay", "testing"}
var catalog bytes.Buffer
if err := ConfigArtifacts(context.Background(), args, &catalog); err != nil {
app, err := newApplication(commandDependencies{loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) { return loaded, nil }}, executePlan)
if err != nil {
t.Fatal(err)
}
records := catalogRecordsByKey(t, catalog.String())
if got := strings.Split(records["character_note_arannis"][2], ","); !reflect.DeepEqual(got, []string{overlayPath, pipelinePath}) {
t.Fatalf("generated family definitions = %#v", got)
var out, diagnostic bytes.Buffer
outcome := app.Execute(context.Background(), []string{"artifacts", "list", "--config", pipeline, "--overlay", selected, "--party-file", roster, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
if outcome.Status != 0 {
t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String())
}
}
func TestConfigArtifactsReportsBaseAndOverlayContributors(t *testing.T) {
dir := t.TempDir()
pipelinePath := filepath.Join(dir, "pipeline.yml")
overlayPath := filepath.Join(dir, "overlays", "testing.yml")
mustWriteTestFile(t, pipelinePath, `workspace:
root: /srv/narratio
whisperx:
transcribe_url: https://transcription.example.com/transcribe
scriptorium:
binary: scriptorium
artifacts:
notes:
enabled: false
output_path: artifacts/notes.md
`)
mustWriteTestFile(t, overlayPath, `scriptorium:
artifacts:
notes:
enabled: true
prompt_id: notes
overlay_only:
enabled: false
output_path: artifacts/overlay-only.md
`)
var out bytes.Buffer
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath, "--overlay", "testing"}, &out); err != nil {
var report artifactCatalogReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
t.Fatal(err)
}
records := catalogRecordsByKey(t, out.String())
if got := strings.Split(records["notes"][2], ","); !reflect.DeepEqual(got, []string{overlayPath, pipelinePath}) {
t.Fatalf("notes definitions = %#v", got)
}
if got := records["overlay_only"][2]; got != overlayPath {
t.Fatalf("overlay-only definition = %q, want %q", got, overlayPath)
}
}
func TestConfigArtifactsIdentityDoesNotDependOnDefinitionFilename(t *testing.T) {
inlineDir := t.TempDir()
inlinePath := filepath.Join(inlineDir, "pipeline.yml")
mustWriteTestFile(t, inlinePath, artifactIdentityPipeline("", "recap"))
importDir := t.TempDir()
importPath := filepath.Join(importDir, "pipeline.yml")
mustWriteTestFile(t, importPath, artifactIdentityPipeline("artifacts.yml", ""))
mustWriteTestFile(t, filepath.Join(importDir, "artifacts.yml"), "scriptorium:\n artifacts:\n recap:\n enabled: false\n output_path: artifacts/recap.md\n")
for _, path := range []string{inlinePath, importPath} {
var out bytes.Buffer
if err := ConfigArtifacts(context.Background(), []string{"--config", path}, &out); err != nil {
t.Fatal(err)
}
if got := catalogRecordsByKey(t, out.String())["recap"][1]; got != "narratio.artifact.recap" {
t.Fatalf("source identity for %s = %q", path, got)
found := false
for _, entry := range report.Artifacts {
if entry.Family == "character_note" {
if entry.CharacterID != "synthetic_character" || entry.PartySource != roster || entry.Key != "character_note_synthetic_character" {
t.Fatalf("member=%#v", entry)
}
if !reflect.DeepEqual(entry.DefinitionSources, []string{overlay, pipeline}) {
t.Fatalf("definition sources=%v", entry.DefinitionSources)
}
found = true
}
}
}
func TestConfigArtifactsPropagatesWriterErrorsAndEscapesFields(t *testing.T) {
dir := t.TempDir()
pipelinePath := filepath.Join(dir, "pipeline.yml")
mustWriteTestFile(t, pipelinePath, "workspace:\n root: /srv/narratio\nwhisperx:\n transcribe_url: https://transcription.example.com/transcribe\n")
want := errors.New("write rejected")
err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath}, rejectingCatalogWriter{err: want})
if !errors.Is(err, want) {
t.Fatalf("ConfigArtifacts() error = %v, want writer error", err)
if !found {
t.Fatal("catalog omitted selected family member")
}
if got := escapeCatalogField("a\\b\tc\nd\r"); got != `a\\b\tc\nd\r` {
t.Fatalf("escaped field = %q", got)
if err := loaded.ConfigKitResult().WriteEffective(&after); err != nil {
t.Fatal(err)
}
if !bytes.Equal(before.Bytes(), after.Bytes()) {
t.Fatal("catalog derivation changed native configuration")
}
}
func catalogRecordsByKey(t *testing.T, output string) map[string][]string {
t.Helper()
lines := strings.Split(strings.TrimSuffix(output, "\n"), "\n")
if len(lines) == 0 || lines[0]+"\n" != artifactCatalogHeader {
t.Fatalf("catalog header = %q", output)
}
records := make(map[string][]string, len(lines)-1)
for _, line := range lines[1:] {
fields := strings.Split(line, "\t")
records[fields[0]] = fields
}
return records
}
func artifactIdentityPipeline(importPath, inlineKey string) string {
document := "workspace:\n root: /srv/narratio\nwhisperx:\n transcribe_url: https://transcription.example.com/transcribe\nscriptorium:\n binary: scriptorium\n"
if importPath != "" {
document = "config:\n imports:\n - path: " + importPath + "\n" + document
}
if inlineKey != "" {
document += " artifacts:\n " + inlineKey + ":\n enabled: false\n output_path: artifacts/recap.md\n"
}
return document
}
type rejectingCatalogWriter struct{ err error }
func (w rejectingCatalogWriter) Write([]byte) (int, error) { return 0, w.err }