provider-model-editor-progressive-cases.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import assert from "node:assert/strict";
  2. import { mkdir } from "node:fs/promises";
  3. import { test } from "node:test";
  4. export function registerProgressiveModelEditorCases(getEnvironment) {
  5. for (const [width, locale, theme] of [
  6. [390, "zh-CN", "light"],
  7. [1200, "en-US", "dark"],
  8. ]) {
  9. test(`Todo141 hidden reasoning validation ${width}/${theme}`, async () => {
  10. const { browser, origin } = getEnvironment();
  11. const page = await browser.newPage({
  12. viewport: { width, height: 900 },
  13. reducedMotion: "reduce",
  14. });
  15. const writes = [];
  16. await page.route("**/mutation", async (route) => {
  17. writes.push(route.request().postDataJSON());
  18. await route.fulfill({ body: "{}" });
  19. });
  20. try {
  21. await page.goto(
  22. `${origin}fixture?${new URLSearchParams({ editor: "1", emptyReasoning: "1", locale, theme })}`,
  23. );
  24. const trigger = page.locator("[data-model-advanced-trigger]");
  25. for (let attempt = 0; attempt < 2; attempt++) {
  26. await page
  27. .locator("[data-model-settings-footer]")
  28. .getByRole("button", { name: locale === "zh-CN" ? "保存" : "Save", exact: true })
  29. .click();
  30. await page.waitForFunction(() =>
  31. document.activeElement?.hasAttribute("data-model-reasoning-level-add"),
  32. );
  33. assert.equal(await trigger.getAttribute("aria-expanded"), "true");
  34. assert.deepEqual(writes, []);
  35. if (!attempt) await trigger.click();
  36. }
  37. await page.locator("[data-model-reasoning-level-add]").press("Enter");
  38. await page.locator("[data-model-reasoning-level-input]").fill("enabled");
  39. await page.locator("[data-model-reasoning-level-input]").press("Enter");
  40. await trigger.click();
  41. await page
  42. .locator("[data-model-settings-footer]")
  43. .getByRole("button", { name: locale === "zh-CN" ? "保存" : "Save", exact: true })
  44. .click();
  45. await page.locator("[data-model-settings-footer]").waitFor({ state: "hidden" });
  46. assert.equal(writes.length, 1);
  47. assert.deepEqual(writes[0].personalConfig.optionSpecs.reasoningLevel.values, ["enabled"]);
  48. } finally {
  49. await page.close();
  50. }
  51. });
  52. test(
  53. `Todo141 progressive editor and hidden-field save ${width}/${theme}`,
  54. { timeout: 30000 },
  55. async () => {
  56. const { browser, origin, artifacts } = getEnvironment();
  57. await mkdir(artifacts, { recursive: true });
  58. const page = await browser.newPage({
  59. viewport: { width, height: 900 },
  60. hasTouch: width === 390,
  61. });
  62. page.setDefaultTimeout(5000);
  63. await page.context().tracing.start({ screenshots: true, snapshots: true });
  64. const writes = [],
  65. errors = [];
  66. page.on("pageerror", (error) => errors.push(String(error)));
  67. await page.route("**/mutation", async (route) => {
  68. writes.push(route.request().postDataJSON());
  69. await route.fulfill({ body: "{}" });
  70. });
  71. try {
  72. await page.goto(
  73. `${origin}fixture?${new URLSearchParams({ editor: "1", locale, theme, ...(width === 1200 ? { edit: "1" } : {}) })}`,
  74. );
  75. const advanced = page.locator("[data-model-advanced]");
  76. const summary = advanced.locator("[data-model-advanced-trigger]");
  77. const max = page.locator("[data-model-max-output] input");
  78. const map = advanced.locator("[data-model-json-slot] textarea");
  79. const footer = page.locator("[data-model-settings-footer]");
  80. const save = footer.getByRole("button", {
  81. name: locale === "zh-CN" ? "保存" : "Save",
  82. exact: true,
  83. });
  84. await summary.waitFor();
  85. const idInput = page.locator("[data-model-identity-row] input");
  86. assert.equal((await idInput.getAttribute("readonly")) !== null, width === 1200);
  87. await page.waitForFunction(
  88. (edit) =>
  89. document.activeElement ===
  90. document.querySelector(
  91. edit
  92. ? '[data-model-settings-group="tokens"] input'
  93. : "[data-model-identity-row] input",
  94. ),
  95. width === 1200,
  96. );
  97. assert.equal(await summary.getAttribute("aria-expanded"), "false");
  98. assert.equal(await max.isVisible(), true);
  99. assert.equal(await advanced.locator("[data-model-max-output]").count(), 0);
  100. assert.equal(
  101. await page.locator("[data-model-reasoning-level-editor]").isVisible(),
  102. false,
  103. );
  104. assert.equal(await page.locator("[data-model-output-modality]").count(), 0);
  105. assert.equal(await page.getByText("MFJS", { exact: false }).count(), 0);
  106. const buttons = await footer
  107. .locator("button[data-variant]:not([data-model-help])")
  108. .allTextContents();
  109. assert.deepEqual(
  110. buttons,
  111. locale === "zh-CN" ? ["重置表单", "取消", "保存"] : ["Reset form", "Cancel", "Save"],
  112. );
  113. assert.equal(await footer.getByRole("switch").count(), 0);
  114. assert.equal(
  115. await page.locator('[data-slot="dialog-header"]').getByRole("switch").count(),
  116. 1,
  117. );
  118. assert.equal(
  119. await page
  120. .locator("[data-model-settings-group]")
  121. .evaluateAll((nodes) =>
  122. nodes.every(
  123. (node) =>
  124. getComputedStyle(node).borderTopWidth === "0px" &&
  125. getComputedStyle(node).paddingLeft === "0px",
  126. ),
  127. ),
  128. true,
  129. );
  130. const bounds = await footer.boundingBox();
  131. assert.ok(
  132. bounds.x >= 0 && bounds.x + bounds.width <= width && bounds.y + bounds.height <= 900,
  133. );
  134. await page.screenshot({ path: `${artifacts}/todo141-basic-${width}.png` });
  135. const spacing = await page.locator("[data-model-settings-scroll]").evaluate((node) => {
  136. const groups = [...node.children].slice(0, 3);
  137. return groups
  138. .slice(1)
  139. .map(
  140. (group, index) =>
  141. group.getBoundingClientRect().top - groups[index].getBoundingClientRect().bottom,
  142. );
  143. });
  144. assert.deepEqual(spacing, [16, 16]);
  145. // 基础区最大输出错误不展开高级配置,但仍把焦点交还给错误输入。
  146. await max.fill("0");
  147. await save.click();
  148. await page.waitForFunction(
  149. () =>
  150. document.activeElement === document.querySelector("[data-model-max-output] input"),
  151. );
  152. assert.equal(await summary.getAttribute("aria-expanded"), "false");
  153. assert.deepEqual(writes, []);
  154. await max.fill("");
  155. // 高级仅控制可见性,不能清草稿或丢掉原有覆盖高亮。
  156. await page.locator('[data-model-settings-group="tokens"] input').first().fill("654321");
  157. await summary.click();
  158. const textOption = advanced.locator('[data-model-input-modality="text"]');
  159. assert.equal(await textOption.isDisabled(), true);
  160. assert.equal(await textOption.getAttribute("aria-pressed"), "true");
  161. assert.equal(await textOption.locator("[data-model-option-checkbox]").count(), 1);
  162. const reasoning = advanced.locator('[data-model-settings-group="reasoning"]');
  163. assert.equal(await reasoning.locator("[data-model-reasoning-level-editor]").count(), 1);
  164. assert.equal(await reasoning.locator("[data-model-json-slot]").count(), 1);
  165. // 重复值在失焦时不能提交,收起再展开仍要保留该局部编辑,而非卸载丢弃。
  166. await reasoning.getByRole("button", { name: "low", exact: true }).click();
  167. const levelInput = reasoning.locator("[data-model-reasoning-level-input]");
  168. await levelInput.fill("high");
  169. await summary.click();
  170. await summary.press("Space");
  171. assert.equal(await levelInput.inputValue(), "high");
  172. await levelInput.fill("low");
  173. await levelInput.blur();
  174. await max.fill("54321");
  175. await map.fill("bad (");
  176. assert.equal(await max.getAttribute("data-personal-override"), "true");
  177. const image = advanced.locator('[data-model-input-modality="image"]');
  178. await image.click();
  179. assert.equal(await image.locator("[data-model-option-checkbox]").count(), 1);
  180. assert.equal(await image.getAttribute("data-personal-override"), "true");
  181. await summary.click();
  182. assert.equal(await max.inputValue(), "54321");
  183. assert.deepEqual(writes, []);
  184. // 收起后保存仍校验高级字段,自动展开并聚焦;重复同一错误也有效。
  185. for (let attempt = 0; attempt < 2; attempt++) {
  186. await save.click();
  187. await page.waitForFunction(
  188. () =>
  189. document
  190. .querySelector("[data-model-advanced-trigger]")
  191. .getAttribute("aria-expanded") === "true" &&
  192. document.activeElement ===
  193. document.querySelector("[data-model-json-slot] textarea"),
  194. );
  195. assert.deepEqual(writes, []);
  196. if (!attempt) await summary.click();
  197. }
  198. await map.fill("{}");
  199. await map.evaluate(async (node) => {
  200. await Promise.all(
  201. node
  202. .closest("[data-model-advanced]")
  203. .getAnimations({ subtree: true })
  204. .map((animation) => animation.finished),
  205. );
  206. });
  207. await map.scrollIntoViewIfNeeded();
  208. await page.screenshot({ path: `${artifacts}/todo141-advanced-${width}.png` });
  209. const layout = await page.locator("[data-model-settings-scroll]").evaluate((node) => ({
  210. scrollWidth: node.scrollWidth,
  211. clientWidth: node.clientWidth,
  212. }));
  213. assert.ok(layout.scrollWidth <= layout.clientWidth + 1, JSON.stringify(layout));
  214. await summary.click();
  215. await save.click();
  216. await footer.waitFor({ state: "hidden" });
  217. assert.equal(writes.length, 1);
  218. assert.equal(writes[0].personalConfig.properties.contextWindow, 654321);
  219. assert.equal(writes[0].personalConfig.optionSpecs.maxOutputTokens.max, 54321);
  220. assert.equal(writes[0].personalConfig.optionSpecs.reasoningLevel.map, "{}");
  221. assert.equal(writes[0].personalConfig.properties.inputFormat.supportsImage, false);
  222. await page.getByRole("button", { name: "Reopen editor" }).click();
  223. assert.equal(await summary.getAttribute("aria-expanded"), "false");
  224. assert.equal(await max.inputValue(), "");
  225. await page.getByRole("switch").click();
  226. await save.click();
  227. await footer.waitFor({ state: "hidden" });
  228. assert.equal(writes.length, 2);
  229. assert.equal(writes[1].useRecommendedConfig, false);
  230. assert.equal(
  231. Object.hasOwn(writes[1].personalConfig.properties, "requiresMfjsToolSchema"),
  232. false,
  233. );
  234. assert.equal(Object.hasOwn(writes[1].personalConfig.properties, "outputFormat"), false);
  235. assert.deepEqual(errors, []);
  236. await page.getByRole("button", { name: "Reopen editor" }).click();
  237. await summary.click();
  238. await max.fill("34567");
  239. await footer
  240. .getByRole("button", { name: locale === "zh-CN" ? "取消" : "Cancel", exact: true })
  241. .click();
  242. await footer.waitFor({ state: "hidden" });
  243. assert.equal(writes.length, 2);
  244. await page.getByRole("button", { name: "Reopen editor" }).click();
  245. assert.equal(await summary.getAttribute("aria-expanded"), "false");
  246. assert.equal(await max.inputValue(), "");
  247. } finally {
  248. await page.screenshot({ path: `${artifacts}/todo141-final-${width}.png` });
  249. await page.context().tracing.stop({ path: `${artifacts}/todo141-${width}.zip` });
  250. await page.close();
  251. }
  252. },
  253. );
  254. }
  255. }