mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-04 04:47:19 +01:00
* feat(field): DictationField with no state in the app — native forms and react-hook-form (0.44.0) - Uncontrolled mode: value/onValueChange optional, new defaultValue; the real text box carries name, so a native form or server action posts typed and said words, and a form reset restores defaultValue. - react-hook-form: forwardRef (ref is the text box), onChange(event) for every change including microphone/Improve/Undo/setText, onBlur(event). - Browser test drives react-hook-form itself (register, Controller, reset) and a native form; docs/roadmap.md records the remaining typing gaps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtoej4Vu1mYafzzcLc * docs(field): say the ref works across the peer React range, not a version Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtoej4Vu1mYafzzcLc --------- Co-authored-by: Claude <noreply@anthropic.com>
835 lines
35 KiB
JavaScript
835 lines
35 KiB
JavaScript
// DictationField and CommentThread, checked in a real browser at phone width.
|
|
//
|
|
// pnpm run demo && node test/browser-field.mjs
|
|
//
|
|
// What each check is for: a text box people type words into must have the
|
|
// microphone and 16px text (iOS zooms below it), every control must be a
|
|
// thumb's size, nothing may scroll sideways at 320px, a failed take or a
|
|
// failed "improve" must say so and keep the person's text, and a comment
|
|
// must post, list and be removable without a reload. On field-form.html: the
|
|
// submit keys a form needs, focus and blur for the whole field (the mic is not
|
|
// a blur; a blur during a take waits for the words), and an error that is
|
|
// seen in both themes and said in words.
|
|
import { createServer } from "node:http";
|
|
import { readFile } from "node:fs/promises";
|
|
import { extname, join, normalize } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { chromium } from "playwright";
|
|
|
|
const root = fileURLToPath(new URL("..", import.meta.url));
|
|
const types = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css" };
|
|
const server = createServer(async (req, res) => {
|
|
const path = normalize(join(root, decodeURIComponent(new URL(req.url, "http://x").pathname)));
|
|
if (!path.startsWith(root)) return res.writeHead(403).end();
|
|
try {
|
|
const body = await readFile(path);
|
|
res
|
|
.writeHead(200, { "content-type": types[extname(path)] ?? "application/octet-stream" })
|
|
.end(body);
|
|
} catch {
|
|
res.writeHead(404).end();
|
|
}
|
|
}).listen(0);
|
|
const base = `http://localhost:${server.address().port}/demo/field.html`;
|
|
|
|
let failures = 0;
|
|
const check = (ok, what) => {
|
|
console.log(`${ok ? "PASS" : "FAIL"} ${what}`);
|
|
if (!ok) failures++;
|
|
};
|
|
|
|
const browser = await chromium.launch({
|
|
...(process.env.CHROMIUM_PATH ? { executablePath: process.env.CHROMIUM_PATH } : {}),
|
|
args: ["--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream"],
|
|
});
|
|
try {
|
|
for (const [name, width, dark] of [
|
|
["phone", 390, false],
|
|
["narrow", 320, true],
|
|
["desktop", 1280, false],
|
|
]) {
|
|
const page = await browser.newPage({ viewport: { width, height: 900 } });
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.route("**/api/transcribe", (route) =>
|
|
route.fulfill({
|
|
contentType: "application/json",
|
|
body: JSON.stringify({ text: "said aloud" }),
|
|
}),
|
|
);
|
|
await page.goto(base);
|
|
if (dark) await page.evaluate(() => document.documentElement.classList.add("dark"));
|
|
await page.waitForSelector(".ck-field-input");
|
|
|
|
const m = await page.evaluate(() => {
|
|
const fields = [...document.querySelectorAll(".ck-field")];
|
|
const small = [
|
|
...document.querySelectorAll(
|
|
".ck-icon-btn, .ck-field-improve, .ck-comment-signin-link, .ck-send",
|
|
),
|
|
]
|
|
.map((e) => e.getBoundingClientRect())
|
|
.filter((r) => r.width > 0 && (r.width < 44 || r.height < 44)).length;
|
|
return {
|
|
overflow: document.documentElement.scrollWidth - window.innerWidth,
|
|
fields: fields.length,
|
|
mics: document.querySelectorAll(".ck-field .ck-mic").length,
|
|
fontMin: Math.min(
|
|
...[...document.querySelectorAll(".ck-field-input, .ck-comment-body, .ck-input")].map(
|
|
(e) => parseFloat(getComputedStyle(e).fontSize),
|
|
),
|
|
),
|
|
small,
|
|
labels: [...document.querySelectorAll("label.ck-field-label")].map((l) => l.textContent),
|
|
commentMics: document.querySelectorAll(".ck-comment-box .ck-mic").length,
|
|
signIn: document.querySelector(".ck-comment-signin-link")?.getAttribute("href"),
|
|
};
|
|
});
|
|
check(m.overflow <= 0, `${name}: no horizontal scroll (${m.overflow}px over)`);
|
|
check(
|
|
m.fields === 2 && m.mics === 2,
|
|
`${name}: a microphone in every field (${m.mics}/${m.fields})`,
|
|
);
|
|
check(m.commentMics === 1, `${name}: the comment box has the microphone too`);
|
|
check(m.fontMin >= 16, `${name}: field and comment text >= 16px (${m.fontMin}px)`);
|
|
check(m.small === 0, `${name}: every control >= 44px (${m.small} smaller)`);
|
|
check(m.labels.join() === "Title,Note", `${name}: fields are labelled (${m.labels})`);
|
|
check(m.signIn === "/sign-in", `${name}: a signed-out reader is shown the way to sign in`);
|
|
check(errors.length === 0, `${name}: no page errors ${errors.join(" | ")}`);
|
|
await page.close();
|
|
}
|
|
|
|
// ── the mic puts words in the field, appending to what is there ──────────
|
|
{
|
|
const page = await browser.newPage({ viewport: { width: 390, height: 900 } });
|
|
await page.route("**/api/transcribe", (route) =>
|
|
route.fulfill({
|
|
contentType: "application/json",
|
|
body: JSON.stringify({ text: "said aloud" }),
|
|
}),
|
|
);
|
|
await page.goto(base);
|
|
const note = page.locator("#field-fixture .ck-field-wrap").nth(1);
|
|
await note.locator("textarea.ck-field-input").fill("Typed first.");
|
|
// Headless Chromium has a recogniser with no speech service behind it, so
|
|
// the press records and posts the audio (the chatkit mic contract).
|
|
await note.locator(".ck-mic").click();
|
|
await page.locator(".ck-voice-confirm").first().waitFor({ timeout: 5000 });
|
|
await page.waitForTimeout(400);
|
|
await page.locator(".ck-voice-confirm").first().click();
|
|
await page
|
|
.waitForFunction(
|
|
() => document.querySelectorAll(".ck-field-input")[1].value.includes("said aloud"),
|
|
null,
|
|
{ timeout: 8000 },
|
|
)
|
|
.catch(() => {});
|
|
check(
|
|
(await note.locator("textarea.ck-field-input").inputValue()) === "Typed first. said aloud",
|
|
"mic: the words are appended to what was typed",
|
|
);
|
|
|
|
// ── improve: a suggestion runs in one tap, Undo puts the text back ─────
|
|
await note.getByRole("button", { name: "Improve" }).click();
|
|
check(
|
|
(await note.locator(".ck-field-suggestion").count()) === 2,
|
|
"improve: the suggestions are offered as buttons",
|
|
);
|
|
const cols = await note
|
|
.locator(".ck-field-suggestions")
|
|
.evaluate((el) => getComputedStyle(el).gridTemplateColumns.split(" ").length);
|
|
check(
|
|
cols === 2,
|
|
`improve: suggestions are an even grid (${cols} columns), not wrapping pills`,
|
|
);
|
|
await note.getByRole("button", { name: "Make it tighter" }).click();
|
|
await page
|
|
.waitForFunction(
|
|
() => document.querySelectorAll(".ck-field-input")[1].value.includes("[Make it tighter]"),
|
|
null,
|
|
{ timeout: 5000 },
|
|
)
|
|
.catch(() => {});
|
|
check(
|
|
(await note.locator("textarea.ck-field-input").inputValue()) ===
|
|
"Typed first. said aloud [Make it tighter]",
|
|
"improve: the text is replaced by the improved one",
|
|
);
|
|
check((await note.locator(".ck-field-undo").count()) === 1, "improve: Undo is offered");
|
|
await note.getByRole("button", { name: "Undo" }).click();
|
|
check(
|
|
(await note.locator("textarea.ck-field-input").inputValue()) === "Typed first. said aloud",
|
|
"improve: Undo puts the previous text back",
|
|
);
|
|
|
|
// ── improve fails: said in words, text kept, the way forward stays ─────
|
|
await note.getByRole("button", { name: "Improve" }).click();
|
|
await note.getByLabel("What should change?").fill("make it fail");
|
|
await note.getByRole("button", { name: "Change it" }).click();
|
|
await note.locator('.ck-problem[role="alert"]').waitFor({ timeout: 5000 });
|
|
check(
|
|
(await note.locator("textarea.ck-field-input").inputValue()) === "Typed first. said aloud",
|
|
"improve failure: the person's text is unchanged",
|
|
);
|
|
check(
|
|
(await note.getByRole("button", { name: "Change it" }).isEnabled()) === false ||
|
|
(await note.getByLabel("What should change?").inputValue()) === "make it fail",
|
|
"improve failure: the instruction is kept, so it can be retried",
|
|
);
|
|
await page.close();
|
|
}
|
|
|
|
// ── comments: post, list, remove; a refused post keeps the draft ─────────
|
|
{
|
|
const page = await browser.newPage({ viewport: { width: 390, height: 900 } });
|
|
await page.goto(base);
|
|
const thread = page.locator(".ck-comments").first();
|
|
check(
|
|
(await thread.locator(".ck-comments-title").innerText()) === "2 comments",
|
|
"comments: the count reads '2 comments'",
|
|
);
|
|
check(
|
|
(await thread.locator(".ck-comment-remove").count()) === 0,
|
|
"comments: nobody gets a Remove on comments that are not theirs",
|
|
);
|
|
await thread.locator(".ck-input").fill("my turn");
|
|
await thread.getByRole("button", { name: "Comment", exact: true }).click();
|
|
await page
|
|
.waitForFunction(
|
|
() => document.querySelector(".ck-comments-title")?.textContent === "3 comments",
|
|
null,
|
|
{ timeout: 5000 },
|
|
)
|
|
.catch(() => {});
|
|
check(
|
|
(await thread.locator(".ck-comments-title").innerText()) === "3 comments",
|
|
"comments: a posted comment is listed at once",
|
|
);
|
|
check(
|
|
(await thread.locator(".ck-input").inputValue()) === "",
|
|
"comments: the box is cleared after a post",
|
|
);
|
|
check(
|
|
(await thread.locator(".ck-comment-remove").count()) === 1,
|
|
"comments: your own comment can be removed",
|
|
);
|
|
await thread.locator(".ck-comment-remove").click();
|
|
await page
|
|
.waitForFunction(
|
|
() => document.querySelector(".ck-comments-title")?.textContent === "2 comments",
|
|
null,
|
|
{ timeout: 5000 },
|
|
)
|
|
.catch(() => {});
|
|
check(
|
|
(await thread.locator(".ck-comments-title").innerText()) === "2 comments",
|
|
"comments: removing takes it off the list",
|
|
);
|
|
await thread.locator(".ck-input").fill("boom");
|
|
await thread.getByRole("button", { name: "Comment", exact: true }).click();
|
|
await thread.locator('.ck-problem[role="alert"]').waitFor({ timeout: 5000 });
|
|
check(
|
|
(await thread.locator(".ck-input").inputValue()) === "boom",
|
|
"comments: a refused post keeps the draft in the box",
|
|
);
|
|
const text = await thread.innerText();
|
|
check(
|
|
text.includes("A longer one") && !text.includes("<"),
|
|
"comments: line breaks and text are shown as text",
|
|
);
|
|
const empty = page.locator(".ck-comments").nth(1);
|
|
check(
|
|
(await empty.locator(".ck-comments-title").innerText()) === "No comments yet",
|
|
"comments: an empty thread says so",
|
|
);
|
|
await page.close();
|
|
}
|
|
|
|
// ── inside a host's own base styles: the frame shows focus, the field ──
|
|
// inside it draws no second box (OrangeCat's planner kept a local fix).
|
|
{
|
|
const page = await browser.newPage({ viewport: { width: 390, height: 844 } });
|
|
await page.goto(`${base}?host=hostile`);
|
|
await page.waitForSelector(".ck-field-input");
|
|
for (const i of [0, 1]) {
|
|
const input = page.locator(".ck-field-input").nth(i);
|
|
await input.focus();
|
|
await page.keyboard.press("Shift+Tab");
|
|
await page.keyboard.press("Tab");
|
|
const s = await input.evaluate((e) => {
|
|
const c = getComputedStyle(e);
|
|
return { ring: c.boxShadow, radius: c.borderRadius };
|
|
});
|
|
check(
|
|
s.ring === "none" && s.radius === "0px",
|
|
`host styles: field ${i} has no ring or rounding of its own (${s.ring}, ${s.radius})`,
|
|
);
|
|
}
|
|
await page.close();
|
|
}
|
|
|
|
// ── a form's field: submit keys, field-level focus, the error state ──────
|
|
// On demo/field-form.html: an inline editor (Note) that logs focus, blur
|
|
// and submit, a one-line Title with a name and an error inside a native
|
|
// <form>, and an "Elsewhere" button to take focus to.
|
|
const formUrl = base.replace("field.html", "field-form.html");
|
|
for (const [name, width, dark] of [
|
|
["form phone light", 390, false],
|
|
["form phone dark", 390, true],
|
|
["form desktop light", 1280, false],
|
|
["form desktop dark", 1280, true],
|
|
]) {
|
|
const page = await browser.newPage({ viewport: { width, height: 900 } });
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
let delay = 0;
|
|
await page.route("**/api/transcribe", async (route) => {
|
|
await new Promise((r) => setTimeout(r, delay));
|
|
await route.fulfill({
|
|
contentType: "application/json",
|
|
body: JSON.stringify({ text: "said aloud" }),
|
|
});
|
|
});
|
|
await page.goto(formUrl);
|
|
if (dark) await page.evaluate(() => document.documentElement.classList.add("dark"));
|
|
const note = page.locator("#note");
|
|
const title = page.locator("#title");
|
|
await note.waitFor();
|
|
const logOf = () => page.evaluate(() => window.__log.slice());
|
|
const count = async (prefix) => (await logOf()).filter((e) => e.startsWith(prefix)).length;
|
|
const reset = () => page.evaluate(() => (window.__log.length = 0));
|
|
|
|
// Submit keys in a multi-line field: Enter is a new line, Cmd/Ctrl+Enter submits.
|
|
await note.click();
|
|
check((await count("focus:note")) === 1, `${name}: focus entering the field is reported once`);
|
|
await note.pressSequentially("hello");
|
|
await note.press("Enter");
|
|
check(
|
|
(await count("submit:")) === 0 && (await note.inputValue()) === "hello\n",
|
|
`${name}: Enter in a multi-line field is a new line, not a submit`,
|
|
);
|
|
await note.press("Control+Enter");
|
|
await note.press("Meta+Enter");
|
|
check((await count("submit:note")) === 2, `${name}: Ctrl+Enter and Cmd+Enter submit`);
|
|
check(
|
|
(await note.inputValue()) === "hello\n",
|
|
`${name}: a submitting Enter adds no line break`,
|
|
);
|
|
await note.evaluate((el) =>
|
|
el.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "Enter",
|
|
ctrlKey: true,
|
|
isComposing: true,
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
),
|
|
);
|
|
check(
|
|
(await count("submit:note")) === 2,
|
|
`${name}: an Enter ending an IME composition does not submit`,
|
|
);
|
|
await page.evaluate(() => (window.__suppressEnter = true));
|
|
await note.press("Control+Enter");
|
|
await page.evaluate(() => (window.__suppressEnter = false));
|
|
check(
|
|
(await count("submit:note")) === 2,
|
|
`${name}: onKeyDown's preventDefault stops the submit`,
|
|
);
|
|
await note.press("Escape");
|
|
check((await count("cancel:note")) === 1, `${name}: the app's onKeyDown sees every key`);
|
|
|
|
// Field-level focus: the microphone and Improve are part of the field.
|
|
await reset();
|
|
const wrap = page.locator(".ck-field-wrap").first();
|
|
await wrap.getByRole("button", { name: "Improve" }).click();
|
|
await wrap.getByRole("button", { name: "Improve" }).click();
|
|
await wrap.locator(".ck-mic").first().click();
|
|
await page.locator(".ck-voice-confirm").first().waitFor({ timeout: 5000 });
|
|
check((await count("blur:")) === 0, `${name}: pressing the microphone is not a blur`);
|
|
await page.waitForTimeout(300);
|
|
await page.locator(".ck-voice-confirm").first().click();
|
|
await page
|
|
.waitForFunction(() => document.querySelector("#note").value.includes("said aloud"), null, {
|
|
timeout: 8000,
|
|
})
|
|
.catch(() => {});
|
|
await page.waitForTimeout(100);
|
|
check(
|
|
(await note.inputValue()).includes("said aloud") && (await count("blur:")) === 0,
|
|
`${name}: a whole take, pressed through to done, is not a blur (${await logOf()})`,
|
|
);
|
|
await note.click();
|
|
await page.locator("#elsewhere").click();
|
|
await page.waitForTimeout(100);
|
|
check(
|
|
(await count("blur:note")) === 1,
|
|
`${name}: focus moving to another element is one blur (${await logOf()})`,
|
|
);
|
|
// A tap on empty page: focus goes nowhere (relatedTarget null), still a blur.
|
|
await note.click();
|
|
await page.mouse.click(4, 890);
|
|
await page.waitForTimeout(100);
|
|
check(
|
|
(await count("blur:note")) === 2 && (await count("focus:note")) === 1,
|
|
`${name}: a tap on empty page is one blur (${await logOf()})`,
|
|
);
|
|
|
|
// A blur during a take waits for the words.
|
|
await reset();
|
|
delay = 1200;
|
|
await note.click();
|
|
await wrap.locator(".ck-mic").first().click();
|
|
await page.locator(".ck-voice-confirm").first().waitFor({ timeout: 5000 });
|
|
await page.waitForTimeout(300);
|
|
const before = await note.inputValue();
|
|
await page.locator(".ck-voice-confirm").first().click();
|
|
await page.locator("#elsewhere").click();
|
|
await page.waitForTimeout(300);
|
|
check(
|
|
(await count("blur:")) === 0,
|
|
`${name}: leaving while the take is transcribed holds the blur (${await logOf()})`,
|
|
);
|
|
await page
|
|
.waitForFunction((b) => document.querySelector("#note").value.length > b.length, before, {
|
|
timeout: 8000,
|
|
})
|
|
.catch(() => {});
|
|
await page.waitForTimeout(100);
|
|
const after = await note.inputValue();
|
|
const blurs = (await logOf()).filter((e) => e.startsWith("blur:note:"));
|
|
check(
|
|
blurs.length === 1 && blurs[0] === `blur:note:${after}` && after.length > before.length,
|
|
`${name}: the held blur comes once, after the words landed (${blurs})`,
|
|
);
|
|
delay = 0;
|
|
|
|
// Improve at work disables its own box: focus falls off it with nowhere
|
|
// to go, long after any press. The person is still in the field.
|
|
await reset();
|
|
await note.click();
|
|
await wrap.getByRole("button", { name: "Improve" }).click();
|
|
const what = page.locator(".ck-field-improve-panel input");
|
|
await what.fill("slow please");
|
|
await page.waitForTimeout(1100);
|
|
const asked = await note.inputValue();
|
|
await what.press("Enter");
|
|
await page
|
|
.waitForFunction(
|
|
() => document.querySelector("#note").value.includes("[slow please]"),
|
|
null,
|
|
{
|
|
timeout: 5000,
|
|
},
|
|
)
|
|
.catch(() => {});
|
|
await page.waitForTimeout(200);
|
|
check(
|
|
(await note.inputValue()) === `${asked} [slow please]` && (await count("blur:")) === 0,
|
|
`${name}: applying Improve from the keyboard while it works is not a blur (${await logOf()})`,
|
|
);
|
|
await page.locator("#elsewhere").click();
|
|
await page.waitForTimeout(100);
|
|
check(
|
|
(await count("blur:note")) === 1,
|
|
`${name}: and leaving afterwards is one blur (${await logOf()})`,
|
|
);
|
|
|
|
// A one-line field: Enter submits, Shift+Enter and a composing Enter do not.
|
|
await reset();
|
|
await title.click();
|
|
await title.press("Enter");
|
|
check((await count("submit:title:")) === 1, `${name}: Enter submits a one-line field`);
|
|
check((await count("native-submit")) === 0, `${name}: and the native form is not also sent`);
|
|
const err = page.locator(".ck-field-error");
|
|
await err.waitFor({ timeout: 2000 }).catch(() => {});
|
|
await page.waitForTimeout(300); // the frame's border-color transition
|
|
const look = await page.evaluate(() => {
|
|
const noteBox = document.querySelector("#note").closest(".ck-field");
|
|
const titleBox = document.querySelector("#title").closest(".ck-field");
|
|
const input = document.querySelector("#title");
|
|
const error = document.querySelector(".ck-field-error");
|
|
return {
|
|
valid: getComputedStyle(noteBox).borderTopColor,
|
|
invalid: getComputedStyle(titleBox).borderTopColor,
|
|
ariaInvalid: input.getAttribute("aria-invalid"),
|
|
describedBy: (input.getAttribute("aria-describedby") ?? "")
|
|
.split(" ")
|
|
.map((id) => document.getElementById(id)?.textContent ?? "(missing)"),
|
|
role: error?.getAttribute("role"),
|
|
text: error?.textContent ?? "",
|
|
errorColour: error ? getComputedStyle(error).color : "",
|
|
textColour: getComputedStyle(document.querySelector(".ck-field-label")).color,
|
|
};
|
|
});
|
|
check(
|
|
look.valid !== look.invalid,
|
|
`${name}: the invalid frame is drawn in another colour (${look.invalid} vs ${look.valid})`,
|
|
);
|
|
check(
|
|
look.text.includes("A title is needed") && look.role === "alert",
|
|
`${name}: the error is said in words, as an alert`,
|
|
);
|
|
check(
|
|
look.ariaInvalid === "true" &&
|
|
look.describedBy.join("|") ===
|
|
"A title is needed before this can be saved.|One line, said or typed.",
|
|
`${name}: aria-invalid, and the error and hint describe the field (${look.describedBy})`,
|
|
);
|
|
await title.pressSequentially("My title");
|
|
await title.press("Shift+Enter");
|
|
check(
|
|
(await count("submit:title:")) === 1 && (await count("native-submit")) === 0,
|
|
`${name}: Shift+Enter submits neither the field nor its native form (${await logOf()})`,
|
|
);
|
|
// Enter during a take: no submit, and the native form is not sent without
|
|
// the words being said.
|
|
await page.locator("#native .ck-mic").click();
|
|
await page.locator("#native .ck-voice-confirm").waitFor({ timeout: 5000 });
|
|
await title.focus();
|
|
await title.press("Enter");
|
|
check(
|
|
(await count("submit:title:")) === 1 && (await count("native-submit")) === 0,
|
|
`${name}: Enter while listening sends nothing (${await logOf()})`,
|
|
);
|
|
await page.locator("#native .ck-voice-confirm").click();
|
|
await page
|
|
.waitForFunction(() => document.querySelector("#title").value.includes("said aloud"), null, {
|
|
timeout: 8000,
|
|
})
|
|
.catch(() => {});
|
|
await title.fill("My title");
|
|
await title.evaluate((el) =>
|
|
el.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "Enter",
|
|
isComposing: true,
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
),
|
|
);
|
|
check((await count("submit:title:")) === 1, `${name}: a composing Enter does not submit`);
|
|
await title.press("Enter");
|
|
check(
|
|
(await logOf()).includes("submit:title:My title"),
|
|
`${name}: Enter submits the one-line field with its text`,
|
|
);
|
|
check(
|
|
(await page.evaluate(() => new FormData(document.querySelector("#native")).get("title"))) ===
|
|
"My title",
|
|
`${name}: the field posts with its form under its name`,
|
|
);
|
|
check(
|
|
(await page.locator(".ck-field-error").count()) === 0 &&
|
|
(await title.getAttribute("aria-invalid")) === null,
|
|
`${name}: the error goes once it is fixed`,
|
|
);
|
|
check(errors.length === 0, `${name}: no page errors ${errors.join(" | ")}`);
|
|
await page.close();
|
|
}
|
|
|
|
// ── an error shown at phone widths: nothing small, nothing sideways ──────
|
|
for (const [name, width, dark] of [
|
|
["error 390 light", 390, false],
|
|
["error 390 dark", 390, true],
|
|
["error 320 light", 320, false],
|
|
["error 320 dark", 320, true],
|
|
]) {
|
|
const page = await browser.newPage({ viewport: { width, height: 900 } });
|
|
await page.goto(formUrl);
|
|
if (dark) await page.evaluate(() => document.documentElement.classList.add("dark"));
|
|
await page.locator("#title").click();
|
|
await page.locator("#title").press("Enter");
|
|
await page
|
|
.locator(".ck-field-error")
|
|
.waitFor({ timeout: 2000 })
|
|
.catch(() => {});
|
|
const m = await page.evaluate(() => ({
|
|
error: document.querySelector(".ck-field-error")?.getBoundingClientRect().height ?? 0,
|
|
overflow: document.documentElement.scrollWidth - window.innerWidth,
|
|
small: [...document.querySelectorAll(".ck-field-wrap button")]
|
|
.map((e) => e.getBoundingClientRect())
|
|
.filter((r) => r.width > 0 && (r.width < 44 || r.height < 44)).length,
|
|
fontMin: Math.min(
|
|
...[...document.querySelectorAll(".ck-field-input")].map((e) =>
|
|
parseFloat(getComputedStyle(e).fontSize),
|
|
),
|
|
),
|
|
}));
|
|
check(m.error > 0, `${name}: the error is shown`);
|
|
check(m.overflow <= 0, `${name}: no horizontal scroll with an error (${m.overflow}px over)`);
|
|
check(m.small === 0, `${name}: every control >= 44px with an error (${m.small} smaller)`);
|
|
check(m.fontMin >= 16, `${name}: field text stays >= 16px (${m.fontMin}px)`);
|
|
await page.close();
|
|
}
|
|
// ── the paperclip in a DictationField (field-attach.html) ────────────────
|
|
// The same paperclip as the composer: off by default, on when asked; a file
|
|
// picked or a screenshot pasted is staged with a preview and Remove, and the
|
|
// app (which owns the staged files) sends them with its own submit and
|
|
// clears them after.
|
|
const attachUrl = base.replace("field.html", "field-attach.html");
|
|
for (const [name, width, dark] of [
|
|
["attach phone", 390, false],
|
|
["attach narrow dark", 320, true],
|
|
["attach desktop", 1280, false],
|
|
]) {
|
|
const page = await browser.newPage({ viewport: { width, height: 900 } });
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.goto(attachUrl);
|
|
if (dark) await page.evaluate(() => document.documentElement.classList.add("dark"));
|
|
const task = page.locator(".ck-field-wrap").filter({ has: page.locator("#task") });
|
|
const note = page.locator(".ck-field-wrap").filter({ has: page.locator("#attach-note") });
|
|
const plain = page.locator(".ck-field-wrap").filter({ has: page.locator("#plain") });
|
|
await task.locator("#task").waitFor();
|
|
const clip = '[aria-haspopup="dialog"]';
|
|
check(
|
|
(await plain.locator(clip).count()) === 0 && (await plain.locator(".ck-mic").count()) === 1,
|
|
`${name}: attach is off by default (mic, no paperclip)`,
|
|
);
|
|
check(
|
|
(await task.locator(clip).count()) === 1 && (await note.locator(clip).count()) === 1,
|
|
`${name}: attach on shows the paperclip beside the mic`,
|
|
);
|
|
const png = await page.evaluate(() => {
|
|
const c = document.createElement("canvas");
|
|
c.width = 40;
|
|
c.height = 30;
|
|
const ctx = c.getContext("2d");
|
|
ctx.fillStyle = "#c33";
|
|
ctx.fillRect(0, 0, 40, 30);
|
|
return c.toDataURL("image/png").split(",")[1];
|
|
});
|
|
// Picked through Files: staged with its preview and a Remove.
|
|
await task.locator('input[data-ck-source="photos"]').setInputFiles({
|
|
name: "receipt.png",
|
|
mimeType: "image/png",
|
|
buffer: Buffer.from(png, "base64"),
|
|
});
|
|
await task
|
|
.locator(".ck-attach-thumb")
|
|
.waitFor({ timeout: 5000 })
|
|
.catch(() => {});
|
|
check(
|
|
(await task.locator(".ck-attach-thumb").count()) === 1,
|
|
`${name}: a picked photo is staged with a preview`,
|
|
);
|
|
await task.locator(".ck-attach-file, .ck-attach-thumb").first().waitFor();
|
|
await task.locator('input[data-ck-source="files"]').setInputFiles({
|
|
name: "list.txt",
|
|
mimeType: "text/plain",
|
|
buffer: Buffer.from("milk\neggs"),
|
|
});
|
|
await task
|
|
.locator(".ck-attach-file")
|
|
.waitFor({ timeout: 5000 })
|
|
.catch(() => {});
|
|
check((await task.locator(".ck-attach-item").count()) === 2, `${name}: a text file stages too`);
|
|
// Pasted: a screenshot on the clipboard is staged, the paste is consumed.
|
|
await task.locator("#task").fill("Pay this");
|
|
await page.evaluate((b64) => {
|
|
const bytes = Uint8Array.from(atob(b64), (ch) => ch.charCodeAt(0));
|
|
const dt = new DataTransfer();
|
|
dt.items.add(new File([bytes], "screenshot.png", { type: "image/png" }));
|
|
const input = document.querySelector("#attach-note");
|
|
input.focus();
|
|
input.dispatchEvent(
|
|
new ClipboardEvent("paste", { clipboardData: dt, bubbles: true, cancelable: true }),
|
|
);
|
|
}, png);
|
|
await note
|
|
.locator(".ck-attach-thumb")
|
|
.waitFor({ timeout: 5000 })
|
|
.catch(() => {});
|
|
check(
|
|
(await note.locator(".ck-attach-thumb").count()) === 1,
|
|
`${name}: a pasted screenshot is staged`,
|
|
);
|
|
// Remove takes one off and leaves the rest.
|
|
await task.getByRole("button", { name: "Remove list.txt" }).click();
|
|
check(
|
|
(await task.locator(".ck-attach-item").count()) === 1,
|
|
`${name}: Remove takes the file off and keeps the other`,
|
|
);
|
|
const m = await page.evaluate(() => ({
|
|
overflow: document.documentElement.scrollWidth - window.innerWidth,
|
|
small: [...document.querySelectorAll(".ck-icon-btn")]
|
|
.map((e) => e.getBoundingClientRect())
|
|
.filter((r) => r.width > 0 && (r.width < 44 || r.height < 44)).length,
|
|
}));
|
|
// One box: the field's paperclip and microphone are the composer's bare
|
|
// icons (test/browser.mjs measures the composer's the same way).
|
|
const fieldTools = await page.evaluate(() =>
|
|
[...document.querySelectorAll(".ck-field .ck-tools .ck-icon-btn:not(.ck-mic-on)")].map(
|
|
(e) => {
|
|
const s = getComputedStyle(e);
|
|
return `${s.backgroundColor}|${s.borderTopColor}`;
|
|
},
|
|
),
|
|
);
|
|
check(
|
|
fieldTools.length >= 4 && fieldTools.every((c) => c === "rgba(0, 0, 0, 0)|rgba(0, 0, 0, 0)"),
|
|
`${name}: the field's ${fieldTools.length} tools are the composer's bare icons (${[...new Set(fieldTools)].join(", ")})`,
|
|
);
|
|
check(m.overflow <= 0, `${name}: no horizontal scroll with files staged (${m.overflow}px)`);
|
|
check(m.small === 0, `${name}: paperclip and mic >= 44px (${m.small} smaller)`);
|
|
// The app's own submit (Enter in a one-line field) sends words and files,
|
|
// then clears both.
|
|
await task.locator("#task").press("Enter");
|
|
const added = await page.evaluate(() => window.__added.slice());
|
|
check(
|
|
added.length === 1 &&
|
|
added[0].text === "Pay this" &&
|
|
added[0].files.length === 1 &&
|
|
added[0].files[0].kind === "image",
|
|
`${name}: the app reads the staged files at its submit (${JSON.stringify(
|
|
added.map((a) => ({ text: a.text, files: a.files.map((f) => f.name) })),
|
|
)})`,
|
|
);
|
|
await page.waitForTimeout(50);
|
|
check(
|
|
(await task.locator(".ck-attach-item").count()) === 0 &&
|
|
(await task.locator("#task").inputValue()) === "",
|
|
`${name}: the app clears the staged files after`,
|
|
);
|
|
// The composer's controls on a field: handleRef fills and submits it from
|
|
// outside; attachExtras puts the app's rows in the paperclip's sheet.
|
|
const handledBox = page.locator("#handled");
|
|
await page.locator("#fill").click();
|
|
check(
|
|
(await handledBox.inputValue()) === "Filled from outside",
|
|
`${name}: handleRef.setText fills the field`,
|
|
);
|
|
await page.locator("#press").click();
|
|
await page.waitForTimeout(50);
|
|
const handledSent = await page.evaluate(() => window.__handled.slice());
|
|
check(
|
|
handledSent.length === 1 && handledSent[0] === "Filled from outside",
|
|
`${name}: handleRef.submit runs the field's onSubmit (${JSON.stringify(handledSent)})`,
|
|
);
|
|
const handledWrap = page.locator(".ck-field-wrap").filter({ has: handledBox });
|
|
await handledWrap.locator(clip).click();
|
|
await page.locator('[data-ck-extra="saved"]').click();
|
|
check(
|
|
(await page.locator("#picked").textContent()) === "saved",
|
|
`${name}: attachExtras puts the app's row in the field's sheet`,
|
|
);
|
|
check(errors.length === 0, `${name}: no page errors ${errors.join(" | ")}`);
|
|
await page.close();
|
|
}
|
|
// ── no state in the app: a native form, and react-hook-form ─────────────
|
|
// On demo/field-uncontrolled.html. The words typed AND said must reach a
|
|
// native post and a form library, and a reset must put the first words back.
|
|
{
|
|
const page = await browser.newPage({ viewport: { width: 390, height: 900 } });
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.route("**/api/transcribe", (route) =>
|
|
route.fulfill({
|
|
contentType: "application/json",
|
|
body: JSON.stringify({ text: "said aloud" }),
|
|
}),
|
|
);
|
|
await page.goto(base.replace("field.html", "field-uncontrolled.html"));
|
|
const story = page.locator("#story");
|
|
await story.waitFor();
|
|
const say = async (wrapOf) => {
|
|
await page.locator(".ck-field-wrap").filter({ has: wrapOf }).locator(".ck-mic").click();
|
|
await page.locator(".ck-voice-confirm").first().waitFor({ timeout: 5000 });
|
|
await page.waitForTimeout(400);
|
|
await page.locator(".ck-voice-confirm").first().click();
|
|
await page.waitForFunction(
|
|
(sel) => document.querySelector(sel).value.includes("said aloud"),
|
|
`#${await wrapOf.getAttribute("id")}`,
|
|
{ timeout: 8000 },
|
|
);
|
|
};
|
|
check((await story.inputValue()) === "Once", "uncontrolled: defaultValue is the first words");
|
|
await story.press("End");
|
|
await story.pressSequentially(" upon a time.");
|
|
await say(story);
|
|
check(
|
|
(await story.inputValue()) === "Once upon a time. said aloud",
|
|
`uncontrolled: what is said appends to what was typed (${await story.inputValue()})`,
|
|
);
|
|
await page.locator("#plain-post").click();
|
|
const posted = await page.evaluate(() => window.__posted.slice());
|
|
check(
|
|
posted.length === 1 && posted[0].story === "Once upon a time. said aloud",
|
|
`uncontrolled: a native form posts the typed and said words (${JSON.stringify(posted)})`,
|
|
);
|
|
await page.locator("#plain-reset").click();
|
|
await page.waitForTimeout(50);
|
|
check(
|
|
(await story.inputValue()) === "Once",
|
|
"uncontrolled: a form reset puts defaultValue back",
|
|
);
|
|
await say(story);
|
|
check(
|
|
(await story.inputValue()) === "Once said aloud",
|
|
`uncontrolled: after a reset, a take appends to the reset words (${await story.inputValue()})`,
|
|
);
|
|
|
|
// react-hook-form: {...register("bio")} and a Controller's {...field}.
|
|
const bio = page.locator("#bio");
|
|
const summary = page.locator("#summary");
|
|
check(
|
|
(await bio.inputValue()) === "Hello" && (await summary.inputValue()) === "Short",
|
|
"react-hook-form: defaultValues reach register() and Controller fields",
|
|
);
|
|
await bio.press("End");
|
|
await bio.pressSequentially(" there.");
|
|
await say(bio);
|
|
await summary.press("End");
|
|
await summary.pressSequentially(" one");
|
|
await say(summary);
|
|
await page.locator("#rhf-save").click();
|
|
await page.waitForFunction(() => window.__rhf.length > 0, null, { timeout: 3000 });
|
|
const saved = await page.evaluate(() => window.__rhf.slice());
|
|
check(
|
|
saved[0]?.bio === "Hello there. said aloud" && saved[0]?.summary === "Short one said aloud",
|
|
`react-hook-form: submitted values hold typed and said words (${JSON.stringify(saved)})`,
|
|
);
|
|
check(
|
|
(await page.evaluate(() => window.__rhfBlurred.length)) > 0,
|
|
"react-hook-form: register()'s onBlur hears the field being left",
|
|
);
|
|
await bio.fill("");
|
|
await summary.click();
|
|
await page
|
|
.locator("#bio-error")
|
|
.waitFor({ timeout: 3000 })
|
|
.catch(() => {});
|
|
check(
|
|
(await page
|
|
.locator("#bio-error")
|
|
.textContent()
|
|
.catch(() => "")) === "A bio is needed.",
|
|
"react-hook-form: validation on blur shows the error under the field",
|
|
);
|
|
await page.locator("#rhf-reset").click();
|
|
await page.waitForTimeout(50);
|
|
check(
|
|
(await bio.inputValue()) === "Hello" && (await summary.inputValue()) === "Short",
|
|
"react-hook-form: reset() puts the default values back",
|
|
);
|
|
await say(bio);
|
|
check(
|
|
(await bio.inputValue()) === "Hello said aloud",
|
|
`react-hook-form: after reset(), a take appends to the reset words (${await bio.inputValue()})`,
|
|
);
|
|
check(errors.length === 0, `uncontrolled: no page errors ${errors.join(" | ")}`);
|
|
await page.close();
|
|
}
|
|
} finally {
|
|
await browser.close();
|
|
server.close();
|
|
}
|
|
if (failures) {
|
|
console.error(`${failures} check(s) failed`);
|
|
process.exit(1);
|
|
}
|
|
console.log("all field and comment checks passed");
|