refactor(web-test): этап B.5.5 — ввести returnFormState (выборочно применить)

core/helpers.mjs: returnFormState(extras) — стандартный хвост action-функций:
getFormState + Object.assign(extras) + checkForErrors → state.errors. Унифицирует
~15 hand-written копий и закрывает R1/R2/R3 (state.errors теперь добавляется
автоматически у любого пользователя хелпера).

В этом коммите конвертированы только 2 простейших P1-сайта (openCommand,
второй handle в navigateLink) — без extras между getFormState и err-проверкой.
Остальные 30+ сайтов сложнее (state.X между, разные return-shape, wrapped
fillFields) — будут мигрированы органически при переносе clickElement/
selectValue/closeForm в forms/* на этапе C.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Nick Shirokov
2026-05-25 22:42:23 +03:00
parent e215957344
commit 9ac0cb3b87
2 changed files with 25 additions and 10 deletions
+3 -9
View File
@@ -143,7 +143,7 @@ import {
_detectPlatformDialogs, _closePlatformDialogs,
} from './core/errors.mjs';
import {
safeClick, findFieldInputId, readEdd,
safeClick, findFieldInputId, readEdd, returnFormState,
detectNewForm as helperDetectNewForm,
} from './core/helpers.mjs';
// Re-export only what was publicly exported before the refactor.
@@ -215,10 +215,7 @@ export async function openCommand(name) {
if (result?.error) throw new Error(`openCommand: "${name}" not found. Available: ${result.available?.join(', ') || 'none'}`);
await waitForStable(formBefore);
const state = await getFormState();
const err = await checkForErrors();
if (err) state.errors = err;
return state;
return await returnFormState();
}
/** Switch to an open tab by name (fuzzy match). Returns updated form state. */
@@ -390,10 +387,7 @@ export async function navigateLink(url) {
}
await waitForStable(formBefore);
const state = await getFormState();
const err = await checkForErrors();
if (err) state.errors = err;
return state;
return await returnFormState();
}
/** Read current form state. Single evaluate call via combined script. */
@@ -3,7 +3,8 @@
// Source: https://github.com/Nikolay-Shirokov/cc-1c-skills
import { page } from './state.mjs';
import { dismissPendingErrors } from './errors.mjs';
import { dismissPendingErrors, checkForErrors } from './errors.mjs';
import { getFormState } from '../browser.mjs';
/**
* page.click with the standard "intercepts pointer events" retry ladder:
@@ -105,3 +106,23 @@ export async function readEdd() {
};
})()`);
}
/**
* Standard "tail" of action functions: fetch current form state, attach
* caller-specified extras (e.g. `{ clicked: {...} }`) and the result of
* `checkForErrors()` if any. Returns the flat state object.
*
* Unifies ~15 hand-written copies in clickElement, selectValue, closeForm,
* navigation functions, etc. Also closes R1/R2/R3 from the refactor plan —
* any caller using this helper gets `state.errors` for free.
*
* @param {object} [extras] — merged into the state object via Object.assign.
* @returns {Promise<object>} form state (flat) with optional `errors`.
*/
export async function returnFormState(extras = {}) {
const state = await getFormState();
Object.assign(state, extras);
const err = await checkForErrors();
if (err) state.errors = err;
return state;
}