Skip to content

fix(ut): 修复5个失败的单元测试用例,补全语音记事本单测首批 - #447

Merged
pengfeixx merged 1 commit into
linuxdeepin:develop/snipefrom
pengfeixx:ut-fix-failing-tests
Aug 5, 2026
Merged

fix(ut): 修复5个失败的单元测试用例,补全语音记事本单测首批#447
pengfeixx merged 1 commit into
linuxdeepin:develop/snipefrom
pengfeixx:ut-fix-failing-tests

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

背景

补充语音记事本单元测试,提升函数覆盖率至100%(Refs: V-1329)。

本批为第1批,目标:修复现有5个失败用例,确保全部用例无失败无崩溃。

修改内容(6个文件)

  1. tests/src/common/ut_metadataparser.cpp — 修复 UT_MetaDataParser_makeMetaData_001EXPECT_EQ 类型不匹配(const char* vs QVariant),改用 QByteArray 比较。

  2. tests/src/task/ut_exportnoteworker.cpp — 修复 UT_ExportNoteWorker_exportAsHtml_001:导出路径 test 目录未创建导致写文件失败。SetUp 创建目录,TearDown 清理。

  3. tests/src/importolddata/tiptapmigration/ut_migrationjsonbuilder.cpp — 修复 GeneratedEnvelopePassesSchemaValidator:node 验证器导入 voice-block.css?inline 失败,添加 CSS inline loader。

  4. tests/src/importolddata/tiptapmigration/ut_migrationnotedataconverter.cpp — 修复 ComparesGoldenJsonAndPassesValidatorsConvertsVoiceBlockGoldenAndPassesValidators:同上 CSS 导入问题。

  5. web-editor/scripts/css-inline-loader.mjs(新增)— Node.js ESM loader 入口,注册 CSS 加载钩子,使 validate-envelope.mjs 可在无 bundler 环境下导入含 .css?inline 的模块。

  6. web-editor/scripts/css-loader-hook.mjs(新增)— ESM load 钩子,将 CSS 导入返回为空字符串默认导出。

验证结果

  • 编译通过(Debug 模式)
  • 全部 444 个测试用例通过,0 失败 0 崩溃
  • 函数覆盖率:63.9%(661/1034)— 本批仅修复失败用例,后续批次将补充新测试提升覆盖率

后续计划

后续将分批次(每批5~7个文件)补充缺失测试,逐步提升函数覆盖率至100%,等待本批合入后再继续。

Summary by Sourcery

Fix failing unit tests for metadata parsing, note export, and tiptap migration validators, and introduce a Node.js CSS loader hook to allow schema validation scripts to run without a bundler.

Bug Fixes:

  • Ensure UT_ExportNoteWorker tests create and clean up the test export directory to prevent file write failures.
  • Align UT_MetaDataParser expectations with the QVariant metadata representation to avoid type mismatches in assertions.
  • Update tiptap migration validator tests to invoke Node.js with a custom import loader for CSS-inlined modules so schema validation processes complete successfully.

Enhancements:

  • Add a Node.js ESM CSS loader hook and loader entry script so validation scripts can import Vite-style CSS inline modules under plain Node.js.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "web-editor/scripts/css-loader-hook.mjs": {
            "b": [
                "// string for the CSS module default export.",
                "export async function load(url, context, nextLoad) {",
                "      source: 'export default \"\"',"
            ]
        }
    }
}

@sourcery-ai

sourcery-ai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fixes five failing unit tests around metadata parsing, note export, and tiptap migration validation by correcting a QByteArray/QVariant comparison, ensuring the test export directory exists and is cleaned up, and enabling Node-based schema validators to import CSS inline modules via custom ESM loader hooks.

File-Level Changes

Change Details Files
Ensure UT_ExportNoteWorker tests can write export files by preparing and cleaning the test directory.
  • Create the "test" output directory in SetUp using QDir::mkpath before export runs.
  • Remove the "test" directory recursively in TearDown to avoid leftover artifacts.
  • Include QDir header to support filesystem operations.
tests/src/task/ut_exportnoteworker.cpp
Fix metadata parser test comparison by aligning types when checking generated JSON metadata.
  • Convert metadata QString to QByteArray for comparison.
  • Use QByteArrayLiteral for the expected JSON value to match the QByteArray type.
tests/src/common/ut_metadataparser.cpp
Allow Node-based migration JSON builder tests to run schema validators that import CSS inline modules.
  • Invoke node with the --import flag to load a custom CSS inline loader before validate-envelope.mjs.
  • Adjust QProcess argument list to include css-inline-loader.mjs when running the validator.
