| 16 | ) |
| 17 | |
| 18 | func TestGenerate(t *testing.T) { |
| 19 | t.Run("FromPrompt", func(t *testing.T) { |
| 20 | // Ensure no API key in env for this test |
| 21 | t.Setenv("ANTHROPIC_API_KEY", "") |
| 22 | |
| 23 | ctx := testutil.Context(t, testutil.WaitShort) |
| 24 | |
| 25 | taskName := taskname.Generate(ctx, testutil.Logger(t), "Create a finance planning app") |
| 26 | |
| 27 | // Should succeed via prompt sanitization |
| 28 | require.NoError(t, codersdk.NameValid(taskName.Name)) |
| 29 | require.Contains(t, taskName.Name, "create-a-finance-planning-") |
| 30 | require.NotEmpty(t, taskName.DisplayName) |
| 31 | require.Equal(t, "Create a finance planning app", taskName.DisplayName) |
| 32 | }) |
| 33 | |
| 34 | t.Run("FromAnthropic", func(t *testing.T) { |
| 35 | apiKey := os.Getenv(anthropicEnvVar) |
| 36 | if apiKey == "" { |
| 37 | t.Skipf("Skipping test as %s not set", anthropicEnvVar) |
| 38 | } |
| 39 | |
| 40 | // Set API key for this test |
| 41 | t.Setenv("ANTHROPIC_API_KEY", apiKey) |
| 42 | |
| 43 | ctx := testutil.Context(t, testutil.WaitShort) |
| 44 | |
| 45 | taskName := taskname.Generate(ctx, testutil.Logger(t), "Create a finance planning app") |
| 46 | |
| 47 | // Should succeed with Claude-generated names |
| 48 | require.NoError(t, codersdk.NameValid(taskName.Name)) |
| 49 | require.NotEmpty(t, taskName.DisplayName) |
| 50 | }) |
| 51 | |
| 52 | t.Run("FromPromptMultiByte", func(t *testing.T) { |
| 53 | t.Setenv("ANTHROPIC_API_KEY", "") |
| 54 | |
| 55 | ctx := testutil.Context(t, testutil.WaitShort) |
| 56 | |
| 57 | taskName := taskname.Generate(ctx, testutil.Logger(t), "über cool feature") |
| 58 | |
| 59 | require.NoError(t, codersdk.NameValid(taskName.Name)) |
| 60 | require.True(t, len(taskName.DisplayName) > 0) |
| 61 | // The display name must start with "Ü", not corrupted bytes. |
| 62 | require.Equal(t, "Über cool feature", taskName.DisplayName) |
| 63 | }) |
| 64 | |
| 65 | t.Run("Fallback", func(t *testing.T) { |
| 66 | // Ensure no API key |
| 67 | t.Setenv("ANTHROPIC_API_KEY", "") |
| 68 | |
| 69 | ctx := testutil.Context(t, testutil.WaitShort) |
| 70 | |
| 71 | // Use a prompt that can't be sanitized (only special chars) |
| 72 | taskName := taskname.Generate(ctx, testutil.Logger(t), "!@#$%^&*()") |
| 73 | |
| 74 | // Should fall back to random name |
| 75 | require.NoError(t, codersdk.NameValid(taskName.Name)) |