provider-model-editor-cases.mjs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import assert from "node:assert/strict";
  2. import { test } from "node:test";
  3. export function registerProviderModelEditorCases(getEnvironment) {
  4. for (const [width, locale, theme] of [
  5. [390, "zh-CN", "light"],
  6. [1200, "en-US", "dark"],
  7. ]) {
  8. test(`empty new model reasoning ${width}/${locale}`, { timeout: 30000 }, async () => {
  9. const { browser, origin } = getEnvironment();
  10. const page = await browser.newPage({ viewport: { width, height: 1000 } });
  11. const errors = [],
  12. requests = [];
  13. page.on("pageerror", (error) => errors.push(String(error)));
  14. let receive;
  15. await page.route("**/resolve?*", (route) => {
  16. requests.push(route.request().url());
  17. receive(route);
  18. });
  19. try {
  20. await page.goto(
  21. `${origin}fixture?${new URLSearchParams({ locale, theme, access: "manual" })}`,
  22. );
  23. await page.getByTestId("model-provider-add-model-button").click();
  24. await page.locator("[data-model-advanced-trigger]").click();
  25. const editor = page.locator("[data-model-reasoning-level-editor]");
  26. const chips = editor.locator("[data-model-reasoning-chip]");
  27. const id = page.locator("[data-model-identity-row] input");
  28. await editor.waitFor();
  29. assert.equal(await chips.count(), 0);
  30. assert.equal(requests.length, 0);
  31. const first = new Promise((resolve) => {
  32. receive = resolve;
  33. });
  34. await id.fill("new-model");
  35. await id.blur();
  36. const recommendation = {
  37. inheritedConfig: {
  38. properties: { contextWindow: 111111 },
  39. optionSpecs: { reasoningLevel: { values: ["disabled", "enabled"], map: "{}" } },
  40. },
  41. issues: [],
  42. revision: 1,
  43. };
  44. await (
  45. await first
  46. ).fulfill({ contentType: "application/json", body: JSON.stringify(recommendation) });
  47. await chips.filter({ hasText: "enabled" }).waitFor();
  48. assert.equal(await chips.count(), 2);
  49. await id.fill("");
  50. await id.blur();
  51. await page.waitForFunction(
  52. () => document.querySelectorAll("[data-model-reasoning-chip]").length === 0,
  53. );
  54. assert.equal(requests.length, 1);
  55. // 空草稿可手动加档位,随后推荐回包不能覆盖显式编辑。
  56. await editor.getByRole("button").last().click();
  57. await editor.locator("input").fill("custom-level");
  58. await editor.locator("input").press("Enter");
  59. const second = new Promise((resolve) => {
  60. receive = resolve;
  61. });
  62. await id.fill("another-model");
  63. await id.blur();
  64. recommendation.inheritedConfig.properties.contextWindow = 222222;
  65. await (
  66. await second
  67. ).fulfill({ contentType: "application/json", body: JSON.stringify(recommendation) });
  68. // 等到该解析已完成,验证未覆盖用户档位。
  69. await page.waitForFunction(
  70. () =>
  71. document
  72. .querySelector('[data-model-settings-group="tokens"] input')
  73. ?.getAttribute("placeholder") === "222222",
  74. );
  75. assert.equal(await chips.count(), 1);
  76. assert.match(await chips.textContent(), /custom-level/);
  77. assert.deepEqual(errors, []);
  78. } finally {
  79. await page.close();
  80. }
  81. });
  82. test(`restore lifecycle guards ${width}/${locale}`, { timeout: 30000 }, async () => {
  83. const { browser, origin } = getEnvironment();
  84. for (const action of ["mode", "scope", "close", "repeat", "save", "id"]) {
  85. const page = await browser.newPage({ viewport: { width, height: 1000 } });
  86. let receive, saveRequest;
  87. const request = () =>
  88. new Promise((resolve) => {
  89. receive = resolve;
  90. });
  91. await page.route("**/resolve?*", (route) => receive(route));
  92. await page.route("**/mutation", (route) => {
  93. saveRequest = route;
  94. });
  95. try {
  96. await page.goto(
  97. `${origin}fixture?${new URLSearchParams({ editor: "1", locale, theme })}`,
  98. );
  99. const restore = page.getByRole("button", {
  100. name: locale === "zh-CN" ? "重置表单" : "Reset form",
  101. exact: true,
  102. });
  103. const toggle = page.locator('[data-model-recommended-config] [role="switch"]');
  104. const capacity = page.locator('[data-model-settings-group="tokens"] input').first();
  105. if (action === "id") await toggle.click();
  106. await capacity.fill("333333");
  107. const first = request();
  108. await restore.click();
  109. const obsolete = await first;
  110. if (action === "mode") await toggle.click();
  111. if (action === "scope")
  112. await page.getByTestId("external-scope").evaluate((node) => node.click());
  113. if (action === "close")
  114. await page
  115. .getByRole("button", { name: locale === "zh-CN" ? "取消" : "Cancel", exact: true })
  116. .click();
  117. if (action === "id")
  118. await page.locator("[data-model-identity-row] input").fill("new-identity");
  119. if (action === "repeat") {
  120. const second = request();
  121. await restore.click();
  122. await (
  123. await second
  124. ).fulfill({
  125. contentType: "application/json",
  126. body: JSON.stringify({
  127. inheritedConfig: { properties: { contextWindow: 222222 } },
  128. issues: [],
  129. }),
  130. });
  131. await page.waitForFunction(
  132. () =>
  133. JSON.parse(document.querySelector('[data-testid="editor-draft"]').textContent)
  134. .contextWindowValue === "",
  135. );
  136. }
  137. if (action === "save") {
  138. await page
  139. .getByRole("button", { name: locale === "zh-CN" ? "保存" : "Save", exact: true })
  140. .click();
  141. for (const control of [restore, toggle]) assert.equal(await control.isDisabled(), true);
  142. }
  143. const before = await page.getByTestId("editor-draft").textContent();
  144. await obsolete.fulfill(
  145. locale === "zh-CN"
  146. ? { status: 500, body: "obsolete failure" }
  147. : {
  148. contentType: "application/json",
  149. body: JSON.stringify({
  150. inheritedConfig: { properties: { contextWindow: 999999 } },
  151. issues: [],
  152. }),
  153. },
  154. );
  155. await page.waitForTimeout(50);
  156. assert.equal(await page.getByTestId("editor-draft").textContent(), before, action);
  157. assert.equal(await page.getByRole("alert").count(), 0, action);
  158. if (action === "save") {
  159. assert.ok(saveRequest);
  160. await saveRequest.fulfill({ status: 500, body: "save failed" });
  161. await page.getByRole("alert").waitFor();
  162. assert.equal(await capacity.inputValue(), "333333");
  163. }
  164. } finally {
  165. await page.close();
  166. }
  167. }
  168. });
  169. test(`seven help entries and header smart placement ${width}/${locale}`, async () => {
  170. const { browser, origin, artifacts } = getEnvironment();
  171. const page = await browser.newPage({
  172. viewport: { width, height: 1000 },
  173. hasTouch: width === 390,
  174. });
  175. const errors = [],
  176. writes = [];
  177. page.on("pageerror", (error) => errors.push(String(error)));
  178. await page.route("**/mutation", async (route) => {
  179. writes.push(route.request().postDataJSON());
  180. await route.fulfill({ body: "{}" });
  181. });
  182. try {
  183. await page.goto(`${origin}fixture?${new URLSearchParams({ editor: "1", locale, theme })}`);
  184. const help = page.locator("button[data-model-help]");
  185. await help.first().waitFor();
  186. assert.equal(await help.count(), 7);
  187. await page.locator("[data-model-advanced-trigger]").click();
  188. assert.equal(
  189. await help.evaluateAll((nodes) => nodes.every((node) => !node.closest("label"))),
  190. true,
  191. "help buttons must not take the input label association",
  192. );
  193. for (const input of await page.locator("input[id]").all()) {
  194. const id = await input.getAttribute("id");
  195. const label = page.locator(`label[for=${JSON.stringify(id)}]`);
  196. await label.click();
  197. assert.equal(await input.evaluate((node) => node === document.activeElement), true);
  198. }
  199. assert.equal(await page.locator('[data-model-help="modelId"]').count(), 0);
  200. assert.equal(await page.locator('[data-model-settings-footer] [role="switch"]').count(), 0);
  201. assert.equal(await page.locator('[data-model-identity-row] [role="switch"]').count(), 0);
  202. const idBox = await page.locator("[data-model-identity-row] input").boundingBox();
  203. const smartBox = await page.locator("[data-model-recommended-config]").boundingBox();
  204. assert.ok(smartBox.y + smartBox.height <= idBox.y);
  205. const before = await page.getByTestId("editor-draft").textContent();
  206. for (const field of await help.evaluateAll((nodes) =>
  207. nodes.map((node) => node.dataset.modelHelp),
  208. )) {
  209. const trigger = page.locator(`[data-model-help="${field}"]`);
  210. if (width === 390) await trigger.tap();
  211. else await trigger.hover();
  212. const popup = page.locator(`[data-model-help-content="${field}"]`);
  213. await popup.waitFor({ timeout: 500 });
  214. await page.waitForTimeout(150);
  215. assert.ok((await popup.innerText()).length > 15);
  216. const box = await popup.boundingBox();
  217. assert.ok(
  218. box.x >= 0 &&
  219. box.x + box.width <= width + 1 &&
  220. box.y >= 0 &&
  221. box.y + box.height <= 1001,
  222. );
  223. if (field === "reasoningLevelsOrdered")
  224. assert.ok((await popup.locator("strong").innerText()).length > 0);
  225. if (field === "reasoningLevelMapping")
  226. assert.equal(await popup.locator("code").innerText(), "reasoningLevel");
  227. await page.keyboard.press("Escape");
  228. }
  229. const keyboard = page.locator('[data-model-help="followRecommendedConfig"]');
  230. // 智能帮助现为最后一个:Escape 关闭弹层后焦点仍在该按钮,先 Tab 离开再验证键盘重入。
  231. await page.keyboard.press("Tab");
  232. await page.mouse.move(0, 0);
  233. await keyboard.scrollIntoViewIfNeeded();
  234. await keyboard.focus();
  235. await page
  236. .locator('[data-model-help-content="followRecommendedConfig"]')
  237. .waitFor({ timeout: 500 });
  238. await page.waitForTimeout(150);
  239. const finalPopup = await page
  240. .locator('[data-model-help-content="followRecommendedConfig"]')
  241. .boundingBox();
  242. assert.ok(finalPopup.y >= 0 && finalPopup.y + finalPopup.height <= 1001);
  243. await page.screenshot({ path: `${artifacts}/editor-help-${locale}.png` });
  244. assert.equal(await page.getByTestId("editor-draft").textContent(), before);
  245. assert.deepEqual(writes, []);
  246. assert.deepEqual(errors, []);
  247. } finally {
  248. await page.close();
  249. }
  250. });
  251. test(`restore is atomic and ignores obsolete intent ${width}/${locale}`, async () => {
  252. const { browser, origin } = getEnvironment();
  253. const page = await browser.newPage({ viewport: { width, height: 1000 } });
  254. const writes = [],
  255. errors = [];
  256. let nextRequest;
  257. page.on("pageerror", (error) => errors.push(String(error)));
  258. await page.route("**/resolve?*", (route) => {
  259. nextRequest?.(route);
  260. });
  261. await page.route("**/mutation", async (route) => {
  262. writes.push(route.request().postDataJSON());
  263. await route.fulfill({ body: "{}" });
  264. });
  265. const request = () =>
  266. new Promise((resolve) => {
  267. nextRequest = resolve;
  268. });
  269. const response = (contextWindow) => ({
  270. contentType: "application/json",
  271. body: JSON.stringify({
  272. inheritedConfig: {
  273. properties: { contextWindow },
  274. optionSpecs: {
  275. reasoningLevel: { values: ["high"], map: "{}" },
  276. maxOutputTokens: { max: 1000 },
  277. },
  278. },
  279. issues: [],
  280. }),
  281. });
  282. try {
  283. await page.goto(`${origin}fixture?${new URLSearchParams({ editor: "1", locale, theme })}`);
  284. const restore = page.getByRole("button", {
  285. name: locale === "zh-CN" ? "重置表单" : "Reset form",
  286. exact: true,
  287. });
  288. const capacity = page.locator('[data-model-settings-group="tokens"] input').first();
  289. const toggle = page.locator('[data-model-recommended-config] [role="switch"]');
  290. await capacity.fill("987654");
  291. const first = request();
  292. await restore.click({ timeout: 2000 });
  293. const pending = await first;
  294. assert.equal(await capacity.inputValue(), "987654", "do not clear before response");
  295. await pending.fulfill(response(222222));
  296. await capacity.waitFor();
  297. await page.waitForFunction(
  298. () =>
  299. JSON.parse(document.querySelector('[data-testid="editor-draft"]').textContent)
  300. .contextWindowValue === "",
  301. );
  302. assert.equal(await capacity.getAttribute("placeholder"), "222222");
  303. assert.equal(await toggle.getAttribute("aria-checked"), "true");
  304. assert.deepEqual(writes, []);
  305. const second = request();
  306. await restore.click();
  307. const obsolete = await second;
  308. await capacity.fill("444444");
  309. await obsolete.fulfill(response(999999));
  310. await page.waitForTimeout(50);
  311. assert.equal(await capacity.inputValue(), "444444");
  312. assert.equal(await capacity.getAttribute("placeholder"), "222222");
  313. const third = request();
  314. await restore.click();
  315. await (await third).fulfill({ status: 500, body: "failed" });
  316. await page.getByRole("alert").waitFor();
  317. assert.equal(await capacity.inputValue(), "444444");
  318. assert.deepEqual(writes, []);
  319. assert.deepEqual(errors, []);
  320. } finally {
  321. await page.close();
  322. }
  323. });
  324. test(`manual editable fields and pending cancellation ${width}/${locale}`, async () => {
  325. const { browser, origin } = getEnvironment();
  326. const page = await browser.newPage({ viewport: { width, height: 1000 } });
  327. const requests = [],
  328. writes = [],
  329. errors = [];
  330. let received;
  331. const requested = new Promise((resolve) => {
  332. received = resolve;
  333. });
  334. page.on("pageerror", (error) => errors.push(String(error)));
  335. await page.route("**/resolve?*", async (route) => {
  336. requests.push(new URL(route.request().url()).searchParams.get("id"));
  337. received(route);
  338. });
  339. await page.route("**/mutation", async (route) => {
  340. writes.push(route.request().postDataJSON());
  341. await route.fulfill({ body: "{}" });
  342. });
  343. try {
  344. await page.goto(`${origin}fixture?${new URLSearchParams({ editor: "1", locale, theme })}`);
  345. const id = page.locator("[data-model-identity-row] input");
  346. const toggle = page.locator('[data-model-recommended-config] [role="switch"]');
  347. await id.fill("");
  348. await toggle.click();
  349. assert.equal(await toggle.getAttribute("aria-checked"), "false");
  350. assert.deepEqual(requests, []);
  351. await page
  352. .getByRole("button", {
  353. name: locale === "zh-CN" ? "重置表单" : "Reset form",
  354. exact: true,
  355. })
  356. .click();
  357. assert.equal(await toggle.getAttribute("aria-checked"), "true");
  358. assert.equal(await id.inputValue(), "");
  359. assert.deepEqual(requests, [], "empty restore must not resolve an empty model ID");
  360. assert.deepEqual(writes, [], "restore only changes the editor draft");
  361. await toggle.click();
  362. await id.fill("new-model");
  363. await toggle.click();
  364. await id.focus();
  365. await id.blur();
  366. await page.waitForFunction(
  367. () =>
  368. JSON.parse(document.querySelector('[data-testid="editor-draft"]').textContent)
  369. .idValue === "new-model",
  370. );
  371. const held = await Promise.race([
  372. requested,
  373. new Promise((_, reject) =>
  374. setTimeout(() => reject(new Error("missing resolution request")), 5000),
  375. ),
  376. ]);
  377. await toggle.click();
  378. const before = await page.getByTestId("editor-draft").textContent();
  379. await held.fulfill(
  380. locale === "zh-CN"
  381. ? { status: 500, body: "failed" }
  382. : {
  383. contentType: "application/json",
  384. body: JSON.stringify({
  385. inheritedConfig: {
  386. properties: { contextWindow: 999999 },
  387. optionSpecs: { reasoningLevel: { values: ["late"], map: "{}" } },
  388. },
  389. issues: [],
  390. }),
  391. },
  392. );
  393. await page.waitForTimeout(50);
  394. assert.equal(await page.getByTestId("editor-draft").textContent(), before);
  395. assert.equal(await toggle.getAttribute("aria-checked"), "false");
  396. assert.deepEqual(writes, []);
  397. assert.deepEqual(errors, []);
  398. } finally {
  399. await page.close();
  400. }
  401. });
  402. }
  403. }