tests/src/importolddata/tiptapmigration/ut_migrationjsonbuilder.cpp
Ensure migration note data converter tests can run validators that depend on CSS inline imports.
  • Run node with --import ./web-editor/scripts/css-inline-loader.mjs when invoking validate-envelope.mjs.
  • Update expectPassesValidators helper to pass the new arguments to QProcess.
tests/src/importolddata/tiptapmigration/ut_migrationnotedataconverter.cpp
Introduce a Node ESM loader hook that neutralizes CSS (including ?inline) imports for validators.
  • Add css-loader-hook.mjs that intercepts .css imports and returns an empty default export module.
  • Implement load() hook that short-circuits CSS module loading while delegating others to nextLoad.
web-editor/scripts/css-loader-hook.mjs
Provide an entrypoint for node --import to register the CSS loader hook for schema validation scripts.
  • Add css-inline-loader.mjs that registers css-loader-hook.mjs via node:module register().
  • Resolve the loader hook path relative to the current module using import.meta.url and pathToFileURL.
web-editor/scripts/css-inline-loader.mjs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

- ut_metadataparser: 修复 EXPECT_EQ 类型不匹配(QVariant vs const char*)
- ut_exportnoteworker: SetUp 创建 test 目录,TearDown 清理
- ut_migrationjsonbuilder/ut_migrationnotedataconverter: 为 node 验证器
  添加 CSS inline loader,解决 voice-block.css 导入失败

本次修改6个文件,全部444个测试用例通过,无失败无崩溃。
函数覆盖率: 63.9% (661/1034)

Refs: V-1329
@pengfeixx
pengfeixx force-pushed the ut-fix-failing-tests branch from b20c12c to 3edf1b7 Compare August 5, 2026 07:02
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "web-editor/scripts/css-loader-hook.mjs": {
            "b": [
                "// string for the CSS module default export.",
                "export async function load(url, context, nextLoad) {",
                "      source: 'export default \"\"',"
            ]
        }
    }
}

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码修复了测试用例中的类型不匹配、Node.js CSS导入兼容性及测试目录缺失问题,整体质量优秀
逻辑正确且无安全风险,因测试代码非核心业务逻辑保留5分余量

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

修复了ut_metadataparser.cpp中将QByteArray与字符串字面量直接比较的类型不匹配错误,通过QByteArrayLiteral和toByteArray()确保类型一致;修复了Node.js环境无法直接处理Vite风格CSS内联导入的问题,通过注册ESM Loader Hook正确拦截并返回空模块;修复了ut_exportnoteworker.cpp中因缺少测试目录导致文件写入失败的问题,在SetUp中增加mkpath创建目录。
建议:无

  • 2.代码质量(良好)✓

代码修改针对性强,解决了具体的测试失败问题。新增的css-inline-loader.mjs和css-loader-hook.mjs文件包含了清晰的SPDX版权头和注释,说明了其用途是替代打包器以支持纯Node.js环境运行。在ut_exportnoteworker.cpp的TearDown中增加了removeRecursively进行测试环境清理,符合测试规范。
建议:无

  • 3.代码性能(无性能问题)✓

测试代码对性能无严格要求。新增的ESM Loader Hook在匹配到.css后缀时直接通过shortCircuit: true短路返回,避免了不必要的后续加载开销,处理逻辑高效。
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码安全无风险。QProcess调用均使用start而非shell命令拼接,参数通过QStringLiteral硬编码或由内部函数生成,不存在命令注入风险。ESM Loader Hook中的url.includes('.css')匹配逻辑仅限于本地测试脚本的Node.js进程内,且仅返回空字符串,不涉及文件读写或代码执行,无安全风险。

  • 建议:无

■ 【改进建议代码示例】

// web-editor/scripts/css-loader-hook.mjs
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

// ESM loader hook: intercepts Vite-style CSS inline imports so that the
// validator can run under plain Node.js (no bundler). Returns an empty
// string for the CSS module default export.
export async function load(url, context, nextLoad) {
  // 使用 URL 对象解析以提升匹配严谨性,避免对包含 .css 的非标准协议 URI 产生误判
  try {
    const urlObj = new URL(url);
    if (urlObj.pathname.endsWith('.css')) {
      return {
        format: 'module',
        source: 'export default ""',
        shortCircuit: true,
      };
    }
  } catch (e) {
    // 若 URL 解析失败(如无效的本地路径传递),直接放行给下一个 loader 处理
  }
  return nextLoad(url, context);
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@pengfeixx
pengfeixx merged commit 53f75df into linuxdeepin:develop/snipe Aug 5, 2026
19 of 20 checks passed
@pengfeixx
pengfeixx deleted the ut-fix-failing-tests branch August 5, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants