droboticflow/tests/frontend/slice-edit-panel-feedback.test.mjs

55 lines
2.6 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs";
import test from "node:test";
import { dictionaryKeyDown } from "../../src/lib/annotation-shortcuts.js";
const panel = fs.readFileSync(new URL("../../src/components/SliceEditPanel.tsx", import.meta.url), "utf8");
const discard = fs.readFileSync(new URL("../../src/components/DiscardReasonFields.tsx", import.meta.url), "utf8");
const dictionary = fs.readFileSync(
new URL("../../src/components/WorkspaceDictionaryPanel.tsx", import.meta.url),
"utf8",
);
test("slice edit panel exposes an in-panel dictionary trigger for the workbench shortcut dispatcher", () => {
assert.match(panel, /data-slice-edit-dictionary-trigger/);
assert.match(panel, /formatAnnotationTime\(target\[field\]\)/);
assert.match(
panel,
/aria-label=\{`\$\{field === "start" \? "首帧" : "尾帧"\}\$\{exactFrames \? "帧号" : "时间(秒)"\}`\}/,
);
assert.match(panel, /role="alert"[\s\S]*?data-testid="slice-edit-panel-error"/);
});
test("first Enter in dictionary search selects the first matching option", () => {
let selected = 0;
let prevented = false;
const option = { click: () => selected++ };
const event = {
key: "Enter",
repeat: false,
target: { matches: (selector) => selector === "input" },
currentTarget: { querySelectorAll: () => [option] },
preventDefault: () => (prevented = true),
stopPropagation: () => {},
};
dictionaryKeyDown(event, () => {});
assert.equal(prevented, true);
assert.equal(selected, 1);
assert.match(dictionary, /if \(firstOption\) onKeyboardSelect\?\.\(\)/);
assert.match(discard, /onKeyboardSelect=\{\(\) => panelRef\.current\?\.focus\(\)\}/);
});
test("discard preview Enter confirms only outside text controls and ignores IME composition", () => {
assert.match(panel, /if \(event\.nativeEvent\.isComposing \|\| event\.keyCode === 229\) return/);
assert.match(panel, /event\.key === "Enter"[\s\S]*?event\.target\.matches\("input, textarea, select, button"\)/);
assert.match(panel, /if \(!event\.repeat && reasonReady\) discard\.onConfirm\(\)/);
assert.match(panel, /disabled=\{!reasonReady \|\| saving\}/);
assert.match(panel, /discard\.loading \|\| Boolean\(discard\.error\)/);
assert.match(panel, /discard\.allowManual && !discard\.reasons\.length[\s\S]*?reason\?\.trim\(\)/);
assert.match(panel, /item\.version === discard\.draft\?\.reasonVersion/);
assert.match(panel, /requiresNote \|\| item\.name === "其他"[\s\S]*?reasonNote\?\.trim\(\)/);
assert.match(discard, /<textarea[\s\S]*?value=\{\(manual \? value\.reason : value\.reasonNote\) \|\| ""\}/);
});