Files
renamer/tests/integration/ai_policy_validation_test.go
Rogee 3867736858 feat: implement AI-assisted rename prompting feature
- Added data model for AI-assisted renaming including structures for prompts, responses, and policies.
- Created implementation plan detailing the integration of Google Genkit into the CLI for renaming tasks.
- Developed quickstart guide for setting up and using the new AI rename functionality.
- Documented research decisions regarding Genkit orchestration and prompt composition.
- Established tasks for phased implementation, including setup, foundational work, and user stories.
- Implemented contract tests to ensure AI rename policies and ledger metadata are correctly applied.
- Developed integration tests for validating AI rename flows, including preview, apply, and undo functionalities.
- Added tooling to pin Genkit dependency for consistent builds.
2025-11-03 18:08:14 +08:00

85 lines
2.1 KiB
Go

package integration
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"
renamercmd "github.com/rogeecn/renamer/cmd"
"github.com/rogeecn/renamer/internal/ai/genkit"
"github.com/rogeecn/renamer/internal/ai/prompt"
)
type violatingWorkflow struct{}
func (violatingWorkflow) Run(ctx context.Context, req genkit.Request) (genkit.Result, error) {
return genkit.Result{
Response: prompt.RenameResponse{
Items: []prompt.RenameItem{
{
Original: "video.mov",
Proposed: "001_clickbait-offer.mov",
Sequence: 1,
},
},
Warnings: []string{"model returned promotional phrasing"},
},
}, nil
}
func TestAIPolicyValidationFailsWithActionableMessage(t *testing.T) {
genkit.OverrideWorkflowFactory(func(ctx context.Context, opts genkit.Options) (genkit.WorkflowRunner, error) {
return violatingWorkflow{}, nil
})
t.Cleanup(genkit.ResetWorkflowFactory)
rootDir := t.TempDir()
createAIPolicyFixture(t, filepath.Join(rootDir, "video.mov"))
rootCmd := renamercmd.NewRootCommand()
var stdout, stderr bytes.Buffer
rootCmd.SetOut(&stdout)
rootCmd.SetErr(&stderr)
rootCmd.SetArgs([]string{
"ai",
"--path", rootDir,
"--dry-run",
"--naming-casing", "kebab",
"--naming-prefix", "proj",
"--banned", "offer",
})
err := rootCmd.Execute()
if err == nil {
t.Fatalf("expected policy violation error")
}
lines := stderr.String()
if !strings.Contains(lines, "Policy violation (prefix)") {
t.Fatalf("expected prefix violation message in stderr, got: %s", lines)
}
if !strings.Contains(lines, "Policy violation (banned)") {
t.Fatalf("expected banned token message in stderr, got: %s", lines)
}
if !strings.Contains(err.Error(), "policy violations") {
t.Fatalf("expected error to mention policy violations, got: %v", err)
}
if stdout.Len() != 0 {
t.Logf("stdout: %s", stdout.String())
}
}
func createAIPolicyFixture(t *testing.T, path string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", path, err)
}
if err := os.WriteFile(path, []byte("demo"), 0o644); err != nil {
t.Fatalf("write file %s: %v", path, err)
}
}