feat: update AI command to streamline token management and remove unnecessary flags

This commit is contained in:
2025-11-04 18:17:56 +08:00
parent 3867736858
commit ad8ca2b1f7
9 changed files with 265 additions and 327 deletions

View File

@@ -32,7 +32,7 @@ func (c *captureWorkflow) Run(ctx context.Context, req genkit.Request) (genkit.R
}, nil
}
func TestAICommandAppliesNamingPoliciesToPrompt(t *testing.T) {
func TestAICommandUsesDefaultPoliciesInPrompt(t *testing.T) {
genkit.ResetWorkflowFactory()
stub := &captureWorkflow{}
genkit.OverrideWorkflowFactory(func(ctx context.Context, opts genkit.Options) (genkit.WorkflowRunner, error) {
@@ -51,11 +51,6 @@ func TestAICommandAppliesNamingPoliciesToPrompt(t *testing.T) {
"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 {
@@ -70,38 +65,29 @@ func TestAICommandAppliesNamingPoliciesToPrompt(t *testing.T) {
req := stub.request
policies := req.Payload.Policies
if policies.Prefix != "proj" {
t.Fatalf("expected prefix proj, got %q", policies.Prefix)
if policies.Prefix != "" {
t.Fatalf("expected empty prefix, got %q", policies.Prefix)
}
if policies.Casing != "snake" {
t.Fatalf("expected casing snake, got %q", policies.Casing)
if policies.Casing != "kebab" {
t.Fatalf("expected default casing kebab, got %q", policies.Casing)
}
if !policies.AllowSpaces {
t.Fatalf("expected allow spaces flag to propagate")
if policies.AllowSpaces {
t.Fatalf("expected allow spaces default false")
}
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)
if policies.KeepOriginalOrder {
t.Fatalf("expected keep original order default false")
}
banned := req.Payload.BannedTerms
containsDefault := false
containsUser := false
for _, term := range banned {
switch term {
case "alpha":
containsUser = true
case "clickbait":
if term == "clickbait" {
containsDefault = true
break
}
}
if !containsUser {
t.Fatalf("expected banned terms to include user-provided token")
}
if !containsDefault {
t.Fatalf("expected banned terms to retain default tokens")
t.Fatalf("expected default banned terms propagated, got %#v", banned)
}
}

View File

@@ -44,7 +44,7 @@ func TestAIApplyAndUndoFlow(t *testing.T) {
writeFile(t, filepath.Join(root, "draft_one.txt"))
writeFile(t, filepath.Join(root, "draft_two.txt"))
planPath := filepath.Join(root, "ai-plan.json")
planPath := filepath.Join(root, "renamer.plan.json")
preview := renamercmd.NewRootCommand()
var previewOut, previewErr bytes.Buffer
@@ -54,7 +54,6 @@ func TestAIApplyAndUndoFlow(t *testing.T) {
"ai",
"--path", root,
"--dry-run",
"--export-plan", planPath,
})
if err := preview.Execute(); err != nil {
@@ -124,7 +123,6 @@ func TestAIApplyAndUndoFlow(t *testing.T) {
"ai",
"--path", root,
"--dry-run",
"--import-plan", planPath,
})
if err := previewEdited.Execute(); err != nil {
@@ -148,7 +146,6 @@ func TestAIApplyAndUndoFlow(t *testing.T) {
applyCmd.SetArgs([]string{
"ai",
"--path", root,
"--import-plan", planPath,
"--yes",
})

View File

@@ -47,9 +47,6 @@ func TestAIPolicyValidationFailsWithActionableMessage(t *testing.T) {
"ai",
"--path", rootDir,
"--dry-run",
"--naming-casing", "kebab",
"--naming-prefix", "proj",
"--banned", "offer",
})
err := rootCmd.Execute()
@@ -58,9 +55,6 @@ func TestAIPolicyValidationFailsWithActionableMessage(t *testing.T) {
}
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)
}

View File

@@ -53,19 +53,18 @@ func TestAIPreviewFlowRendersSequenceTable(t *testing.T) {
createAIPreviewFile(t, filepath.Join(root, "promo SALE 01.JPG"))
createAIPreviewFile(t, filepath.Join(root, "family_photo.png"))
t.Setenv("default_MODEL_AUTH_TOKEN", "test-token")
t.Setenv("OPENAI_TOKEN", "test-token")
rootCmd := renamercmd.NewRootCommand()
var stdout, stderr bytes.Buffer
rootCmd.SetOut(&stdout)
rootCmd.SetErr(&stderr)
exportPath := filepath.Join(root, "plan.json")
exportPath := filepath.Join(root, "renamer.plan.json")
rootCmd.SetArgs([]string{
"ai",
"--path", root,
"--dry-run",
"--debug-genkit",
"--export-plan", exportPath,
})
if err := rootCmd.Execute(); err != nil {