2026-11-23 04:32:15 +00:00
|
|
|
package prompts_test
|
2026-05-13 18:49:06 +00:00
|
|
|
|
|
|
|
|
import (
|
2026-09-24 01:44:23 +00:00
|
|
|
"context"
|
2026-05-13 18:49:06 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-11-23 04:32:15 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
|
2026-11-10 16:13:53 +00:00
|
|
|
ck "go.maximumdirect.net/configkit"
|
|
|
|
|
pk "go.maximumdirect.net/promptkit"
|
2026-11-23 04:32:15 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-09-24 01:44:23 +00:00
|
|
|
)
|
2026-05-13 18:49:06 +00:00
|
|
|
|
2026-09-24 01:44:23 +00:00
|
|
|
func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) {
|
2026-11-23 04:32:15 +00:00
|
|
|
engine := fixtureEngine(t)
|
2026-09-24 01:44:23 +00:00
|
|
|
vars := map[string]string{
|
2026-11-23 04:32:15 +00:00
|
|
|
"HardeningPolicy": prompts.HardeningText(),
|
2026-09-24 01:44:23 +00:00
|
|
|
"TranscriptDescriptionBlock": "Transcript description:\nfixture-description\n\n",
|
|
|
|
|
"GlossaryJSON": "{\"name\":\"fixture-glossary\"}",
|
|
|
|
|
"SectionJSON": "{\"text\":\"fixture-section\"}",
|
|
|
|
|
"PayloadJSON": "[{\"text\":\"fixture-correction\"}]",
|
2026-05-13 18:49:06 +00:00
|
|
|
}
|
2026-11-23 04:32:15 +00:00
|
|
|
vars = prompts.WithSharedVariables(vars)
|
2026-11-11 17:05:52 +00:00
|
|
|
for _, tc := range []struct{ id, schema, payload string }{
|
2026-11-23 04:32:15 +00:00
|
|
|
{prompts.PromptIDModuleGlossaryProposal, "correction_set.v1.json", "fixture-section"},
|
|
|
|
|
{prompts.PromptIDModuleHomophonesProposal, "correction_set.v1.json", "fixture-section"},
|
|
|
|
|
{prompts.PromptIDModuleSpokenWordProposal, "correction_set.v1.json", "fixture-section"},
|
|
|
|
|
{prompts.PromptIDModuleGrammarProposal, "correction_set.v1.json", "fixture-section"},
|
|
|
|
|
{prompts.PromptIDValidatorSpokenFormPlausibility, "validator_decision_set.v1.json", "fixture-correction"},
|
|
|
|
|
{prompts.PromptIDValidatorMeaningReversalReview, "validator_decision_set.v1.json", "fixture-correction"},
|
|
|
|
|
{prompts.PromptIDValidatorEditorialReview, "validator_decision_set.v1.json", "fixture-correction"},
|
2026-09-24 01:44:23 +00:00
|
|
|
} {
|
|
|
|
|
t.Run(tc.id, func(t *testing.T) {
|
2026-11-10 16:13:53 +00:00
|
|
|
prepared, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: vars})
|
2026-09-24 01:44:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-10 16:13:53 +00:00
|
|
|
if prepared.PromptID != tc.id || prepared.OutputContract.SchemaPath != tc.schema || prepared.StructuredOutput == nil {
|
2026-09-24 01:44:23 +00:00
|
|
|
t.Fatalf("selection/schema: %+v", prepared)
|
|
|
|
|
}
|
2026-11-23 04:32:15 +00:00
|
|
|
inspection, err := engine.InspectPrompt(context.Background(), tc.id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if prepared.OutputContract.RepairAttempts != inspection.OutputContract.RepairAttempts {
|
|
|
|
|
t.Fatalf("prepared budget = %d, inspected budget = %d", prepared.OutputContract.RepairAttempts, inspection.OutputContract.RepairAttempts)
|
|
|
|
|
}
|
2026-09-24 01:44:23 +00:00
|
|
|
if len(prepared.Messages) != 2 {
|
|
|
|
|
t.Fatalf("message count: %d", len(prepared.Messages))
|
|
|
|
|
}
|
|
|
|
|
rendered := prepared.Messages[0].Content + prepared.Messages[1].Content
|
2026-11-11 17:05:52 +00:00
|
|
|
for _, fragment := range []string{"fixture-description", tc.payload} {
|
2026-09-24 01:44:23 +00:00
|
|
|
if !strings.Contains(rendered, fragment) {
|
|
|
|
|
t.Errorf("missing %q", fragment)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-10 16:13:53 +00:00
|
|
|
if strings.HasPrefix(tc.id, "audita/modules.") && !strings.Contains(rendered, "fixture-glossary") {
|
2026-09-24 01:44:23 +00:00
|
|
|
t.Error("missing glossary context")
|
|
|
|
|
}
|
2026-11-23 04:32:15 +00:00
|
|
|
missing := map[string]string{"HardeningPolicy": prompts.HardeningText()}
|
2026-11-10 16:13:53 +00:00
|
|
|
if _, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: missing}); err == nil {
|
2026-09-24 01:44:23 +00:00
|
|
|
t.Error("missing variables accepted")
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-05-13 18:49:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 15:19:59 +00:00
|
|
|
func TestRegistryMetadataIdentifiesRequiredPrompts(t *testing.T) {
|
|
|
|
|
required := []string{
|
|
|
|
|
"audita/modules.glossary.proposal", "audita/modules.glossary.repair",
|
|
|
|
|
"audita/modules.grammar.proposal", "audita/modules.grammar.repair",
|
|
|
|
|
"audita/modules.homophones.proposal", "audita/modules.homophones.repair",
|
|
|
|
|
"audita/modules.spoken_word.proposal", "audita/modules.spoken_word.repair",
|
|
|
|
|
"audita/validators.editorial_review", "audita/validators.meaning_reversal_review",
|
|
|
|
|
"audita/validators.spoken_form_plausibility",
|
2026-05-13 18:49:06 +00:00
|
|
|
}
|
2026-11-16 15:19:59 +00:00
|
|
|
for _, id := range required {
|
2026-11-23 04:32:15 +00:00
|
|
|
metadata, ok := prompts.LookupMetadata(id)
|
|
|
|
|
if !ok || metadata.PromptID != id || metadata.PromptSource != prompts.SourceBuiltin ||
|
|
|
|
|
metadata.EmbeddedPath != prompts.AssetRoot+"/"+strings.TrimPrefix(id, "audita/")+".yaml" {
|
2026-11-16 15:19:59 +00:00
|
|
|
t.Fatalf("required prompt metadata for %q: %+v", id, metadata)
|
2026-05-23 17:48:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-23 04:32:15 +00:00
|
|
|
all := prompts.RegisteredMetadata()
|
2026-11-16 15:19:59 +00:00
|
|
|
all[0].PromptID = "changed"
|
2026-11-23 04:32:15 +00:00
|
|
|
if again := prompts.RegisteredMetadata(); again[0].PromptID == "changed" {
|
2026-11-16 15:19:59 +00:00
|
|
|
t.Fatal("metadata enumeration shares mutable state")
|
|
|
|
|
}
|
2026-05-23 17:48:37 +00:00
|
|
|
}
|
2026-11-11 04:13:55 +00:00
|
|
|
|
|
|
|
|
func TestRepairDefinitionsPrepareThroughPromptKit(t *testing.T) {
|
2026-11-23 04:32:15 +00:00
|
|
|
engine := fixtureEngine(t)
|
|
|
|
|
for _, id := range []string{prompts.PromptIDModuleGlossaryRepair, prompts.PromptIDModuleHomophonesRepair, prompts.PromptIDModuleSpokenWordRepair, prompts.PromptIDModuleGrammarRepair} {
|
2026-11-11 04:13:55 +00:00
|
|
|
t.Run(id, func(t *testing.T) {
|
2026-11-23 04:32:15 +00:00
|
|
|
prepared, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: id, ProfileID: "fixture", Vars: prompts.WithSharedVariables(map[string]string{"RepairRequestJSON": `{"proposal_ref":2,"correction":{"id":1,"original_text":" ","corrected_text":""},"findings":[{"validator":"shape","reason":"example"}]}`})})
|
2026-11-11 04:13:55 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if prepared.OutputContract.SchemaPath != "correction_repair.v1.json" || prepared.StructuredOutput == nil || len(prepared.Messages) != 1 || prepared.Messages[0].Role != pk.RoleUser {
|
|
|
|
|
t.Fatalf("prepared = %+v", prepared)
|
|
|
|
|
}
|
2026-11-11 17:05:52 +00:00
|
|
|
for _, fragment := range []string{`{"proposal_ref":2,"correction":{"id":1,"original_text":" ","corrected_text":""},"findings":[{"validator":"shape","reason":"example"}]}`} {
|
2026-11-11 04:13:55 +00:00
|
|
|
if !strings.Contains(prepared.Messages[0].Content, fragment) {
|
|
|
|
|
t.Errorf("missing %q", fragment)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-23 04:32:15 +00:00
|
|
|
|
|
|
|
|
func fixtureEngine(t *testing.T) *pk.Engine {
|
|
|
|
|
t.Helper()
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
main := filepath.Join(dir, "config.yml")
|
|
|
|
|
if err := os.WriteFile(main, []byte("version: 3\npromptkit:\n profiles:\n - id: fixture\n endpoint: https://example.invalid/v1\n model: fixture-model\n"), 0600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
loaded, _, err := appconfig.Load(context.Background(), ck.Input{Source: ck.File(main)}, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
engine, err := pk.NewFromConfig(loaded, pk.RuntimeConfig{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return engine
|
|
|
|
|
}
|