- 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.
117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package contract
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
renamercmd "github.com/rogeecn/renamer/cmd"
|
|
"github.com/rogeecn/renamer/internal/ai/genkit"
|
|
"github.com/rogeecn/renamer/internal/ai/prompt"
|
|
)
|
|
|
|
type captureWorkflow struct {
|
|
request genkit.Request
|
|
}
|
|
|
|
func (c *captureWorkflow) Run(ctx context.Context, req genkit.Request) (genkit.Result, error) {
|
|
c.request = req
|
|
return genkit.Result{
|
|
Response: prompt.RenameResponse{
|
|
Items: []prompt.RenameItem{
|
|
{
|
|
Original: "alpha.txt",
|
|
Proposed: "proj_001_sample_file.txt",
|
|
Sequence: 1,
|
|
},
|
|
},
|
|
Model: "test-model",
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func TestAICommandAppliesNamingPoliciesToPrompt(t *testing.T) {
|
|
genkit.ResetWorkflowFactory()
|
|
stub := &captureWorkflow{}
|
|
genkit.OverrideWorkflowFactory(func(ctx context.Context, opts genkit.Options) (genkit.WorkflowRunner, error) {
|
|
return stub, nil
|
|
})
|
|
t.Cleanup(genkit.ResetWorkflowFactory)
|
|
|
|
rootDir := t.TempDir()
|
|
createPolicyTestFile(t, filepath.Join(rootDir, "alpha.txt"))
|
|
|
|
rootCmd := renamercmd.NewRootCommand()
|
|
var stdout, stderr bytes.Buffer
|
|
rootCmd.SetOut(&stdout)
|
|
rootCmd.SetErr(&stderr)
|
|
rootCmd.SetArgs([]string{
|
|
"ai",
|
|
"--path", rootDir,
|
|
"--dry-run",
|
|
"--naming-casing", "snake",
|
|
"--naming-prefix", "proj",
|
|
"--naming-allow-spaces",
|
|
"--naming-keep-order",
|
|
"--banned", "alpha",
|
|
})
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
if stdout.Len() > 0 {
|
|
t.Logf("stdout: %s", stdout.String())
|
|
}
|
|
if stderr.Len() > 0 {
|
|
t.Logf("stderr: %s", stderr.String())
|
|
}
|
|
t.Fatalf("command execute: %v", err)
|
|
}
|
|
|
|
req := stub.request
|
|
policies := req.Payload.Policies
|
|
if policies.Prefix != "proj" {
|
|
t.Fatalf("expected prefix proj, got %q", policies.Prefix)
|
|
}
|
|
if policies.Casing != "snake" {
|
|
t.Fatalf("expected casing snake, got %q", policies.Casing)
|
|
}
|
|
if !policies.AllowSpaces {
|
|
t.Fatalf("expected allow spaces flag to propagate")
|
|
}
|
|
if !policies.KeepOriginalOrder {
|
|
t.Fatalf("expected keep original order flag to propagate")
|
|
}
|
|
if len(policies.ForbiddenTokens) != 1 || policies.ForbiddenTokens[0] != "alpha" {
|
|
t.Fatalf("expected forbidden tokens to capture user list, got %#v", policies.ForbiddenTokens)
|
|
}
|
|
|
|
banned := req.Payload.BannedTerms
|
|
containsDefault := false
|
|
containsUser := false
|
|
for _, term := range banned {
|
|
switch term {
|
|
case "alpha":
|
|
containsUser = true
|
|
case "clickbait":
|
|
containsDefault = true
|
|
}
|
|
}
|
|
if !containsUser {
|
|
t.Fatalf("expected banned terms to include user-provided token")
|
|
}
|
|
if !containsDefault {
|
|
t.Fatalf("expected banned terms to retain default tokens")
|
|
}
|
|
}
|
|
|
|
func createPolicyTestFile(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)
|
|
}
|
|
}
|