@buaa_smat/hometrans 0.1.19 → 0.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/agents/code-reviewer.md +1 -1
- package/agents/self-tester.md +33 -354
- package/agents/test-case-generation-generator.md +1 -1
- package/dist/cli/config-store.js +4 -84
- package/dist/cli/config.js +6 -8
- package/dist/cli/init.js +29 -36
- package/dist/cli/uninstall.js +2 -2
- package/env-requirements.json +5 -4
- package/package.json +3 -5
- package/resource/project_architecture.svg +1 -1
- package/skills/hmos-convert-pipeline/SKILL.md +31 -21
- package/skills/hmos-incremental-ui-align/README.md +5 -5
- package/skills/hmos-incremental-ui-align/SKILL.md +2 -2
- package/skills/hmos-integration-test/README.md +58 -17
- package/skills/hmos-integration-test/SKILL.md +107 -40
- package/{tools/test-tools/autotest/engine → skills/hmos-integration-test/scripts}/report-tool.ts +71 -53
- package/skills/hmos-integration-test/scripts/resolve-metadata-tool.ts +125 -0
- package/skills/hmos-integration-test/scripts/self-test-runner.ts +756 -0
- package/{tools/test-tools/autotest/engine → skills/hmos-integration-test/scripts}/testcases-tool.ts +38 -45
- package/skills/hmos-test-case-generation/references/contract.md +3 -3
- package/tools/test-tools/autotest/config.ts +0 -175
- package/tools/test-tools/autotest/deps/autotest-agent-0.1.1.tgz +0 -0
- package/tools/test-tools/autotest/docs/README.md +0 -266
- package/tools/test-tools/autotest/docs/testcases_schema.md +0 -143
- package/tools/test-tools/autotest/engine/batch-launcher.ts +0 -327
- package/tools/test-tools/autotest/engine/self-test-runner.ts +0 -961
- package/tools/test-tools/autotest/paths.ts +0 -41
- package/tools/test-tools/autotest/resolve-metadata-tool.ts +0 -143
- package/tools/test-tools/autotest/runner/logger.ts +0 -45
- package/tools/test-tools/autotest/runner/process-utils.ts +0 -41
- package/tools/test-tools/autotest/validate.ts +0 -167
package/{tools/test-tools/autotest/engine → skills/hmos-integration-test/scripts}/testcases-tool.ts
RENAMED
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generate or validate testcases.json for the Self-Testing Agent.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* CLI (no subcommands — positional args + flags):
|
|
5
|
+
* testcases-tool.ts <input-json> <output-json> [--validate]
|
|
6
|
+
* Read `_extracted.json` ({bundle_name, app_name, cases[]}) and write
|
|
7
|
+
* `testcases.json` (array of {case_name, bundle_name, app_name, test_steps}),
|
|
8
|
+
* composing test_steps strictly as "动作:..." / "预期结果:..." and
|
|
9
|
+
* rewriting app_name → bundle_name. With --validate, re-check the output.
|
|
5
10
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* validate <testcases-json>
|
|
12
|
-
* Check a `testcases.json` (array of cases) against the schema in
|
|
13
|
-
* `testcases_schema.md`. Exit 0 = PASSED, exit 1 = FAILED.
|
|
11
|
+
* testcases-tool.ts --check-only <testcases-json>
|
|
12
|
+
* Validate an existing testcases.json (array of cases) against the
|
|
13
|
+
* in-file rules (FORBIDDEN_STRINGS / REQUIRED_LINES / TEST_STEPS_TEMPLATE).
|
|
14
|
+
* Exit 0 = PASSED, exit 1 = FAILED.
|
|
14
15
|
*
|
|
15
16
|
* The forbidden-strings and section-header rules live in this file as the
|
|
16
|
-
* single source of truth —
|
|
17
|
+
* single source of truth — generation enforces them at write time, validation
|
|
17
18
|
* re-checks them on existing files.
|
|
18
19
|
*/
|
|
19
20
|
import fs from 'node:fs';
|
|
20
21
|
import path from 'node:path';
|
|
21
|
-
import { fileURLToPath } from 'node:url';
|
|
22
|
-
import { Command } from 'commander';
|
|
23
22
|
|
|
24
23
|
const FORBIDDEN_STRINGS = [
|
|
25
24
|
'前置条件',
|
|
@@ -76,7 +75,7 @@ function composeTestSteps(
|
|
|
76
75
|
const rewrittenExpected = rewriteAppName(expectedResults, appName, bundleName);
|
|
77
76
|
// Use function replacements so '$' sequences ($&, $$, $1, ...) in the action /
|
|
78
77
|
// expected text are inserted literally — String.replace with a STRING argument
|
|
79
|
-
// would interpret them
|
|
78
|
+
// would interpret them.
|
|
80
79
|
let testSteps = TEST_STEPS_TEMPLATE.replace('{actions}', () => rewrittenActions).replace(
|
|
81
80
|
'{expected_results}',
|
|
82
81
|
() => rewrittenExpected,
|
|
@@ -106,7 +105,7 @@ function composeTestSteps(
|
|
|
106
105
|
export function generate(inputData: InputData): OutputCase[] {
|
|
107
106
|
const { bundle_name: bundleName, app_name: appName, cases } = inputData;
|
|
108
107
|
// Fail loudly on malformed extractor input instead of silently writing
|
|
109
|
-
// `undefined`/dropped keys
|
|
108
|
+
// `undefined`/dropped keys.
|
|
110
109
|
const missingTop = (['bundle_name', 'app_name', 'cases'] as const).filter(
|
|
111
110
|
(k) => inputData[k] === undefined,
|
|
112
111
|
);
|
|
@@ -207,40 +206,34 @@ function cmdValidate(filepath: string): void {
|
|
|
207
206
|
console.log(`VALIDATION PASSED: all ${cases.length} cases have correct test_steps format`);
|
|
208
207
|
}
|
|
209
208
|
|
|
210
|
-
|
|
211
|
-
export function main(): void {
|
|
212
|
-
const program = new Command();
|
|
213
|
-
program.description(
|
|
214
|
-
'Generate or validate testcases.json for the Self-Testing Agent.',
|
|
215
|
-
);
|
|
209
|
+
// ---- CLI ----
|
|
216
210
|
|
|
217
|
-
|
|
218
|
-
.command('generate')
|
|
219
|
-
.description('Generate compliant testcases.json from an extracted input')
|
|
220
|
-
.argument('<input>', 'Path to the extracted input JSON')
|
|
221
|
-
.argument('<output>', 'Path to write the compliant testcases.json')
|
|
222
|
-
.option('--validate', 'After writing, re-validate the output against the schema')
|
|
223
|
-
.action((input: string, output: string, opts: { validate?: boolean }) => {
|
|
224
|
-
cmdGenerate(input, output, !!opts.validate);
|
|
225
|
-
});
|
|
211
|
+
const args = process.argv.slice(2);
|
|
226
212
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
.description('Validate an existing testcases.json against the schema')
|
|
230
|
-
.argument('<path>', 'Path to the testcases.json to validate')
|
|
231
|
-
.action((filepath: string) => {
|
|
232
|
-
cmdValidate(filepath);
|
|
233
|
-
});
|
|
213
|
+
if (args.includes('-h') || args.includes('--help')) {
|
|
214
|
+
console.log(`Usage: testcases-tool.ts <input> <output> [--validate]
|
|
234
215
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
216
|
+
Arguments:
|
|
217
|
+
<input> Path to the extracted input JSON (_extracted.json)
|
|
218
|
+
<output> Path to write testcases.json
|
|
219
|
+
|
|
220
|
+
Options:
|
|
221
|
+
--validate After writing, re-validate the output
|
|
222
|
+
--check-only <path> Validate an existing testcases.json without generating
|
|
223
|
+
`);
|
|
224
|
+
process.exit(0);
|
|
239
225
|
}
|
|
240
226
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
227
|
+
if (args.includes('--check-only')) {
|
|
228
|
+
// Validate mode
|
|
229
|
+
const idx = args.indexOf('--check-only');
|
|
230
|
+
const filepath = args[idx + 1];
|
|
231
|
+
if (!filepath) { console.error('Usage: testcases-tool.ts --check-only <path>'); process.exit(1); }
|
|
232
|
+
cmdValidate(filepath);
|
|
233
|
+
} else {
|
|
234
|
+
// Generate mode (default)
|
|
235
|
+
const input = args.find((a) => !a.startsWith('-'));
|
|
236
|
+
const output = args.filter((a) => !a.startsWith('-')).find((a) => a !== input);
|
|
237
|
+
if (!input || !output) { console.error('Usage: testcases-tool.ts <input> <output> [--validate]'); process.exit(1); }
|
|
238
|
+
cmdGenerate(input, output, args.includes('--validate'));
|
|
246
239
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# TCG 契约 · 四诚实 + 7 硬约束 + 用例格式
|
|
1
|
+
# TCG 契约 · 四诚实 + 7 硬约束 + 用例格式
|
|
2
2
|
|
|
3
3
|
> 三方共享的唯一契约:`test-case-generation-generator`(照此写用例) / `test-case-generation-validator`(照此读、判语义) / `tools/validate.ts`(照此扫结构)。
|
|
4
4
|
> **用例格式 = 现有真实产出的格式**(`前置条件 / 动作 / 预期结果 / 测试点`),不另造字段;删 `scenarios.json`,证据就是「规范写法本身」活在 `test_case.md` 里。
|
|
@@ -213,13 +213,13 @@
|
|
|
213
213
|
|
|
214
214
|
## 4. 前置用例段体格式 + 资源复用原则
|
|
215
215
|
|
|
216
|
-
> `pre_test_case.md` 与 test_case.md 不同档:它不是 `### Scenario` 字段块,而是「段 + 单行操作链」。这一节是 pre_test_case.md 的**唯一权威格式定义**;generator 写它、
|
|
216
|
+
> `pre_test_case.md` 与 test_case.md 不同档:它不是 `### Scenario` 字段块,而是「段 + 单行操作链」。这一节是 pre_test_case.md 的**唯一权威格式定义**;generator 写它、hmos-integration-test skill 切它、validator 读它(输入 `pre-test-case-path`)、validate.ts 的 `md` 子命令扫它,四处指这一节。改这里必须同步 validate.ts 顶部 `PRE_SEG_HEADER` 旁的镜像正则(见 §4 末「代码镜像」提示)。
|
|
217
217
|
|
|
218
218
|
### 前置用例段体格式(pre_test_case.md 的唯一权威格式)
|
|
219
219
|
|
|
220
220
|
前置用例(`pre_test_case.md`)用于把 test_case.md 中标注「(见前置用例)」的**应用内累积态前提**外置成一套可先行执行的准备序列。每次测试前会重新卸载并重装 HAP,应用处于**全新安装**状态(无歌曲、无歌单、空库)。
|
|
221
221
|
|
|
222
|
-
下游 `
|
|
222
|
+
下游 `hmos-integration-test` skill 按段切片注入 `testcases.json`,由 `batch_runner.js` **按段逐条执行**;单条 case 执行超时 `CASE_TIMEOUT_MS = 10*60*1000`(10 分钟)。段间应用会 force-stop 重启,但**数据持久**(写入持久存储的歌曲/歌单/成员关系跨段保留)。因此前置用例必须按语义阶段拆成多段、单段不超时。
|
|
223
223
|
|
|
224
224
|
**段体格式(逐字遵守)**:
|
|
225
225
|
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AutoTest config loader — TS port of `self_test_runner.py:load_autotest_config`
|
|
3
|
-
* and `materialize_config` (lines 408–538).
|
|
4
|
-
*
|
|
5
|
-
* Resolution chain (matches Python exactly):
|
|
6
|
-
* 1. Explicit `--config <path>` (caller passes through)
|
|
7
|
-
* 2. `HOMETRANS_CONFIG` env var
|
|
8
|
-
* 3. `HOMETRANS_TOOL_PATH` reversed into `<toolPath>/../config.json`
|
|
9
|
-
* (i.e. `<parent of tools>/config.json` — kept for parity, though
|
|
10
|
-
* in practice this rarely matches the real `~/.hometrans/config.json`)
|
|
11
|
-
* 4. Default `~/.hometrans/config.json`
|
|
12
|
-
*
|
|
13
|
-
* Then applies the standard model-key overlay:
|
|
14
|
-
* - `HOMETRANS_MODEL_API_KEY` overrides
|
|
15
|
-
* `unified_model.api_key` and any `execute_model`/`decision_model` api_key
|
|
16
|
-
* in layered mode.
|
|
17
|
-
* - `HOMETRANS_MODEL_NAME` / `HOMETRANS_MODEL_BASE_URL` override the same
|
|
18
|
-
* fields on `unified_model` only (matches Python).
|
|
19
|
-
* - `provider` is never taken from env (always config.json, default openai).
|
|
20
|
-
*
|
|
21
|
-
* Validates every model endpoint has a real api_key (placeholder = missing).
|
|
22
|
-
* Defaults `agent_mode` to "single" when absent.
|
|
23
|
-
*/
|
|
24
|
-
import fs from 'node:fs';
|
|
25
|
-
import os from 'node:os';
|
|
26
|
-
import path from 'node:path';
|
|
27
|
-
|
|
28
|
-
// Inlined from src/cli/config-store.ts so this file is self-contained (Node
|
|
29
|
-
// resolves imports physically, not via tsconfig rootDirs, so a relative hop
|
|
30
|
-
// into src/ would miss at runtime).
|
|
31
|
-
function expandHome(p: string): string {
|
|
32
|
-
if (!p) return p;
|
|
33
|
-
if (p === '~') return os.homedir();
|
|
34
|
-
if (p.startsWith('~/') || p.startsWith('~\\')) return path.join(os.homedir(), p.slice(2));
|
|
35
|
-
return p;
|
|
36
|
-
}
|
|
37
|
-
function getConfigPath(): string {
|
|
38
|
-
return path.join(os.homedir(), '.hometrans', 'config.json');
|
|
39
|
-
}
|
|
40
|
-
function isPlaceholderApiKey(key: string | undefined): boolean {
|
|
41
|
-
const k = (key ?? '').trim();
|
|
42
|
-
return !k || k === 'YOUR_API_KEY_HERE';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const API_KEY_PLACEHOLDER = 'YOUR_API_KEY_HERE';
|
|
46
|
-
export const DEFAULT_AGENT_MODE = 'single';
|
|
47
|
-
|
|
48
|
-
const MODEL_KEYS = ['unified_model', 'execute_model', 'decision_model'] as const;
|
|
49
|
-
|
|
50
|
-
/** A model endpoint block (one of unified/execute/decision). */
|
|
51
|
-
export interface AutotestModel {
|
|
52
|
-
name?: string;
|
|
53
|
-
base_url?: string;
|
|
54
|
-
api_key?: string;
|
|
55
|
-
provider?: string;
|
|
56
|
-
temperature?: number;
|
|
57
|
-
top_p?: number;
|
|
58
|
-
frequency_penalty?: number;
|
|
59
|
-
[k: string]: unknown;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Shape of the `autotest` block inside `~/.hometrans/config.json`. */
|
|
63
|
-
export interface AutotestConfigBlock {
|
|
64
|
-
unified_model?: AutotestModel;
|
|
65
|
-
execute_model?: AutotestModel;
|
|
66
|
-
decision_model?: AutotestModel;
|
|
67
|
-
device?: Record<string, unknown>;
|
|
68
|
-
agent?: Record<string, unknown>;
|
|
69
|
-
agent_mode?: string;
|
|
70
|
-
[k: string]: unknown;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export class AutotestConfigError extends Error {}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Locate the config file path following the documented precedence chain.
|
|
77
|
-
* `explicit` (from `--config`) wins; then env; then default.
|
|
78
|
-
*/
|
|
79
|
-
export function findHometransConfig(explicit?: string): string {
|
|
80
|
-
if (explicit) return path.resolve(expandHome(explicit));
|
|
81
|
-
const envCfg = process.env.HOMETRANS_CONFIG;
|
|
82
|
-
if (envCfg) return path.resolve(expandHome(envCfg));
|
|
83
|
-
const toolPath = process.env.HOMETRANS_TOOL_PATH;
|
|
84
|
-
if (toolPath) {
|
|
85
|
-
const cand = path.resolve(expandHome(toolPath), '..', 'config.json');
|
|
86
|
-
if (fs.existsSync(cand)) return cand;
|
|
87
|
-
}
|
|
88
|
-
return getConfigPath();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Iterate over the model endpoints that actually exist in the block. */
|
|
92
|
-
function* modelDicts(block: AutotestConfigBlock): Generator<[string, AutotestModel]> {
|
|
93
|
-
for (const key of MODEL_KEYS) {
|
|
94
|
-
const m = block[key];
|
|
95
|
-
if (m && typeof m === 'object') yield [key, m];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Load + validate the autotest block. Throws `AutotestConfigError` with a
|
|
101
|
-
* human-readable message on any failure (file missing / block missing /
|
|
102
|
-
* api_key still placeholder). The returned block has `agent_mode` defaulted
|
|
103
|
-
* to "single" and env-var overlays applied.
|
|
104
|
-
*/
|
|
105
|
-
export function loadAutotestConfig(explicit?: string): AutotestConfigBlock {
|
|
106
|
-
const configPath = findHometransConfig(explicit);
|
|
107
|
-
if (!fs.existsSync(configPath)) {
|
|
108
|
-
throw new AutotestConfigError(
|
|
109
|
-
`未找到 HomeTrans 配置文件: ${configPath}\n` +
|
|
110
|
-
`请先运行 \`ht init\` 生成 ~/.hometrans/config.json(其中包含 autotest 配置)。`,
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
let parsed: unknown;
|
|
115
|
-
try {
|
|
116
|
-
parsed = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
117
|
-
} catch (e) {
|
|
118
|
-
throw new AutotestConfigError(
|
|
119
|
-
`无法解析配置文件 ${configPath}: ${(e as Error).message}`,
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const autotest = (parsed as { autotest?: AutotestConfigBlock })?.autotest;
|
|
124
|
-
if (!autotest || typeof autotest !== 'object') {
|
|
125
|
-
throw new AutotestConfigError(
|
|
126
|
-
`配置文件 ${configPath} 缺少 \`autotest\` 配置。请运行 \`ht init\` 重新生成。`,
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Deep copy so we never mutate the in-memory parsed object.
|
|
131
|
-
const block: AutotestConfigBlock = JSON.parse(JSON.stringify(autotest));
|
|
132
|
-
|
|
133
|
-
// --- Env var overlay (env wins over config.json) -----------------------
|
|
134
|
-
const envApiKey = process.env.HOMETRANS_MODEL_API_KEY?.trim() || '';
|
|
135
|
-
const envName = process.env.HOMETRANS_MODEL_NAME?.trim() || '';
|
|
136
|
-
const envBaseUrl = process.env.HOMETRANS_MODEL_BASE_URL?.trim() || '';
|
|
137
|
-
|
|
138
|
-
if (block.unified_model) {
|
|
139
|
-
if (envApiKey) block.unified_model.api_key = envApiKey;
|
|
140
|
-
if (envName) block.unified_model.name = envName;
|
|
141
|
-
if (envBaseUrl) block.unified_model.base_url = envBaseUrl;
|
|
142
|
-
}
|
|
143
|
-
// api_key is shared across all endpoints when env provides it.
|
|
144
|
-
if (envApiKey) {
|
|
145
|
-
for (const [, m] of modelDicts(block)) m.api_key = envApiKey;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// --- Validate: every endpoint must have a real api_key -----------------
|
|
149
|
-
const missing: string[] = [];
|
|
150
|
-
const endpoints = [...modelDicts(block)];
|
|
151
|
-
if (endpoints.length === 0) {
|
|
152
|
-
throw new AutotestConfigError(
|
|
153
|
-
`配置文件 ${configPath} 的 autotest 未定义任何模型端点` +
|
|
154
|
-
`(unified_model / execute_model / decision_model)。`,
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
for (const [name, m] of endpoints) {
|
|
158
|
-
const cur = (m.api_key ?? '').trim();
|
|
159
|
-
if (!cur || cur === API_KEY_PLACEHOLDER || isPlaceholderApiKey(cur)) {
|
|
160
|
-
missing.push(`${name}.api_key`);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
if (missing.length > 0) {
|
|
164
|
-
throw new AutotestConfigError(
|
|
165
|
-
`autotest 配置中以下 api_key 仍为占位符/空:${missing.join(', ')}。\n` +
|
|
166
|
-
`请在 ${configPath} 的 autotest 配置里填入真实 api_key,` +
|
|
167
|
-
`或设置 OS 环境变量 HOMETRANS_MODEL_API_KEY 后重试(可运行 \`ht init\` 一并配置)。`,
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Default agent_mode = single (matches Python `setdefault`).
|
|
172
|
-
if (!block.agent_mode) block.agent_mode = DEFAULT_AGENT_MODE;
|
|
173
|
-
|
|
174
|
-
return block;
|
|
175
|
-
}
|
|
Binary file
|
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
# Self-Test Runner
|
|
2
|
-
|
|
3
|
-
HarmonyOS 应用自测执行器。负责把自然语言写的测试用例(`test_case.md`)跑在真机上,产出可读的测试报告。底层引擎是 Node.js 的 `@autotest/agent`,通过 `new AutoTestAgent({...})` 编程式构造。Agent 编排由 `self-tester`(统一的自测 agent,通过 `setup` 布尔参数控制是否生成用例)与 `self-test-fixer`(可选,按报告修复 HarmonyOS 代码)完成。
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## 一、一次性准备
|
|
8
|
-
|
|
9
|
-
每台机器装好插件后做一次即可。
|
|
10
|
-
|
|
11
|
-
### 1. 工具依赖
|
|
12
|
-
|
|
13
|
-
确认下列工具在 `PATH` 中:
|
|
14
|
-
|
|
15
|
-
| 工具 | 用途 | 验证命令 |
|
|
16
|
-
|------|------|----------|
|
|
17
|
-
| `node` | 运行 `@autotest/agent` 引擎与 HomeTrans CLI(≥ 22.18) | `node -v` |
|
|
18
|
-
| `npm` | 安装 `@autotest/agent`(仅在 `npm install` 时需要,运行时不调用) | `npm -v` |
|
|
19
|
-
| `hdc` | 设备连接 / 安装 HAP | `hdc version` |
|
|
20
|
-
|
|
21
|
-
> `@autotest/agent` 是 HomeTrans 的直接依赖(根 `package.json` 里 `"@autotest/agent": "file:tools/test-tools/autotest/deps/autotest-agent-0.1.1.tgz"`)。在 HomeTrans 根目录执行一次 `npm install` 即把它(及其 `sharp` / `ai` 等依赖)装进根 `node_modules`,运行时 `batch-launcher.js` 通过 `require('@autotest/agent')` 直接加载。
|
|
22
|
-
|
|
23
|
-
### 2. 配置 `autotest`(在 `~/.hometrans/config.json` 中)
|
|
24
|
-
|
|
25
|
-
AutoTest 配置集中存放在 HomeTrans 全局配置
|
|
26
|
-
`~/.hometrans/config.json` 的 **`autotest`** 字段(与 `env`、`editors` 同级)。
|
|
27
|
-
|
|
28
|
-
```jsonc
|
|
29
|
-
{
|
|
30
|
-
"editors": [ /* ... */ ],
|
|
31
|
-
"env": { /* ... */ },
|
|
32
|
-
"autotest": {
|
|
33
|
-
"unified_model": {
|
|
34
|
-
"name": "",
|
|
35
|
-
"base_url": "",
|
|
36
|
-
"api_key": "YOUR_API_KEY_HERE",
|
|
37
|
-
"provider": "openai",
|
|
38
|
-
"temperature": 0.1,
|
|
39
|
-
"top_p": 0.9,
|
|
40
|
-
"frequency_penalty": 0.5
|
|
41
|
-
},
|
|
42
|
-
"device": { "device_sn": null, "ip": "127.0.0.1", "port": 8710, "device_log_level": "debug" },
|
|
43
|
-
"agent": { "max_steps": 50, "verbose": true, "lang": "zh", "enable_preprocess": false, "debug_mode": null }
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
- `ht init` 的 **模型配置** 环节会展示该块默认内容并提示填写 `api_key`(及 `name`/`base_url`/`provider`),写入 `~/.hometrans/config.json` 的 `autotest.unified_model`,并导出为 `HOMETRANS_MODEL_*` 环境变量(与 UI 对齐共用同一个模型)。
|
|
49
|
-
- 模型字段解析链路「环境变量 → config.json → 报错」:`self-test-runner.js` 跑用例前用 OS 环境变量 `HOMETRANS_MODEL_API_KEY` / `HOMETRANS_MODEL_NAME` / `HOMETRANS_MODEL_BASE_URL` 覆盖 config.json 里 `unified_model` 的对应字段(api_key 兼容老版本 `TEST_API_KEY` 作兜底;`provider` 不导出为环境变量,始终取 config.json,默认 openai);覆盖后 api_key 仍是占位符/空才报错退出。
|
|
50
|
-
- **运行时配置注入**:`self-test-runner.js` 读取该块后,把 `unified_model`(api_key/name/base_url/provider)、`device`(device_sn/ip/port)、`agent_mode` 映射成 `AUTOTEST_*` 环境变量,传给后台进程 `batch-launcher.js`;后者用这些值构造 `new AutoTestAgent({ apiKey, modelName, baseUrl, provider, deviceSn, ip, port, agentMode, ... })`。缺 `agent_mode` 时默认补 `"single"`。
|
|
51
|
-
- 默认单层模式只需填 `unified_model.api_key`;如需双层模式,可在 `autotest` 块里手动加 `execute_model` / `decision_model`(各自含 `api_key`)并把 `agent_mode` 设为 `"layered"`。
|
|
52
|
-
|
|
53
|
-
> **Prompt 版本**:`SingleAgent` 从 `config.model.unified.promptType` 取 prompt 版本(zod 默认 `'default'` 会被拼成无效的 `'vdefault'`)。`batch-launcher.js` 通过构造器的 `configOverrides: { model: { unified: { promptType: <version> } } }` 注入它,默认 `v2.0`(可选 `v3.0` / `v1.0`),可用环境变量 `AUTOTEST_PROMPT_VERSION` 覆盖。
|
|
54
|
-
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
## 二、准备测试输入
|
|
58
|
-
|
|
59
|
-
在某个目录(下文记为 `<case-dir>`)下放置:
|
|
60
|
-
|
|
61
|
-
| 文件 | 必需 | 说明 |
|
|
62
|
-
|------|------|------|
|
|
63
|
-
| `test_case.md` | 是 | 常规测试用例集,多条 `### Scenario:` 块 |
|
|
64
|
-
| `pre_test_case.md` | 否 | 前置用例(如首启授权、新手引导);可写 0 / 1 / 多条 scenario |
|
|
65
|
-
|
|
66
|
-
格式约定:每条 scenario 写两行
|
|
67
|
-
|
|
68
|
-
```markdown
|
|
69
|
-
### Scenario: 冷启动初始化后正常进入主页面
|
|
70
|
-
- 动作:打开应用 → 等待 5 秒
|
|
71
|
-
- 预期结果:文件夹网格主页面成功展示文件夹列表
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
并且需要提供 `<project-dir>`(HarmonyOS 工程根,含 `AppScope/app.json5`)—— `self-tester` 通过 `ht autotest selftest resolve-metadata` 据此解析 `bundleName` 与 `app_name`。
|
|
75
|
-
|
|
76
|
-
---
|
|
77
|
-
|
|
78
|
-
## 三、Agent 流水线
|
|
79
|
-
|
|
80
|
-
### 1. `self-tester`:解析用例并执行真机测试
|
|
81
|
-
|
|
82
|
-
读取 `test_case.md`(首轮)与 `pre_test_case.md`(可选)→ 写入 `<output-path>/testcases.json` 和 `<output-path>/app-metadata.json` → 检测真机 → 安装 HAP → 跑 AutoTest → 渲染 `<output-path>/self-test-report.md`。前置用例自动以 `[PRE] ` 前缀置顶,按源文件顺序排列。`setup` 布尔参数控制是否运行解析阶段:
|
|
83
|
-
|
|
84
|
-
| 参数 | 是否必填 | 说明 |
|
|
85
|
-
|------|----------|------|
|
|
86
|
-
| `hap-path` | 必填 | 待测签名包:单个 `.hap`/`.hsp` 文件、目录,或逗号分隔的多个文件/目录(entry HAP + 应用内 HSP / feature HAP,可分布在不同目录),所有 `.hap`/`.hsp` 汇总后一次事务安装 |
|
|
87
|
-
| `output-path` | 必填 | 产物目录(JSON、报告、`task/` 都写在此根目录) |
|
|
88
|
-
| `project-dir` | `setup=true` 时必填 | HarmonyOS 工程根目录(含 `AppScope/app.json5`),用于解析 `bundle_name` / `app_name` |
|
|
89
|
-
| `test-case-path` | `setup=true` 时必填 | `test_case.md` 路径 |
|
|
90
|
-
| `pre-test-case-path` | 可选 | 前置用例文件路径;不传时自动从 `test-case-path` 同目录查找 |
|
|
91
|
-
| `setup` | 可选,默认 `true` | `true` = 解析 + 测试;`false` = 仅测试,要求 `<output-path>/testcases.json` 与 `<output-path>/app-metadata.json` 已存在 |
|
|
92
|
-
|
|
93
|
-
产出:
|
|
94
|
-
|
|
95
|
-
- `<output-path>/testcases.json` — 用例数组(`setup=true` 时写入)
|
|
96
|
-
- `<output-path>/app-metadata.json` — `{ bundle_name, app_name, project_root }`(`setup=true` 时写入)
|
|
97
|
-
- `<output-path>/_extracted.json` — 用例提取的中间文件(`setup=true` 时写入)
|
|
98
|
-
- `<output-path>/self-test-report.md` — 自测报告(每次都写)
|
|
99
|
-
- `<output-path>/task/task_<timestamp>/` — AutoTest 原始产物(每条用例的报告、日志、截图)
|
|
100
|
-
|
|
101
|
-
> **多轮迭代测试**:首轮用 `setup=true`,第二轮起用 `setup=false` 复用首轮产出的两个 JSON,不重复解析 `test_case.md`。
|
|
102
|
-
|
|
103
|
-
执行前请先确认设备已连接:
|
|
104
|
-
|
|
105
|
-
```bash
|
|
106
|
-
hdc list targets
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
agent 通过 `ht autotest selftest run` 完成前 4 步(1-3 同步、4 后台启动):
|
|
110
|
-
|
|
111
|
-
1. 读取并校验 `autotest` 配置(`~/.hometrans/config.json`),把 api_key/model/device 映射成 `AUTOTEST_*` 环境变量
|
|
112
|
-
2. `hdc uninstall` + `hdc install -r`(多个 `.hap`/`.hsp` 一次事务安装)
|
|
113
|
-
3. 把用例 JSON 规整为 JSONL(补 `uuid` / `spec`)
|
|
114
|
-
4. 后台启动 `node batch-launcher.js`,后者 `new AutoTestAgent({...})` 构造 agent 并逐条 `runTask`
|
|
115
|
-
|
|
116
|
-
随后 agent 用 `ht autotest selftest status` 每 60 秒轮询一次直到 `COMPLETED`,再调 `ht autotest report generate` 把 `task_<ts>/` 产物渲染成 `self-test-report.md`。报告把前置用例与常规用例分两节渲染(`## 前置用例` / `## 用例详情`),由模板生成,不依赖 LLM 现写。
|
|
117
|
-
|
|
118
|
-
`<output-path>/task/task_<timestamp>/` 会保存 batch 运行期间的中间产物(每条用例的 `report_dir` 子目录、`task_results.jsonl`、`summary.json`)—— `batch-launcher.js` 启动时 cwd 即此目录,所有产物都落在该任务子目录下。
|
|
119
|
-
|
|
120
|
-
### 2. `self-test-fixer`:按报告修复 HarmonyOS 代码(可选)
|
|
121
|
-
|
|
122
|
-
入参就是上一步的 `self-test-report.md`。当 `self-tester` 报告里有 FAIL 时再跑这一步。
|
|
123
|
-
|
|
124
|
-
---
|
|
125
|
-
|
|
126
|
-
## 四、直接调用(不通过 agent)
|
|
127
|
-
|
|
128
|
-
agent 内部用的就是这几个工具,开发自测时可以直接调 —— `ht` 已在 PATH 中(`ht init` 后),无需解析安装路径:
|
|
129
|
-
|
|
130
|
-
```bash
|
|
131
|
-
# 解析工程元数据(bundle_name / app_name → app-metadata.json)
|
|
132
|
-
ht autotest resolve-metadata \
|
|
133
|
-
--project-dir <harmonyos-project-root> \
|
|
134
|
-
--output <out>/app-metadata.json
|
|
135
|
-
|
|
136
|
-
# 启动一次自测(agent 主路径:阻塞直到终态或 --timeout 超时;超时自动 kill batch)
|
|
137
|
-
# Compute N = case count, then timeout = N * 720 (per-case 10min cap + 20% margin)
|
|
138
|
-
ht autotest selftest run \
|
|
139
|
-
--testcases <path-to-testcases.json> \
|
|
140
|
-
--hap <path-to-app.hap> \
|
|
141
|
-
--bundle-name com.example.xxx \
|
|
142
|
-
--category <app_name> \
|
|
143
|
-
--task-dir <out>/task \
|
|
144
|
-
--output-dir <out> \
|
|
145
|
-
--timeout $((N * 720))
|
|
146
|
-
|
|
147
|
-
# 不带 --timeout:立即返回 RUNNING JSON + pid(向后兼容;agent 不再使用此模式)
|
|
148
|
-
# 人类 ad-hoc 轮询(agent 不再调用;改用 run --timeout)
|
|
149
|
-
ht autotest selftest status --task-dir <out>/task --output-dir <out>
|
|
150
|
-
|
|
151
|
-
# 强行终止后台 batch(用户主动取消时使用;正常 --timeout 超时会自动 kill)
|
|
152
|
-
ht autotest selftest kill --task-dir <out>/task
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
> 若要绕过 `ht` 直接调编译后的 JS:在 HomeTrans 仓库根目录(开发态)执行 `node dist/test-tools/autotest/<script>.js <args…>`(`dist/` 需先 `npm run build` 生成);安装态则从 `ht` 的实际安装位置定位 `dist/test-tools/autotest/`。
|
|
156
|
-
|
|
157
|
-
### `report-tool` — 单独重渲染报告
|
|
158
|
-
|
|
159
|
-
batch 跑完后 agent 会自动调一次 report-tool;本地调试想重渲染某次历史 task:
|
|
160
|
-
|
|
161
|
-
```bash
|
|
162
|
-
ht autotest report generate \
|
|
163
|
-
--task-subdir <out>/task/task_<ts> \
|
|
164
|
-
--app-metadata <out>/app-metadata.json \
|
|
165
|
-
--hap <hap-path> \
|
|
166
|
-
--device 127.0.0.1:5555 \
|
|
167
|
-
--suite "<title from test_case.md>" \
|
|
168
|
-
--out <out>/self-test-report.md \
|
|
169
|
-
--validate
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
也可以只校验一份已有的 `self-test-report.md`:
|
|
173
|
-
|
|
174
|
-
```bash
|
|
175
|
-
ht autotest report validate <out>/self-test-report.md
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
读取 `summary.json` + `task_results.jsonl` + 每个 case 的结构化运行日志(解析 AutoTest planner 写入的 `<judgment>` 块;目录结构不符预期时,`loadCaseJudgment` 会优雅降级为用 `reason` 兜底),用模板渲染整份报告。完全无 LLM 调用。
|
|
179
|
-
|
|
180
|
-
参数说明:
|
|
181
|
-
|
|
182
|
-
| 参数 | 含义 |
|
|
183
|
-
|------|------|
|
|
184
|
-
| `--testcases` | JSON 数组或 JSONL 均可,旧 JSON 数组会被自动转 JSONL |
|
|
185
|
-
| `--hap` | 待测签名包:单个 `.hap`/`.hsp` 文件、目录、或逗号分隔的多个;所有 `.hap`/`.hsp` 一次事务安装(至少一个 `.hap`) |
|
|
186
|
-
| `--bundle-name` | HAP 包名,用于 `hdc uninstall` |
|
|
187
|
-
| `--category` | 测试分类标签,会传给 batch-launcher,建议直接用 `app_name` |
|
|
188
|
-
| `--task-dir` | 任务工作目录,脚本会在内部创建 `task_<timestamp>` 子目录 |
|
|
189
|
-
| `--output-dir` | 日志目录(`self_test_*.log`) |
|
|
190
|
-
| `--config` | 指定 HomeTrans 配置文件(`config.json`)路径;默认 `~/.hometrans/config.json`,从中读取 `autotest` 配置 |
|
|
191
|
-
|
|
192
|
-
`run --timeout` / `status` 子命令的 JSON 响应字段:
|
|
193
|
-
|
|
194
|
-
| 字段 | 含义 |
|
|
195
|
-
|------|------|
|
|
196
|
-
| `status` | `RUNNING`(仅 status / 无 --timeout 的 run)/ `COMPLETED` / `CRASHED` / `NOT_STARTED` / `TIMEOUT`(仅 run --timeout 超时)/ `FAILED`(仅 run --timeout spawn 失败) |
|
|
197
|
-
| `pid` | batch PID |
|
|
198
|
-
| `cases_done` | 已完成的用例数(来自 `task_results.jsonl` 行数,或 `summary.json.total_cases`) |
|
|
199
|
-
| `pass_count` / `fail_count` / `unknown_count` | 仅 `COMPLETED` 时给出 |
|
|
200
|
-
| `pass_rate` | 仅 `COMPLETED`,字符串如 `"30.00%"`(原样来自 `summary.json`) |
|
|
201
|
-
| `last_case` | `RUNNING`/`CRASHED`/`TIMEOUT` 时给出(最近一条 `task_results.jsonl` 行的 `case_name`) |
|
|
202
|
-
| `task_subdir` | `task_<timestamp>` 绝对路径,AutoTest 产物都在里面 |
|
|
203
|
-
| `log_tail` | runner 日志最近 5 行 |
|
|
204
|
-
| `error` | 仅 `FAILED` 时给出(spawn 错误消息) |
|
|
205
|
-
|
|
206
|
-
Exit codes(`run --timeout` 与 `status` 共用):
|
|
207
|
-
|
|
208
|
-
| Code | Status |
|
|
209
|
-
|------|--------|
|
|
210
|
-
| 0 | `COMPLETED` |
|
|
211
|
-
| 1 | `FAILED`(`run --timeout` 任何阶段失败:prep 或 spawn) |
|
|
212
|
-
| 2 | `RUNNING`(仅 `status` 单次探针) |
|
|
213
|
-
| 3 | `CRASHED` |
|
|
214
|
-
| 4 | `NOT_STARTED` |
|
|
215
|
-
| 5 | `TIMEOUT`(仅 `run --timeout`) |
|
|
216
|
-
|
|
217
|
-
---
|
|
218
|
-
|
|
219
|
-
## 五、目录布局一览
|
|
220
|
-
|
|
221
|
-
```
|
|
222
|
-
tools/test-tools/autotest/ # 本目录
|
|
223
|
-
├── README.md # 本文件
|
|
224
|
-
├── .gitignore # 排除运行期日志等
|
|
225
|
-
├── testcases_schema.md # testcases.json 字段规范
|
|
226
|
-
└── deps/autotest-agent-0.1.1.tgz # @autotest/agent 打包快照(被根 package.json 以 file: 引用)
|
|
227
|
-
|
|
228
|
-
# 引擎入口脚本(TypeScript 源码 → 编译进 HomeTrans dist/):
|
|
229
|
-
# 源码: src/test-tools/autotest/{self-test-runner, batch-launcher, testcases-tool, report-tool}.ts
|
|
230
|
-
# 编译后:dist/test-tools/autotest/{self-test-runner, batch-launcher, testcases-tool, report-tool}.js
|
|
231
|
-
# @autotest/agent 本体装在根 node_modules/@autotest/agent/dist/(由根 npm install 安装)。
|
|
232
|
-
#
|
|
233
|
-
# 运行时 self-test-runner.js 经 HOMETRANS_TOOL_PATH → ~/.hometrans/config.json → 向上查找 解析到本目录,
|
|
234
|
-
# 但只是用作 cwd/产物定位;引擎本身由 batch-launcher.js 通过 require('@autotest/agent') 从根 node_modules 加载。
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
---
|
|
238
|
-
|
|
239
|
-
## 六、常见问题
|
|
240
|
-
|
|
241
|
-
**Q: `autotest 配置缺失 / api_key 没填`**
|
|
242
|
-
A: 跑 `ht init` 配置统一模型,它会写入 `~/.hometrans/config.json` 的 `autotest.unified_model` 并导出 `HOMETRANS_MODEL_*` 环境变量;或手动把 `autotest.unified_model.api_key` 改成真实 key;或设置 OS 环境变量 `HOMETRANS_MODEL_API_KEY`(兼容老版本 `TEST_API_KEY`)后重试。
|
|
243
|
-
|
|
244
|
-
**Q: `require('@autotest/agent')` 找不到 / `batch-launcher` 启动即 FAILED**
|
|
245
|
-
A: `@autotest/agent` 没装进根 `node_modules`。在 HomeTrans 根目录跑一次 `npm install`(它会把 `tools/test-tools/autotest/deps/autotest-agent-0.1.1.tgz` 安装为 `@autotest/agent`)。确认 `node_modules/@autotest/agent/dist/index.js` 存在。
|
|
246
|
-
|
|
247
|
-
**Q: 报 `Prompt version 'vdefault' not found`**
|
|
248
|
-
A: `SingleAgent` 从 `model.unified.promptType` 取 prompt 版本,默认 `'default'` 会被拼成无效的 `'vdefault'`。`batch-launcher.js` 已通过构造器 `configOverrides` 默认注入 `v2.0`;如需换版本,设环境变量 `AUTOTEST_PROMPT_VERSION=v3.0`(可选 `v1.0`/`v2.0`/`v3.0`)。
|
|
249
|
-
|
|
250
|
-
**Q: 测试过程被打断,下次跑会冲突吗?**
|
|
251
|
-
A: 不会。`ht autotest selftest run` 启动时会读取上一次的 pid 文件,若其记录的 batch 进程仍存活则先杀掉;`task/` 目录在 agent 流程里也会被清空。带 `--timeout` 超时也会自动 kill batch,无需手动清理。若 agent 的后台 bash 被用户取消但 batch 仍在跑,调用 `ht autotest selftest kill --task-dir <out>/task` 立即释放设备。
|
|
252
|
-
|
|
253
|
-
**Q: 想彻底重置一遍本地状态再跑?**
|
|
254
|
-
A: 删掉 `<output>/task/` 残留(`task_<ts>/`、`batch.pid`、`batch_stdout.log`)以及 `<output>/self_test_*.log` 即可。引擎依赖在根 `node_modules`,无需在本目录重装。
|
|
255
|
-
|
|
256
|
-
**Q: 想自己改 `testcases.json` 后再跑?**
|
|
257
|
-
A: 改完后跑一遍校验,再交给 `self-tester`(`setup=false`):
|
|
258
|
-
|
|
259
|
-
```bash
|
|
260
|
-
ht autotest testcases validate <path-to-testcases.json>
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
只有打印 `VALIDATION PASSED` 才能用。
|
|
264
|
-
|
|
265
|
-
**Q: 报告里 `AutoTest 任务路径` 指向哪?**
|
|
266
|
-
A: 每条用例的 AutoTest 执行目录(报告 + 日志 + 截图),路径形如 `<output>/task/task_<ts>/<uuid>_<case_name>/`。报告里所有 case 都必须带这个字段。
|