覆盖 src/utils/ 下所有可独立单元测试的纯函数。这些函数无外部依赖,输入输出确定性强,是测试金字塔的底层基石。
| 文件 | 状态 | 关键导出 |
|---|---|---|
src/utils/array.ts |
已有测试 | intersperse, count, uniq |
src/utils/set.ts |
已有测试 | difference, intersects, every, union |
src/utils/xml.ts |
待测 | escapeXml, escapeXmlAttr |
src/utils/hash.ts |
待测 | djb2Hash, hashContent, hashPair |
src/utils/stringUtils.ts |
待测 | escapeRegExp, capitalize, plural, firstLineOf, countCharInString, normalizeFullWidthDigits, normalizeFullWidthSpace, safeJoinLines, truncateToLines, EndTruncatingAccumulator |
src/utils/semver.ts |
待测 | gt, gte, lt, lte, satisfies, order |
src/utils/uuid.ts |
待测 | validateUuid, createAgentId |
src/utils/format.ts |
待测 | formatFileSize, formatSecondsShort, formatDuration, formatNumber, formatTokens, formatRelativeTime, formatRelativeTimeAgo |
src/utils/json.ts |
待测 | safeParseJSON, safeParseJSONC, parseJSONL, addItemToJSONCArray |
src/utils/truncate.ts |
待测 | truncatePathMiddle, truncateToWidth, truncateStartToWidth, truncateToWidthNoEllipsis, truncate, wrapText |
src/utils/diff.ts |
待测 | adjustHunkLineNumbers, getPatchFromContents |
src/utils/frontmatterParser.ts |
待测 | parseFrontmatter, splitPathInFrontmatter, parsePositiveIntFromFrontmatter, parseBooleanFrontmatter, parseShellFrontmatter |
src/utils/file.ts |
待测(纯函数部分) | convertLeadingTabsToSpaces, addLineNumbers, stripLineNumberPrefix, pathsEqual, normalizePathForComparison |
src/utils/glob.ts |
待测(纯函数部分) | extractGlobBaseDirectory |
src/utils/tokens.ts |
待测 | getTokenCountFromUsage |
src/utils/path.ts |
待测(纯函数部分) | containsPathTraversal, normalizePathForConfigKey |
src/utils/__tests__/xml.test.ts& → &< → <> → >" 和 ' 保持原样"" → """hello" 原样返回<a & b> → <a & b>" → ", ' → 'he said "hi" 正确转义it's 正确转义src/utils/__tests__/hash.test.tssrc/utils/__tests__/stringUtils.test.ts. → \\.* → \\*[ → \\[.*+?^${}()|[]\ 全部转义hello 原样new RegExp(escapeRegExp('a.b')) 精确匹配 a.b"foo" → "Foo""fooBar" → "FooBar"(区别于 lodash capitalize)"a" → "A""" → """Foo" → "Foo"plural(1, 'file') → 'file'plural(0, 'file') → 'files'plural(3, 'file') → 'files'plural(2, 'entry', 'entries') → 'entries'"a\nb\nc" → "a""hello" → "hello""" → """\nhello" → ""countCharInString("aabac", "a") → 3countCharInString("hello", "x") → 0countCharInString("", "a") → 0countCharInString("aaba", "a", 2) → 1"0123" → "0123""0123" → "0123""port 8080" → "port 8080""\u3000" → " ""a\u3000b\u3000c" → "a b c"["a","b"] → "a,b"...[truncated][] → """\n" 时按行连接…src/utils/__tests__/semver.test.ts1.0.0-beta < 1.0.0satisfies('1.2.3', '^1.0.0') → truesatisfies('2.0.0', '^1.0.0') → falsesatisfies('1.0.0', '1.0.0') → trueorder('1.0.0', '2.0.0') → -1order('1.0.0', '1.0.0') → 0order('2.0.0', '1.0.0') → 1src/utils/__tests__/uuid.test.ts'550e8400-e29b-41d4-a716-446655440000' → 返回 UUID'not-a-uuid' → null'' → nullasrc/utils/__tests__/format.test.ts500 → "500 bytes"1536 → "1.5KB"1572864 → "1.5MB"1610612736 → "1.5GB"1024 → "1KB" (不是 "1.0KB")1234 → "1.2s"0 → "0.0s"5000 → "5s"65000 → "1m 5s"3661000 → "1h 1m 1s"90061000 → "1d 1h 1m"0 → "0s"3600000 + hideTrailingZeros → "1h"3661000 + mostSignificantOnly → "1h"1321 → "1.3k"900 → "900"1500 → "1.5k" (不是 "1.5K")1000 → "1k" (不是 "1.0k")1500 → "1.5k""1h ago" (narrow style)"in 1h" (narrow style)"0s ago"src/utils/__tests__/json.test.ts'{"a":1}' → { a: 1 }'not json' → nullnull → nullundefined → null"" → null// 注释的 JSON 正确解析'{"a":1}\n{"b":2}' → [{a:1}, {b:2}]"" → [][1, 2] + 3 → [1, 2, 3]"" + item → [item]'"hello"' + item → [item]"[]" + item → [item]src/utils/__tests__/diff.test.ts[] → []src/utils/__tests__/frontmatterParser.test.ts"" 正确处理"a.ts, b.ts" → ["a.ts", "b.ts"]"*.{ts,tsx}" → ["*.ts", "*.tsx"]["a.ts", "b.ts"] → ["a.ts", "b.ts"]5 → 5-1 → undefined"abc" → undefined1.5 → undefinedtrue → true"true" → truefalse → false"yes", 1 → false"zsh" → undefinedsrc/utils/__tests__/file.test.ts"\thello" → " hello""\t\thello" → " hello""a\tb" 保持原样N\t 前缀"1\thello" → "hello"" 1\thello" → "hello"src/utils/__tests__/glob.test.ts"src/**/*.ts" → { baseDir: "src", relativePattern: "**/*.ts" }"*.ts" → { baseDir: ".", relativePattern: "*.ts" }"src/utils/model/*.ts" → baseDir 为 "src/utils/model""C:\\Users\\**\\*.ts" 正确分割src/utils/__tests__/tokens.test.ts{ input_tokens: 100, output_tokens: 50 } → 150src/utils/__tests__/path.test.ts"../etc/passwd" → true"foo/../../bar" → true"src/utils/file.ts" → false"foo..bar" → false"src\\utils" → "src/utils"本计划中的函数大部分为纯函数,不需要 mock。少数例外:
| 函数 | 依赖 | 处理 |
|---|---|---|
hashContent / hashPair |
Bun.hash |
Bun 运行时下自动可用 |
formatRelativeTime |
Date |
使用 now 参数注入确定性时间 |
safeParseJSON |
logError |
可通过 shouldLogError: false 跳过 |
safeParseJSONC |
logError |
mock logError 避免测试输出噪音 |