Files
kandev/apps/web/e2e/tests/settings/prompts-settings.spec.ts
T
José Almeida 5223a1cd17 fix(prompts): warn on duplicate custom prompt name instead of 500 (#859)
* fix(backend): return 409 on duplicate prompt name

Pre-check by name in CreatePrompt and rename paths and surface
ErrPromptAlreadyExists as 409 with the underlying error message.
Previously the SQLite UNIQUE constraint bubbled up as a 500 with
a generic body, so the frontend had nothing actionable to show.

* feat(web): toast on prompt save errors with e2e coverage

Wire useToast in the Prompts settings page so create/update/delete
failures surface a warning instead of being swallowed silently. Add
data-testid hooks plus Playwright specs covering duplicate-name
create, rename-to-existing, and the happy path.

* fix(backend): close TOCTOU + tighten prompt error handling

Address PR review:
- log client errors (4xx) at Info, reserve Error for unexpected 500s
- switch handler error mapping to errors.Is so wrapped sentinels still
  resolve to the right HTTP status
- drop the unreachable existing==nil branch in assertNameAvailable
- translate SQLite UNIQUE constraint errors that slip through the
  pre-check race into ErrPromptAlreadyExists so concurrent dup-name
  inserts return 409 instead of 500
2026-05-10 17:04:56 +01:00

93 lines
3.5 KiB
TypeScript

import { test, expect } from "../../fixtures/test-base";
// Custom prompts share a UNIQUE name index with built-in prompts. Prior to the
// 409-mapping fix, posting a duplicate name returned 500 and the frontend
// silently swallowed the error. These tests guard the toast contract so a
// regression cannot ship unnoticed.
test.describe("Prompts settings — duplicate name handling", () => {
test.afterEach(async ({ apiClient }) => {
const { prompts } = await apiClient.listPrompts();
for (const p of prompts) {
if (!p.builtin) {
await apiClient.deletePrompt(p.id).catch(() => undefined);
}
}
});
test("creating a prompt with a duplicate name shows an error toast and keeps the form open", async ({
testPage,
apiClient,
}) => {
test.setTimeout(60_000);
await apiClient.createPrompt("dupe-prompt", "first content");
await testPage.goto("/settings/prompts");
await testPage.getByTestId("prompt-create-button").click();
const form = testPage.getByTestId("prompt-create-form");
await expect(form).toBeVisible();
await form.getByTestId("prompt-name-input").fill("dupe-prompt");
await form.getByTestId("prompt-content-input").fill("second content");
await form.getByTestId("prompt-submit").click();
const toast = testPage.getByTestId("toast-message");
await expect(toast).toBeVisible({ timeout: 5_000 });
await expect(toast).toContainText(/already exists/i);
// Form must remain open (no silent reset) so the user can fix the name.
await expect(form).toBeVisible();
await expect(form.getByTestId("prompt-name-input")).toHaveValue("dupe-prompt");
});
test("renaming a prompt to an existing name shows an error toast and does not persist the rename", async ({
testPage,
apiClient,
}) => {
test.setTimeout(60_000);
await apiClient.createPrompt("alpha", "a");
await apiClient.createPrompt("beta", "b");
await testPage.goto("/settings/prompts");
const betaRow = testPage.locator('[data-testid="prompt-list-item"][data-prompt-name="beta"]');
await expect(betaRow).toBeVisible();
await betaRow.getByTestId("prompt-edit-button").click();
const nameInput = betaRow.getByTestId("prompt-name-input");
await nameInput.fill("alpha");
await betaRow.getByTestId("prompt-submit").click();
const toast = testPage.getByTestId("toast-message");
await expect(toast).toBeVisible({ timeout: 5_000 });
await expect(toast).toContainText(/already exists/i);
// Backend rejected — the row must still exist under its original name.
await testPage.reload();
await expect(
testPage.locator('[data-testid="prompt-list-item"][data-prompt-name="beta"]'),
).toBeVisible();
});
test("creating a prompt with a unique name succeeds and appears in the list", async ({
testPage,
}) => {
test.setTimeout(60_000);
await testPage.goto("/settings/prompts");
await testPage.getByTestId("prompt-create-button").click();
const form = testPage.getByTestId("prompt-create-form");
await form.getByTestId("prompt-name-input").fill("e2e-fresh-prompt");
await form.getByTestId("prompt-content-input").fill("hello world");
await form.getByTestId("prompt-submit").click();
await expect(
testPage.locator('[data-testid="prompt-list-item"][data-prompt-name="e2e-fresh-prompt"]'),
).toBeVisible({ timeout: 10_000 });
// Form should be reset / closed on success.
await expect(testPage.getByTestId("prompt-create-form")).toHaveCount(0);
});
});