diff --git a/.github/workflows/community-package.yml b/.github/workflows/community-package.yml index f8b53c0f..1f28f787 100644 --- a/.github/workflows/community-package.yml +++ b/.github/workflows/community-package.yml @@ -1,6 +1,6 @@ name: community-package -# Builds the unsigned Windows community installer: OpenTest plus the plugins from -# crysound/OpenTest-Plugins (the public business plugins and the SonoDAQ device plugin). It verifies a silent install and publishes to GitHub Releases. +# 构建 Windows、Linux、macOS 社区安装包,验证安装后统一发布。 +# 三个平台共用版本;发布任务等待所有安装验证成功。 # push to main -> daily build A.B.C-dev.[.N] (prerelease, tag created on publish) # push tag vA.B.C-rc.N -> release candidate (prerelease) # push tag vA.B.C -> release (latest) @@ -20,21 +20,66 @@ on: - tools/default-plugins.mjs - tools/package.mjs - tools/packaging.mjs + - tools/packaging-platforms.mjs + - tools/package-posix-native.mjs + - tools/package-java.mjs + - tools/macos-signing.mjs + - tools/__tests__/** - tools/publish-community-build.cjs - tools/ci/** - workflow_dispatch: - inputs: - plugins_ref: - description: OpenTest-Plugins branch or tag (default main; tag builds use the same tag) - required: false - default: '' + # Manual runs use the same plugin revision rules as push runs; they cannot select arbitrary plugin code. + workflow_dispatch: {} permissions: contents: read concurrency: group: community-package-${{ github.ref }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: + version: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + outputs: + version: ${{ steps.version.outputs.version }} + plugins-source: ${{ steps.plugins.outputs.source }} + plugins-revision: ${{ steps.plugins.outputs.revision }} + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: actions/setup-node@v7 + with: + node-version: 22 + package-manager-cache: false + - name: Test version, plugin resolution, delivery manifest and publication rules + run: node --test tools/__tests__/build-version.test.mjs tools/__tests__/resolve-plugin-revision.test.mjs tools/__tests__/ensure-delivery-manifest.test.mjs tools/__tests__/publish-community-build.test.mjs + - name: Resolve package version + id: version + env: + GH_TOKEN: ${{ github.token }} + REF: ${{ github.ref }} + REF_TYPE: ${{ github.ref_type }} + shell: pwsh + run: | + if ($env:REF_TYPE -eq 'tag') { + node tools/build-version.mjs --tag $env:REF + } else { + $base = (Get-Content -LiteralPath package.json -Raw | ConvertFrom-Json).version + $existing = Join-Path $env:RUNNER_TEMP 'dev-tags.txt' + # Write the file even when no daily tag exists yet; an empty pipeline would not create it. + $tags = @(gh api "repos/$env:GITHUB_REPOSITORY/git/matching-refs/tags/v$base-dev." --paginate --jq '.[].ref') + if ($LASTEXITCODE -ne 0) { throw 'Could not list existing daily tags.' } + [System.IO.File]::WriteAllLines($existing, [string[]]$tags) + node tools/build-version.mjs --dev --existing-file $existing + } + if ($LASTEXITCODE -ne 0) { throw 'Version resolution failed.' } + - name: Resolve public plugin revision + id: plugins + env: + REF_TYPE: ${{ github.ref_type }} + REF_NAME: ${{ github.ref_name }} + run: node tools/ci/resolve-plugin-revision.mjs build: + needs: version name: build-community-installer runs-on: windows-latest timeout-minutes: 120 @@ -48,7 +93,6 @@ jobs: PLUGIN_PACKAGES: ${{ github.workspace }}/.runtime/ci-plugin-packages NATIVE_PLUGIN_BIN: ${{ github.workspace }}/.runtime/ci-native-plugins outputs: - version: ${{ steps.version.outputs.version }} installer-name: ${{ steps.verify.outputs.installer-name }} installer-sha256: ${{ steps.verify.outputs.installer-sha256 }} artifact-id: ${{ steps.upload.outputs.artifact-id }} @@ -62,18 +106,26 @@ jobs: uses: actions/checkout@v7 with: repository: crysound/OpenTest-Plugins - ref: ${{ inputs.plugins_ref || (github.ref_type == 'tag' && github.ref_name) || 'main' }} + ref: ${{ needs.version.outputs.plugins-revision }} path: .runtime/opentest-plugins persist-credentials: false + - name: Verify public plugin revision + env: + EXPECTED_PLUGINS_REVISION: ${{ needs.version.outputs.plugins-revision }} + run: | + $actual = git -C $env:PLUGINS_ROOT rev-parse HEAD + if ($LASTEXITCODE -ne 0) { throw 'Could not read the checked-out plugin revision.' } + $actual = $actual.Trim() + if ($actual -ne $env:EXPECTED_PLUGINS_REVISION) { throw "OpenTest-Plugins revision mismatch: $actual" } - uses: actions/setup-node@v7 with: node-version: 22 package-manager-cache: false + # Do not persist Maven caches between runs. - uses: actions/setup-java@v6 with: distribution: zulu java-version: '21' - cache: maven # The host device-plugin packager runs `python` (standard library only). - uses: actions/setup-python@v7 with: @@ -83,27 +135,10 @@ jobs: corepack enable corepack prepare pnpm@10.6.5 --activate pnpm --version - - name: Resolve package version - id: version - env: - GH_TOKEN: ${{ github.token }} - REF: ${{ github.ref }} - REF_TYPE: ${{ github.ref_type }} - run: | - if ($env:REF_TYPE -eq 'tag') { - node tools/build-version.mjs --tag $env:REF - } else { - $base = (Get-Content -LiteralPath package.json -Raw | ConvertFrom-Json).version - $existing = Join-Path $env:RUNNER_TEMP 'dev-tags.txt' - # Write the file even when no daily tag exists yet; an empty pipeline would not create it. - $tags = @(gh api "repos/$env:GITHUB_REPOSITORY/git/matching-refs/tags/v$base-dev." --paginate --jq '.[].ref') - if ($LASTEXITCODE -ne 0) { throw 'Could not list existing daily tags.' } - [System.IO.File]::WriteAllLines($existing, [string[]]$tags) - node tools/build-version.mjs --dev --existing-file $existing - } - if ($LASTEXITCODE -ne 0) { throw 'Version resolution failed.' } - name: Install dependencies run: pnpm install --frozen-lockfile + - name: Test installer configuration and macOS license resources + run: node --test tools/__tests__/community-package.test.mjs tools/__tests__/mac-build-resources.test.mjs tools/__tests__/service-environment.test.mjs - name: Prepare Electron runtime run: ./tools/ci/prepare-electron.ps1 - name: Install the host plugin SDK into Maven @@ -165,7 +200,7 @@ jobs: - name: Package community installer env: OPENTEST_ENV_FILE: .runtime/ci-installer/runtime.env - VERSION: ${{ steps.version.outputs.version }} + VERSION: ${{ needs.version.outputs.version }} # CI has no Crowdin CLI or credentials; use the translations committed in the repository. MAVEN_ARGS: -ntp -Dskip.crowdin.download=true run: | @@ -191,12 +226,117 @@ jobs: dist/releases/windows-x64/test/unsigned/installer-verification.json if-no-files-found: error retention-days: 14 + build-posix: + name: build-community-${{ matrix.platform }} + needs: version + strategy: + fail-fast: false + matrix: + include: + - platform: linux + runner: ubuntu-24.04 + arch: x64 + extension: deb + - platform: mac + runner: macos-26 + arch: arm64 + extension: dmg + runs-on: ${{ matrix.runner }} + timeout-minutes: 120 + defaults: + run: + shell: bash + env: + COREPACK_ENABLE_DOWNLOAD_PROMPT: '0' + NODE_OPTIONS: --max-old-space-size=6144 + PLUGIN_PACKAGES: ${{ github.workspace }}/.runtime/ci-plugin-packages + NATIVE_PLUGIN_BIN: ${{ github.workspace }}/.runtime/ci-native-plugins + PACKAGE_PLATFORM: ${{ matrix.platform }} + VERSION: ${{ needs.version.outputs.version }} + OPENTEST_ENV_FILE: .runtime/ci-installer/runtime.env + MAVEN_ARGS: -ntp -Dskip.crowdin.download=true + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - name: Check out public plugins + uses: actions/checkout@v7 + with: + repository: crysound/OpenTest-Plugins + ref: ${{ needs.version.outputs.plugins-revision }} + path: .runtime/opentest-plugins + persist-credentials: false + - name: Verify public plugin revision + env: + EXPECTED_PLUGINS_REVISION: ${{ needs.version.outputs.plugins-revision }} + run: | + actual=$(git -C .runtime/opentest-plugins rev-parse HEAD) + test "$actual" = "$EXPECTED_PLUGINS_REVISION" + - uses: actions/setup-node@v7 + with: + node-version: 22 + package-manager-cache: false + # Do not persist Maven caches between runs. + - uses: actions/setup-java@v6 + with: + distribution: zulu + java-version: '21' + - uses: actions/setup-python@v7 + with: + python-version: '3.12' + - name: Prepare Linux dependencies + if: matrix.platform == 'linux' + run: | + sudo apt-get update + sudo apt-get install -y build-essential patchelf libasound2t64 libgtk-3-0t64 libnotify4 libnss3 libxss1 libxtst6 libatspi2.0-0t64 libpulse0 libudev1 libblkid1 libssl3t64 libhidapi-hidraw0 xdg-utils + - name: Prepare pnpm and dependencies + run: | + corepack enable + corepack prepare pnpm@10.6.5 --activate + pnpm install --frozen-lockfile + node tools/ci/prepare-electron.mjs + - name: Install the host plugin SDK into Maven + run: mvn -B --no-transfer-progress -DskipTests -f apps/backend/pom.xml -pl ':crydaq-dependencies,:opentest-plugin-api,:opentest-dashboard-standard-processors' -am install + - name: Build public plugins + run: | + names=$(node --input-type=module -e "import { editionPluginNames } from './tools/default-plugins.mjs'; console.log(editionPluginNames({ edition: 'community' }).join('\n'))") + test -n "$names" + plugins=() + while IFS= read -r name; do plugins+=("$name"); done <<< "$names" + mv .runtime/opentest-plugins "$RUNNER_TEMP/opentest-plugins" + export OPENTEST_PLUGIN_PACKAGE_OUTPUT="$PLUGIN_PACKAGES" + cd "$RUNNER_TEMP/opentest-plugins" + node tools/build.mjs "${plugins[@]}" --platform "$PACKAGE_PLATFORM" --skip-tests -Dskip.crowdin.download=true + - name: Build and stage SonoDAQ device plugin + run: node tools/ci/stage-posix-device-plugin.mjs + - name: Prepare installer settings + env: + OPENTEST_CI_NATIVE_PLUGIN_BIN_ROOT: ${{ env.NATIVE_PLUGIN_BIN }} + run: node tools/ci/prepare-installer.mjs + - name: Package community installer + run: | + node tools/package.mjs --platform "$PACKAGE_PLATFORM" --mode production --kind installer --edition community \ + --version "$VERSION" --plugins-dir "$PLUGIN_PACKAGES" --config .runtime/ci-installer/packaging.json + - name: Ensure delivery manifest + run: node tools/ci/ensure-delivery-manifest.mjs + - name: Verify silent install + run: node tools/ci/verify-posix-installer.mjs + - name: Upload installer + uses: actions/upload-artifact@v7 + with: + name: opentest-community-${{ matrix.platform }}-${{ matrix.arch }}-${{ github.sha }} + path: | + dist/releases/${{ matrix.platform }}-${{ matrix.arch }}/production/unsigned/installer/*.${{ matrix.extension }} + dist/releases/${{ matrix.platform }}-${{ matrix.arch }}/production/unsigned/delivery.json + dist/releases/${{ matrix.platform }}-${{ matrix.arch }}/production/unsigned/installer-verification.json + if-no-files-found: error + retention-days: 14 publish: name: publish-community-release - needs: build + needs: [version, build, build-posix] if: github.event_name == 'push' runs-on: ubuntu-latest - timeout-minutes: 15 + timeout-minutes: 30 permissions: contents: write actions: read @@ -209,11 +349,21 @@ jobs: - uses: actions/download-artifact@v8 with: name: opentest-community-windows-x64-${{ github.sha }} - path: release + path: release/windows + - uses: actions/download-artifact@v8 + with: + name: opentest-community-linux-x64-${{ github.sha }} + path: release/linux + - uses: actions/download-artifact@v8 + with: + name: opentest-community-mac-arm64-${{ github.sha }} + path: release/mac - name: Publish GitHub release uses: actions/github-script@v9 env: - VERSION: ${{ needs.build.outputs.version }} + VERSION: ${{ needs.version.outputs.version }} + PLUGINS_SOURCE: ${{ needs.version.outputs.plugins-source }} + PLUGINS_REVISION: ${{ needs.version.outputs.plugins-revision }} ARTIFACT_ID: ${{ needs.build.outputs.artifact-id }} ARTIFACT_DIGEST: ${{ needs.build.outputs.artifact-digest }} INSTALLER_NAME: ${{ needs.build.outputs.installer-name }} @@ -227,7 +377,10 @@ jobs: version: process.env.VERSION, artifactId: process.env.ARTIFACT_ID, artifactDigest: process.env.ARTIFACT_DIGEST, - installerPath: path.join(process.env.GITHUB_WORKSPACE, 'release/installer', process.env.INSTALLER_NAME), + installerPath: path.join(process.env.GITHUB_WORKSPACE, 'release/windows/installer', process.env.INSTALLER_NAME), installerDigest: process.env.INSTALLER_SHA256, + posixRoot: path.join(process.env.GITHUB_WORKSPACE, 'release'), + pluginsSource: process.env.PLUGINS_SOURCE, + pluginsRevision: process.env.PLUGINS_REVISION, }) core.notice(`Published ${release.html_url}`) diff --git a/CONTEXT.md b/CONTEXT.md index a14da749..07d4737b 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -5,7 +5,7 @@ 宿主默认独立构建、打包和启动,不依赖插件仓库,不要求安装外置业务或设备插件。`default-plugins.json` 只列公开插件 `fft-tools`,仅供显式套件构建;只有提供预制 `--plugins-dir` 或启用 CI 的 `bundle_plugins` 时才随附业务插件。源码启动默认忽略套件缓存,只有显式 `OPENTEST_BUNDLED_PLUGINS_DIR` 才选择该材料。插件仓库消费公开 SDK 并独立生成自己的安装包。 `apps/native` 维护公开 Device Host、IPC、SDK、portable 加载器、校验器、管理器、`common_device_runtime` 共享插件运行时及其平台实现和测试。JNI(含 Host 客户端)、算法适配、系统音频、蓝牙、Sequence 和设备控制等原生组件以预编译二进制随附,例如 `apps/desktop/packages/native-modules/vendor/` 和后端 `resources/native/`;其源码不在本仓库。 -SonoDAQ 源码在公开的 `OpenTest-Plugins/native/opentest_sonodaq_plugin` 维护;其他具体设备插件由各自独立源码和部署材料提供。公开 Host 构建不依赖具体设备插件源码或 JNI 源码。 +SonoDAQ 源码在公开的 `OpenTest-Plugins/plugins/opentest_sonodaq_plugin` 维护;其他具体设备插件由各自独立源码和部署材料提供。公开 Host 构建不依赖具体设备插件源码或 JNI 源码。 构建输出位于源码外,与后端 `resources/native/` 运行材料分开管理;构建不自动替换已部署二进制。框架边界遵循 `apps/native/AGENTS.md`,当前入口见 [公开构建说明](apps/native/docs/public-build.md)。 ## 开发工作区术语 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d4b082dc..2e2ab692 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,7 +41,11 @@ node --test tools/__tests__/docs-policy.test.mjs ## Community packages -The `community-package` workflow builds the unsigned Windows community installer: OpenTest plus the plugins from `crysound/OpenTest-Plugins`: the business plugins listed in `default-plugins.json` (currently `fft-tools`) and the SonoDAQ device plugin, which is bundled under `resources/backend/native/plugins/devices`. Each push to `main` publishes a daily prerelease `A.B.C-dev.` (later builds on the same Beijing day add `.2`, `.3` and so on). Pushing tag `vA.B.C-rc.N` or `vA.B.C` publishes a release candidate or a release; tag the same name in `OpenTest-Plugins` first. Pull requests that change packaging only build and verify the installer. `package.json` keeps the plain `A.B.C` used for plugin compatibility; the full version is passed to `node tools/package.mjs --version`. The workflow uses `--edition community`, which bundles only the plugins listed in `default-plugins.json`. +The `community-package` workflow builds three community installers: an EXE executable installer for Windows x64, a DEB Debian package for Linux x64, and a DMG disk image for macOS ARM64. They include OpenTest, the business plugins listed in `default-plugins.json` (currently `fft-tools`) from `crysound/OpenTest-Plugins`, and the SonoDAQ device plugin, but not the other plugins from the website package. Device plugins are bundled under `backend/native/plugins/devices` in the application resources. Windows retains `test` mode and uses the backend defaults without an explicit `cloud.license.url`; Linux and macOS use `production` license mode with the formal license service and separate generated configurations. There is no separate test server. Automatic updates are disabled. Windows is unsigned; macOS retains its multilingual license agreement and has only ad hoc signatures, without developer signing or Apple notarization. On first launch, choose Open Anyway in Privacy & Security. The SonoDAQ device plugin requires macOS 26 or later. + +Each push to `main` publishes a daily prerelease `A.B.C-dev.`; later builds on the same Beijing day add `.2`, `.3` and so on. Pushing `vA.B.C-rc.N` or `vA.B.C` publishes a release candidate or release. First create the same tag in `OpenTest-Plugins`: a missing tag fails the build instead of falling back to `main`. Daily builds, pull requests, and manual runs use the plugin repository's `main`; manual runs cannot select an arbitrary plugin revision. Tag runs require the matching plugin tag. All three platforms check out the same resolved commit directly and verify it again, so a moving branch cannot change the build inputs. After every build and installation check succeeds, one publishing job uploads three installers and their SHA-256 checksum files before making the release public. + +Pull requests targeting `main` trigger all three builds and installation checks, without publishing, when they change `.github/workflows/community-package.yml`, `default-plugins.json`, `packaging.config.json`, the packaging scripts listed in the workflow, `tools/__tests__/**`, `tools/ci/**`, or `tools/publish-community-build.cjs`. The workflow uses `--edition community` and does not trigger for unrelated application, SDK, vendor, manifest, or lockfile changes. `package.json` keeps the plain `A.B.C` used for plugin compatibility; the full version is passed to `node tools/package.mjs --version`. ## GitHub enforcement diff --git a/CONTRIBUTING.zh-CN.md b/CONTRIBUTING.zh-CN.md index b295bc56..f8e9c5d4 100644 --- a/CONTRIBUTING.zh-CN.md +++ b/CONTRIBUTING.zh-CN.md @@ -41,7 +41,11 @@ node --test tools/__tests__/docs-policy.test.mjs ## 社区包 -`community-package` 工作流构建未签名的 Windows 社区安装包:OpenTest,加上 `crysound/OpenTest-Plugins` 中的插件:`default-plugins.json` 列出的业务插件(当前为 `fft-tools`),以及 SonoDAQ 设备插件。设备插件放在安装目录的 `resources/backend/native/plugins/devices` 下。每次推送到 `main` 都发布一个日常预发布版本 `A.B.C-dev.<年月日>`,北京时间同一天的后续构建依次加 `.2`、`.3`。推送 `vA.B.C-rc.N` 或 `vA.B.C` 标签会发布候选版本或正式版本,打标签前先在 `OpenTest-Plugins` 打同名标签。修改打包相关文件的拉取请求只构建和校验安装包,不发布。`package.json` 保留用于插件兼容性的 `A.B.C`,完整版本号通过 `node tools/package.mjs --version` 传入。该工作流使用 `--edition community`,只随附 `default-plugins.json` 列出的插件。 +`community-package` 工作流构建三平台社区安装包:Windows x64 的 EXE(可执行安装程序)、Linux x64 的 DEB(Debian 软件包)、macOS ARM64 的 DMG(磁盘映像)。社区包包含 OpenTest、`crysound/OpenTest-Plugins` 中由 `default-plugins.json` 列出的业务插件(当前为 `fft-tools`)和 SonoDAQ 设备插件,不含官网完整包的其他插件。设备插件放在应用资源目录的 `backend/native/plugins/devices` 下。Windows 保留 `test` 测试模式,使用后端默认配置且不写入 `cloud.license.url`;Linux、macOS 使用正式授权服务的 `production` 模式,各模式独立生成配置。当前没有独立测试服务器;自动更新均关闭。Windows 未签名;macOS 保留多语言许可协议,仅作本机临时签名,未取得开发者签名或 Apple 公证,首次打开需在“隐私与安全性”中点“仍要打开”。SonoDAQ 设备插件要求 macOS 26 或更高版本。 + +每次推送到 `main` 都发布一个日常预发布版本 `A.B.C-dev.<年月日>`,北京时间同一天的后续构建依次加 `.2`、`.3`。推送 `vA.B.C-rc.N` 或 `vA.B.C` 标签会发布候选版本或正式版本;必须先在 `OpenTest-Plugins` 创建同名标签,缺失时构建立即失败,不回落到 `main`。日常构建、拉取请求和手动运行都使用插件仓库的 `main`;手动运行不能指定任意插件版本,标签运行必须使用同名插件标签。三个平台直接检出解析得到的同一提交,并再次核对,防止构建期间分支前移。全部构建及安装验证通过后,单一发布任务先上传三个安装包及各自的 SHA-256 文件校验值,再公开版本。 + +面向 `main` 的拉取请求若修改 `.github/workflows/community-package.yml`、`default-plugins.json`、`packaging.config.json`、工作流中列出的打包脚本、`tools/__tests__/**`、`tools/ci/**` 或 `tools/publish-community-build.cjs`,会触发三平台构建和安装验证,但不发布。工作流使用 `--edition community`;无关的应用、软件开发套件、vendor、清单或锁文件变更不会触发这条流程。`package.json` 保留用于插件兼容性的 `A.B.C`,完整版本号通过 `node tools/package.mjs --version` 传入。 ## GitHub 强制检查 diff --git a/tools/__tests__/community-package.test.mjs b/tools/__tests__/community-package.test.mjs new file mode 100644 index 00000000..1eebd150 --- /dev/null +++ b/tools/__tests__/community-package.test.mjs @@ -0,0 +1,142 @@ +import assert from 'node:assert/strict'; +import { createRequire } from 'node:module'; +import { execFileSync } from 'node:child_process'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import test from 'node:test'; +import { assertBinary, assertSameTree } from '../ci/verify-posix-installer.mjs'; +import { loadPackagingConfig } from '../packaging.mjs'; +import { repositoryRoot } from '../runtime.mjs'; + +// 使用现有打包依赖的 YAML 解析器,不为工作流检查另加产品依赖。 +const desktopRequire = createRequire(path.join(repositoryRoot, 'apps/desktop/package.json')); +const builderRequire = createRequire(desktopRequire.resolve('electron-builder/package.json')); +const yaml = createRequire(builderRequire.resolve('app-builder-lib/package.json'))('js-yaml'); + +test('工作流为三平台共用版本,只有推送且全部构建成功才进入单一发布任务', () => { + const workflow = yaml.load(readFileSync(path.join(repositoryRoot, '.github/workflows/community-package.yml'), 'utf8')); + assert.deepEqual(workflow.on.push.branches, ['main']); + assert.ok(workflow.on.push.tags.includes('!v*-dev.*')); + assert.ok(workflow.on.pull_request); + assert.ok(workflow.on.workflow_dispatch); + assert.deepEqual(workflow.on.workflow_dispatch, {}); + assert.equal(JSON.stringify(workflow).includes('plugins_ref'), false); + assert.equal(JSON.stringify(workflow).includes('REQUESTED_REF'), false); + assert.equal(JSON.stringify(workflow).includes('plugins-sha'), false); + assert.equal(JSON.stringify(workflow).includes('plugins-ref'), false); + assert.deepEqual(workflow.on.pull_request.paths, [ + '.github/workflows/community-package.yml', 'default-plugins.json', 'packaging.config.json', + 'tools/build-version.mjs', 'tools/default-plugins.mjs', 'tools/package.mjs', 'tools/packaging.mjs', + 'tools/packaging-platforms.mjs', 'tools/package-posix-native.mjs', 'tools/package-java.mjs', + 'tools/macos-signing.mjs', 'tools/__tests__/**', 'tools/publish-community-build.cjs', 'tools/ci/**', + ]); + assert.equal(workflow.jobs.build.needs, 'version'); + assert.equal(workflow.jobs.version.outputs['plugins-source'], '${{ steps.plugins.outputs.source }}'); + assert.equal(workflow.jobs.version.outputs['plugins-revision'], '${{ steps.plugins.outputs.revision }}'); + assert.equal(workflow.jobs['build-posix'].needs, 'version'); + assert.deepEqual(workflow.jobs.publish.needs, ['version', 'build', 'build-posix']); + assert.equal(workflow.jobs.publish.if, "github.event_name == 'push'"); + assert.equal(workflow.jobs.publish['timeout-minutes'], 30); + assert.equal(workflow.concurrency.group, 'community-package-${{ github.ref }}'); + assert.equal(workflow.concurrency['cancel-in-progress'], "${{ github.event_name == 'pull_request' }}"); + assert.equal(workflow.jobs['build-posix'].strategy['fail-fast'], false); + assert.deepEqual(workflow.jobs['build-posix'].strategy.matrix.include.map(m => [m.platform, m.runner, m.arch]), + [['linux', 'ubuntu-24.04', 'x64'], ['mac', 'macos-26', 'arm64']]); + const writers = Object.entries(workflow.jobs).filter(([, job]) => job.permissions?.contents === 'write'); + assert.deepEqual(writers.map(([name]) => name), ['publish']); + assert.equal(JSON.stringify(workflow).includes('secrets.'), false); + const windows = workflow.jobs.build.steps; + assert.ok(windows.some(s => s.run?.includes('verify-installer.ps1'))); + assert.ok(windows.some(s => s.run?.includes('--platform windows --mode test'))); + assert.ok(windows.some(s => s.name === 'Verify public plugin revision')); + assert.ok(workflow.jobs['build-posix'].steps.some(s => s.name === 'Verify public plugin revision')); + for (const job of [workflow.jobs.build, workflow.jobs['build-posix']]) { + const checkout = job.steps.find(s => s.with?.repository === 'crysound/OpenTest-Plugins'); + assert.equal(checkout.with.ref, '${{ needs.version.outputs.plugins-revision }}'); + assert.equal(checkout.with.path, '.runtime/opentest-plugins'); + } + assert.equal(workflow.jobs.build.env.PLUGINS_ROOT, '${{ github.workspace }}/.runtime/opentest-plugins'); + const windowsVerify = windows.find(s => s.name === 'Verify public plugin revision').run; + assert.match(windowsVerify, /\$env:EXPECTED_PLUGINS_REVISION/); + assert.match(windowsVerify, /\$LASTEXITCODE -ne 0/); + const posix = workflow.jobs['build-posix'].steps; + const verify = posix.findIndex(s => s.run?.includes('verify-posix-installer.mjs')); + const upload = posix.findIndex(s => s.uses?.startsWith('actions/upload-artifact@')); + assert.ok(verify > 0 && upload > verify); +}); + +test('三平台生成独立模式配置并使用明确的正式许可证地址', t => { + const temporary = mkdtempSync(path.join(os.tmpdir(), 'opentest-ci-installer-')); + t.after(() => rmSync(temporary, { recursive: true, force: true })); + execFileSync(process.execPath, ['tools/ci/prepare-installer.mjs'], { + cwd: repositoryRoot, env: { ...process.env, OPENTEST_CI_NATIVE_PLUGIN_BIN_ROOT: '', OPENTEST_CI_INSTALLER_DIRECTORY: temporary }, + }); + for (const platform of ['windows', 'linux', 'mac']) { + const mode = platform === 'windows' ? 'test' : 'production'; + const config = loadPackagingConfig(mode, + path.join(temporary, 'packaging.json'), platform); + assert.equal(config.backendConfig, path.join(temporary, `application-${mode}.properties`)); + assert.equal(config.desktopConfig, path.join(temporary, `local-settings-${mode}.json`)); + assert.equal(config.services.environment, mode); + assert.equal(config.services.licenseUrl, 'https://update.opentest.com'); + const backend = readFileSync(config.backendConfig, 'utf8'); + if (mode === 'test') assert.doesNotMatch(backend, /^cloud\.license\.url=/m); + else assert.match(backend, /^cloud\.license\.url=https:\/\/update\.opentest\.com$/m); + assert.equal(JSON.parse(readFileSync(config.desktopConfig, 'utf8')).updates.enabled, false); + } +}); + +test('安装文件检查识别符号链接缺失、目标变化和文件类型变化', t => { + const root = mkdtempSync(path.join(os.tmpdir(), 'opentest-links-')); + t.after(() => rmSync(root, { recursive: true, force: true })); + const source = path.join(root, 'source'), installed = path.join(root, 'installed'); + mkdirSync(source); mkdirSync(installed); + const original = path.join(root, 'original'), other = path.join(root, 'other'); + mkdirSync(original); mkdirSync(other); + // Windows 目录联接无需额外权限,仍由 lstat/readlink 作为链接比较。 + const link = (target, name) => symlinkSync(target, name, process.platform === 'win32' ? 'junction' : 'dir'); + link(original, path.join(source, 'runtime')); + link(original, path.join(installed, 'runtime')); + assert.equal(assertSameTree(source, installed), 1); + rmSync(path.join(installed, 'runtime')); + assert.throws(() => assertSameTree(source, installed)); + link(other, path.join(installed, 'runtime')); + assert.throws(() => assertSameTree(source, installed)); + rmSync(path.join(installed, 'runtime')); + writeFileSync(path.join(installed, 'runtime'), 'regular file'); + assert.throws(() => assertSameTree(source, installed), /类型/); +}); + +test('安装文件检查拒绝缺失、额外或内容被修改的文件', t => { + const root = mkdtempSync(path.join(os.tmpdir(), 'opentest-tree-')); + t.after(() => rmSync(root, { recursive: true, force: true })); + const source = path.join(root, 'source'), installed = path.join(root, 'installed'); + mkdirSync(source); mkdirSync(installed); + writeFileSync(path.join(source, 'runtime'), 'expected'); + writeFileSync(path.join(installed, 'runtime'), 'expected'); + assert.equal(assertSameTree(source, installed), 1); + writeFileSync(path.join(installed, 'runtime'), 'changed'); + assert.throws(() => assertSameTree(source, installed)); + writeFileSync(path.join(installed, 'runtime'), 'expected'); + writeFileSync(path.join(installed, 'unexpected'), 'extra'); + assert.throws(() => assertSameTree(source, installed)); + rmSync(path.join(installed, 'unexpected')); rmSync(path.join(installed, 'runtime')); + assert.throws(() => assertSameTree(source, installed)); +}); + +test('安装程序架构检查区分 Linux x64 与 macOS ARM64,拒绝错误架构', t => { + const root = mkdtempSync(path.join(os.tmpdir(), 'opentest-binary-')); + t.after(() => rmSync(root, { recursive: true, force: true })); + const file = path.join(root, 'binary'); + const linux = Buffer.alloc(64); + linux.write('\x7fELF'); linux[4] = 2; linux[5] = 1; linux.writeUInt16LE(62, 18); + writeFileSync(file, linux); assertBinary(file, 'linux'); + linux.writeUInt16LE(183, 18); writeFileSync(file, linux); + assert.throws(() => assertBinary(file, 'linux')); + const mac = Buffer.alloc(64); + mac.writeUInt32LE(0xfeedfacf); mac.writeUInt32LE(0x0100000c, 4); + writeFileSync(file, mac); assertBinary(file, 'mac'); + mac.writeUInt32LE(0x01000007, 4); writeFileSync(file, mac); + assert.throws(() => assertBinary(file, 'mac')); +}); diff --git a/tools/__tests__/ensure-delivery-manifest.test.mjs b/tools/__tests__/ensure-delivery-manifest.test.mjs new file mode 100644 index 00000000..8b4d8026 --- /dev/null +++ b/tools/__tests__/ensure-delivery-manifest.test.mjs @@ -0,0 +1,103 @@ +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import test from 'node:test'; +import { ensureDeliveryManifest } from '../ci/ensure-delivery-manifest.mjs'; + +function fixture(prefix, platform = 'linux') { + const root = mkdtempSync(path.join(os.tmpdir(), prefix)); + const arch = platform === 'mac' ? 'arm64' : 'x64'; + const extension = platform === 'mac' ? '.dmg' : '.deb'; + const version = '2.5.0-dev.20261117'; + const output = path.join(root, `dist/releases/${platform}-${arch}/production/unsigned`); + const installer = path.join(output, 'installer', `OpenTest-Community-${version}-production-installer-${platform}-${arch}${extension}`); + mkdirSync(path.dirname(installer), { recursive: true }); + const data = Buffer.from(`${platform} fixture`); + writeFileSync(installer, data); + return { root, output, installer, version, data }; +} + +test('creates and rechecks a delivery manifest with the installer digest', t => { + const { root, version, data } = fixture('opentest-delivery-', 'mac'); + t.after(() => rmSync(root, { recursive: true, force: true })); + const result = ensureDeliveryManifest({ root, platform: 'mac', version }); + assert.equal(result.automaticUpdate, false); + assert.equal(result.artifacts[0].sha256, createHash('sha256').update(data).digest('hex')); + const manifest = JSON.parse(readFileSync(path.join(root, 'dist/releases/mac-arm64/production/unsigned/delivery.json'), 'utf8')); + assert.deepEqual(manifest, result); + assert.deepEqual(ensureDeliveryManifest({ root, platform: 'mac', version }), result); +}); + +test('discovers bundled plugin archives whose IDs contain hyphens', t => { + const { root, version } = fixture('opentest-delivery-plugin-'); + t.after(() => rmSync(root, { recursive: true, force: true })); + const staged = path.join(root, '.runtime/packaging/production-stage/resources/bundled-plugins'); + mkdirSync(staged, { recursive: true }); + writeFileSync(path.join(staged, `com.opentest.fft-tools-2.5.4-${'a'.repeat(64)}.otplugin`), 'fixture'); + const result = ensureDeliveryManifest({ root, platform: 'linux', version }); + assert.deepEqual(result.bundledPlugins, [{ pluginId: 'com.opentest.fft-tools', version: '2.5.4' }]); +}); + +test('uses the package index format for bundled plugin metadata', t => { + const { root, version } = fixture('opentest-delivery-index-'); + t.after(() => rmSync(root, { recursive: true, force: true })); + const staged = path.join(root, '.runtime/packaging/production-stage/resources/bundled-plugins'); + mkdirSync(staged, { recursive: true }); + writeFileSync(path.join(staged, 'index.json'), JSON.stringify({ + schemaVersion: 1, + hostVersion: '2.5.0', + plugins: [{ + pluginId: 'com.opentest.fft-tools', + version: '2.5.4', + file: `com.opentest.fft-tools-2.5.4-${'a'.repeat(64)}.otplugin`, + archiveSha256: 'a'.repeat(64), + packageDigest: 'b'.repeat(64), + }], + })); + const result = ensureDeliveryManifest({ root, platform: 'linux', version }); + assert.deepEqual(result.bundledPlugins, [{ pluginId: 'com.opentest.fft-tools', version: '2.5.4' }]); +}); + +test('preserves bundled plugin metadata while validating generated fields', t => { + const { root, output, installer, version, data } = fixture('opentest-delivery-existing-'); + t.after(() => rmSync(root, { recursive: true, force: true })); + const digest = createHash('sha256').update(data).digest('hex'); + const existing = { + version, productVersion: '2.5.0', edition: 'community', mode: 'production', + platform: 'linux', arch: 'x64', signed: false, notarized: false, + bundledPlugins: [{ pluginId: 'com.opentest.fft-tools', version: '2.5.4' }], automaticUpdate: false, + artifacts: [{ file: `installer/${path.basename(installer)}`, sha256: digest }], + }; + writeFileSync(path.join(output, 'delivery.json'), JSON.stringify(existing)); + assert.deepEqual(ensureDeliveryManifest({ root, platform: 'linux', version }), existing); +}); + +test('rejects a missing installer directory or duplicate installers', t => { + const root = mkdtempSync(path.join(os.tmpdir(), 'opentest-delivery-missing-')); + t.after(() => rmSync(root, { recursive: true, force: true })); + assert.throws(() => ensureDeliveryManifest({ root, platform: 'linux', version: '2.5.0-dev.20261117' })); + const duplicate = fixture('opentest-delivery-duplicate-'); + t.after(() => rmSync(duplicate.root, { recursive: true, force: true })); + writeFileSync(path.join(duplicate.output, 'installer', `OpenTest-Community-${duplicate.version}-production-installer-linux-x64-copy.deb`), 'duplicate'); + assert.throws(() => ensureDeliveryManifest({ root: duplicate.root, platform: 'linux', version: duplicate.version })); +}); + +test('rejects tampered digest, version, platform, and automatic update fields', t => { + const { root, output, installer, version, data } = fixture('opentest-delivery-tampered-'); + t.after(() => rmSync(root, { recursive: true, force: true })); + const digest = createHash('sha256').update(data).digest('hex'); + const base = { + version, productVersion: '2.5.0', edition: 'community', mode: 'production', + platform: 'linux', arch: 'x64', signed: false, notarized: false, + bundledPlugins: [{ pluginId: 'com.opentest.fft-tools', version: '2.5.4' }], automaticUpdate: false, + artifacts: [{ file: `installer/${path.basename(installer)}`, sha256: digest }], + }; + for (const [field, value] of [['version', '2.5.1-dev.20261117'], ['platform', 'mac'], ['automaticUpdate', true]]) { + writeFileSync(path.join(output, 'delivery.json'), JSON.stringify({ ...base, [field]: value })); + assert.throws(() => ensureDeliveryManifest({ root, platform: 'linux', version })); + } + writeFileSync(path.join(output, 'delivery.json'), JSON.stringify({ ...base, artifacts: [{ ...base.artifacts[0], sha256: '0'.repeat(64) }] })); + assert.throws(() => ensureDeliveryManifest({ root, platform: 'linux', version })); +}); diff --git a/tools/__tests__/mac-build-resources.test.mjs b/tools/__tests__/mac-build-resources.test.mjs new file mode 100644 index 00000000..291620c5 --- /dev/null +++ b/tools/__tests__/mac-build-resources.test.mjs @@ -0,0 +1,48 @@ +import assert from 'node:assert/strict'; +import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import test from 'node:test'; +import { createBuilderConfig, licenseTextToRtf, prepareMacBuildResources } from '../package.mjs'; +import { desktopRoot } from '../runtime.mjs'; + +function decodeRtf(rtf) { + assert.match(rtf, /^\{\\rtf1\\ansi\\ansicpg1252\\uc1 /); + const body = rtf.replace(/^\{\\rtf1\\ansi\\ansicpg1252\\uc1 /, '').slice(0, -1); + return body.replace(/\\u(-?\d+)\?|\\par\n|\\tab |\\([\\{}])/g, (all, number, escaped) => { + if (number !== undefined) return String.fromCharCode(Number(number) & 0xffff); + if (escaped) return escaped; + return all.startsWith('\\par') ? '\n' : '\t'; + }); +} + +test('许可协议转换保留 Unicode、特殊符号、段落及 RTF 保留字符', () => { + const text = 'Français ● 中文 한국어 日本語 😀 {terms} \\ path\tvalue\r\nnext'; + const rtf = licenseTextToRtf('\uFEFF' + text); + assert.doesNotMatch(rtf, /[^\x00-\x7f]/); + assert.equal(decodeRtf(rtf), text.replace(/\r\n/g, '\n')); +}); + +test('macOS builder 使用含全部九种语言协议的暂存资源,其他平台保持原资源', t => { + const stage = mkdtempSync(path.join(os.tmpdir(), 'opentest-mac-resources-')); + t.after(() => rmSync(stage, { recursive: true, force: true })); + const source = path.join(desktopRoot, 'assets'); + const target = prepareMacBuildResources(stage); + // --kind both 会在同一暂存目录分别构建安装包和升级包。 + assert.equal(prepareMacBuildResources(stage), target); + const licenses = readdirSync(source).filter(name => /^license_.*\.txt$/.test(name)); + assert.equal(licenses.length, 9); + for (const name of licenses) { + const original = readFileSync(path.join(source, name), 'utf8'); + const rtf = readFileSync(path.join(target, name.replace('.txt', '.rtf')), 'ascii'); + assert.equal(decodeRtf(rtf), original.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n'), name); + assert.equal(readFileSync(path.join(source, name), 'utf8'), original); + } + assert.deepEqual(readFileSync(path.join(target, 'icons/darwin/icon.icns')), readFileSync(path.join(source, 'icons/darwin/icon.icns'))); + assert.equal(readdirSync(target).filter(name => /^license_.*\.(txt|rtf|html)$/.test(name)).length, licenses.length); + for (const platform of ['mac', 'windows', 'linux']) { + const builder = createBuilderConfig({ config: { platform, mode: 'production' }, stage, + output: path.join(stage, 'out'), version: '2.5.0', kind: 'installer', electronExecutable: '/runtime/electron' }); + assert.equal(builder.directories.buildResources, platform === 'mac' ? target : source); + } +}); diff --git a/tools/__tests__/publish-community-build.test.mjs b/tools/__tests__/publish-community-build.test.mjs index 648bc3c6..f28858a7 100644 --- a/tools/__tests__/publish-community-build.test.mjs +++ b/tools/__tests__/publish-community-build.test.mjs @@ -1,6 +1,6 @@ import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; -import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import test, { after } from 'node:test'; @@ -9,7 +9,7 @@ import publish from '../publish-community-build.cjs'; const temporary = mkdtempSync(path.join(os.tmpdir(), 'opentest-release-')); after(() => rmSync(temporary, { recursive: true, force: true })); -function fixture(version = '2.5.0-dev.20261114.2', ref = 'refs/heads/main') { +function windowsFixture(version = '2.5.0-dev.20261114.2', ref = 'refs/heads/main') { const installer = Buffer.alloc(256); installer.write('MZ'); installer.writeUInt32LE(128, 0x3c); @@ -36,7 +36,8 @@ function fixture(version = '2.5.0-dev.20261114.2', ref = 'refs/heads/main') { return { artifact, calls, installer, options: { github, context, version, artifactId: '456', artifactDigest, installerPath, - installerDigest: createHash('sha256').update(installer).digest('hex') }, + installerDigest: createHash('sha256').update(installer).digest('hex'), + pluginsSource: 'main', pluginsRevision: 'b'.repeat(40) }, }; } @@ -44,7 +45,7 @@ test('daily builds from main create the dated tag and stay prereleases', async ( const { options, calls, installer } = fixture(); const result = await publish(options); assert.match(result.html_url, /releases\/tag\/v2\.5\.0-dev\.20261114\.2$/); - assert.deepEqual(calls.map(c => c.name), ['createRelease', 'uploadReleaseAsset', 'uploadReleaseAsset', 'updateRelease']); + assert.deepEqual(calls.map(c => c.name), ['createRelease', ...Array(6).fill('uploadReleaseAsset'), 'updateRelease']); assert.equal(calls[0].args.tag_name, 'v2.5.0-dev.20261114.2'); assert.equal(calls[0].args.target_commitish, options.context.sha); assert.equal(calls[0].args.draft, true); @@ -53,9 +54,9 @@ test('daily builds from main create the dated tag and stay prereleases', async ( assert.deepEqual(calls[1].args.data, installer); assert.equal(calls[1].args.name, 'OpenTest-Community-2.5.0-dev.20261114.2-windows-x64-setup.exe'); assert.equal(calls[2].args.data.toString(), `${options.installerDigest} ${calls[1].args.name}\n`); - assert.equal(calls[3].args.draft, false); - assert.equal(calls[3].args.prerelease, true); - assert.equal(calls[3].args.make_latest, 'false'); + assert.equal(calls.at(-1).args.draft, false); + assert.equal(calls.at(-1).args.prerelease, true); + assert.equal(calls.at(-1).args.make_latest, 'false'); }); test('release candidates and releases publish only from their own tag', async () => { @@ -63,12 +64,12 @@ test('release candidates and releases publish only from their own tag', async () await publish(rc.options); assert.equal(rc.calls[0].args.prerelease, true); assert.equal(rc.calls[0].args.generate_release_notes, true); - assert.equal(rc.calls[3].args.make_latest, 'false'); + assert.equal(rc.calls.at(-1).args.make_latest, 'false'); const release = fixture('2.5.0', 'refs/tags/v2.5.0'); await publish(release.options); assert.equal(release.calls[0].args.tag_name, 'v2.5.0'); - assert.equal(release.calls[3].args.prerelease, false); - assert.equal(release.calls[3].args.make_latest, 'true'); + assert.equal(release.calls.at(-1).args.prerelease, false); + assert.equal(release.calls.at(-1).args.make_latest, 'true'); }); test('refuses a version that does not match the triggering ref', async () => { @@ -130,6 +131,11 @@ test('rejects invalid inputs without creating a release', async () => { for (const change of [ o => { o.version = '2.5.0-beta.1'; }, o => { o.artifactId = ''; }, + o => { delete o.posixRoot; }, + o => { o.posixRoot = 'relative'; }, + o => { delete o.pluginsRevision; }, + o => { o.pluginsRevision = 'b'.repeat(39); }, + o => { o.pluginsRevision = 'g'.repeat(40); }, o => { o.artifactDigest = ''; }, o => { o.installerDigest = ''; }, o => { o.installerPath = 'app.zip'; }, @@ -141,3 +147,118 @@ test('rejects invalid inputs without creating a release', async () => { assert.equal(calls.length, 0); } }); + +function fixture(version, ref) { + const f = windowsFixture(version, ref); + const { options } = f; + options.posixRoot = mkdtempSync(path.join(temporary, 'posix-')); + f.reports = []; + for (const [platform, arch, extension] of [['linux', 'x64', '.deb'], ['mac', 'arm64', '.dmg']]) { + const directory = path.join(options.posixRoot, platform); + mkdirSync(path.join(directory, 'installer'), { recursive: true }); + const installer = Buffer.alloc(1024); + if (platform === 'linux') installer.write('!\n'); + else installer.write('koly', installer.length - 512); + const name = `OpenTest-Community-${options.version}-production-installer-${platform}-${arch}${extension}`; + const digest = createHash('sha256').update(installer).digest('hex'); + writeFileSync(path.join(directory, 'installer', name), installer); + const reportPath = path.join(directory, 'installer-verification.json'); + writeFileSync(reportPath, JSON.stringify({ status: 'pass', installationVerification: 'pass', cleanup: 'pass', + javaRuntime: 'pass', automaticUpdates: 'disabled', mode: 'production', version: options.version, + platform, architecture: arch, installer: name, sha256: digest })); + f.reports.push(reportPath); + writeFileSync(path.join(directory, 'delivery.json'), JSON.stringify({ version: options.version, + platform, arch, edition: 'community', mode: 'production', signed: false, + artifacts: [{ file: `installer/${name}`, sha256: digest }] })); + } + options.github.rest.actions.downloadArtifact = async () => { throw new Error('POSIX artifacts must use the files already downloaded by the workflow'); }; + return f; +} + +test('three verified platforms create one release and publish only after all six uploads', async () => { + const { options, calls } = fixture(); + await publish(options); + assert.equal(calls.filter(c => c.name === 'createRelease').length, 1); + const uploads = calls.filter(c => c.name === 'uploadReleaseAsset'); + assert.equal(uploads.length, 6); + assert.equal(new Set(uploads.map(c => c.args.name)).size, 6); + assert.match(uploads[2].args.name, /linux-x64-setup\.deb$/); + assert.match(uploads[4].args.name, /macos-arm64-setup\.dmg$/); + assert.equal(calls.at(-1).name, 'updateRelease'); + assert.equal(calls.at(-1).args.prerelease, true); + assert.match(calls[0].args.body, /Source commit:/); + assert.match(calls[0].args.body, /Successful build:/); + assert.match(calls[0].args.body, /OpenTest-Plugins commit: b{40}/); + assert.match(calls[0].args.body, /Includes OpenTest/); + for (const phrase of ['production license mode', 'https://update.opentest.com', 'no separate test server', 'multilingual license agreement', 'Open Anyway', 'macOS 26', 'were not verified', 'SHA-256 checksums:']) assert.ok(calls[0].args.body.includes(phrase), phrase); +}); + +test('all-platform release and RC retain channel semantics', async () => { + for (const version of ['2.5.0', '2.5.0-rc.1']) { + const f = fixture(version, `refs/tags/v${version}`); + await publish(f.options); + assert.equal(f.calls.at(-1).args.make_latest, version === '2.5.0' ? 'true' : 'false'); + } +}); + +test('missing or invalid downloaded POSIX files block release creation', async () => { + for (const damage of ['missing', 'report', 'delivery', 'digest', 'format']) { + const f = fixture(); + if (damage === 'missing') rmSync(path.join(f.options.posixRoot, 'mac'), { recursive: true, force: true }); + if (damage === 'report') rmSync(f.reports[1]); + if (damage === 'delivery') rmSync(path.join(f.options.posixRoot, 'mac/delivery.json')); + if (damage === 'digest') { + const report = JSON.parse(readFileSync(f.reports[1], 'utf8')); + report.sha256 = '0'.repeat(64); + writeFileSync(f.reports[1], JSON.stringify(report)); + } + if (damage === 'format') { + const report = JSON.parse(readFileSync(f.reports[1], 'utf8')); + const installer = path.join(f.options.posixRoot, 'mac/installer', report.installer); + writeFileSync(installer, Buffer.alloc(1024)); + report.sha256 = createHash('sha256').update(readFileSync(installer)).digest('hex'); + writeFileSync(f.reports[1], JSON.stringify(report)); + const deliveryPath = path.join(f.options.posixRoot, 'mac/delivery.json'); + const delivery = JSON.parse(readFileSync(deliveryPath, 'utf8')); + delivery.artifacts[0].sha256 = report.sha256; + writeFileSync(deliveryPath, JSON.stringify(delivery)); + } + await assert.rejects(publish(f.options)); + assert.equal(f.calls.length, 0); + } +}); + +test('failed installation, wrong version and changed installer block all publication', async () => { + for (const damage of ['install', 'version', 'bytes', 'format']) { + const f = fixture(); + const report = JSON.parse(readFileSync(f.reports[1], 'utf8')); + if (damage === 'install') report.installationVerification = 'failed'; + if (damage === 'version') report.version = '2.5.0'; + if (damage === 'bytes' || damage === 'format') { + const installer = path.join(f.options.posixRoot, 'mac/installer', report.installer); + writeFileSync(installer, Buffer.alloc(1024)); + if (damage === 'format') { + report.sha256 = createHash('sha256').update(readFileSync(installer)).digest('hex'); + const deliveryPath = path.join(f.options.posixRoot, 'mac/delivery.json'); + const delivery = JSON.parse(readFileSync(deliveryPath, 'utf8')); + delivery.artifacts[0].sha256 = report.sha256; + writeFileSync(deliveryPath, JSON.stringify(delivery)); + } + } + writeFileSync(f.reports[1], JSON.stringify(report)); + await assert.rejects(publish(f.options)); + assert.equal(f.calls.length, 0); + } +}); + +test('a late platform upload failure leaves the release as a draft', async () => { + const f = fixture(); + const upload = f.options.github.rest.repos.uploadReleaseAsset; + f.options.github.rest.repos.uploadReleaseAsset = async args => { + if (args.name.endsWith('.dmg')) throw new Error('mac upload failed'); + return upload(args); + }; + await assert.rejects(publish(f.options), /mac upload failed/); + assert.equal(f.calls.filter(c => c.name === 'createRelease').length, 1); + assert.equal(f.calls.some(c => c.name === 'updateRelease'), false); +}); diff --git a/tools/__tests__/resolve-plugin-revision.test.mjs b/tools/__tests__/resolve-plugin-revision.test.mjs new file mode 100644 index 00000000..d485f948 --- /dev/null +++ b/tools/__tests__/resolve-plugin-revision.test.mjs @@ -0,0 +1,46 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolvePluginRevision } from '../ci/resolve-plugin-revision.mjs'; + +const commit = 'a'.repeat(40), tagObject = 'b'.repeat(40); +function lookup(entries, queries = []) { + return { lsRemote(refs) { + queries.push(...refs); + return Object.entries(entries).filter(([ref]) => refs.includes(ref)).map(([ref, sha]) => `${sha}\t${ref}`).join('\n'); + } }; +} + +test('主线、拉取请求和手动运行都锁定插件 main', () => { + for (const refType of ['branch', undefined]) { + assert.deepEqual(resolvePluginRevision({ refType, refName: 'feature' }, + lookup({ 'refs/heads/main': commit, 'refs/heads/feature': tagObject })), { ref: 'main', sha: commit }); + } +}); + +for (const refName of ['v2.5.0', 'v2.5.0-rc.1']) { + test(`${refName} 缺少同名插件标签时失败,不借用分支或 main`, () => { + const queries = []; + assert.throws(() => resolvePluginRevision({ refType: 'tag', refName }, + lookup({ 'refs/heads/main': commit, [`refs/heads/${refName}`]: commit }, queries)), /同名标签/); + assert.ok(queries.every(ref => ref.startsWith(`refs/tags/${refName}`))); + }); + test(`${refName} 支持轻量标签与附注标签,附注标签取提交而非标签对象`, () => { + for (const annotated of [false, true]) { + const refs = { [`refs/tags/${refName}`]: annotated ? tagObject : commit }; + if (annotated) refs[`refs/tags/${refName}^{}`] = commit; + assert.equal(resolvePluginRevision({ refType: 'tag', refName }, lookup(refs)).sha, commit); + } + }); +} + +test('手动运行不能指定任意插件引用,标签仍遵守同名标签规则', () => { + assert.deepEqual(resolvePluginRevision({ refType: 'branch' }, lookup({ 'refs/heads/main': commit, + 'refs/heads/feature': tagObject })), { ref: 'main', sha: commit }); + assert.throws(() => resolvePluginRevision({ refType: 'tag', refName: 'v2.5.0' }, lookup({})), /同名标签/); +}); + +test('网络错误不能回落,错误提交不能通过', () => { + const failure = new Error('remote unavailable'); + assert.throws(() => resolvePluginRevision({ refType: 'branch' }, { lsRemote() { throw failure; } }), error => error === failure); + assert.throws(() => resolvePluginRevision({ refType: 'branch' }, lookup({ 'refs/heads/main': 'bad' })), /无法解析/); +}); diff --git a/tools/ci/ensure-delivery-manifest.mjs b/tools/ci/ensure-delivery-manifest.mjs new file mode 100644 index 00000000..d7c9619f --- /dev/null +++ b/tools/ci/ensure-delivery-manifest.mjs @@ -0,0 +1,108 @@ +import { createHash } from 'node:crypto'; +import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; +import path from 'node:path'; +import { pathToFileURL } from 'node:url'; +import { repositoryRoot } from '../runtime.mjs'; +import { resolvePackagingPlatform } from '../packaging-platforms.mjs'; + +function discoverBundledPlugins(root) { + const entries = new Map(); + const add = (pluginId, version) => { + if (typeof pluginId === 'string' && typeof version === 'string' && pluginId && version) { + entries.set(pluginId, { pluginId, version }); + } + }; + const searchRoot = path.join(root, '.runtime/packaging'); + if (!existsSync(searchRoot)) return []; + // package.mjs writes this index beside the staged archives. Reading it keeps + // the verifier on the same metadata source as the generated delivery.json. + for (const stage of readdirSync(searchRoot, { withFileTypes: true })) { + if (!stage.isDirectory() || !stage.name.startsWith('production-')) continue; + const index = path.join(searchRoot, stage.name, 'resources', 'bundled-plugins', 'index.json'); + if (!existsSync(index)) continue; + try { + const metadata = JSON.parse(readFileSync(index, 'utf8')); + for (const plugin of metadata.plugins || []) add(plugin.pluginId, plugin.version); + } catch { /* Ignore incomplete staging files. */ } + } + if (!entries.size) { + for (const entry of readdirSync(searchRoot, { recursive: true, withFileTypes: true })) { + if (!entry.isFile()) continue; + const file = path.join(entry.parentPath, entry.name); + if (entry.name === 'plugin-package.json') { + try { + const metadata = JSON.parse(readFileSync(file, 'utf8')); + add(metadata.pluginId || metadata.packageId, metadata.version); + } catch { /* Ignore unrelated or incomplete staging files. */ } + } else { + const match = entry.name.match(/^(com\.opentest\..+)-(\d+\.\d+\.\d+)-[0-9a-f]{64}\.otplugin$/i); + if (match) add(match[1], match[2]); + } + } + } + return [...entries.values()].sort((a, b) => a.pluginId.localeCompare(b.pluginId)); +} + +export function ensureDeliveryManifest({ root = repositoryRoot, platform, version, mode = 'production', edition = 'community' } = {}) { + const target = resolvePackagingPlatform(platform); + const output = path.join(root, 'dist/releases', `${target.platform}-${target.arch}`, mode, 'unsigned'); + const installerDir = path.join(output, 'installer'); + const extension = target.installerExtension; + const installers = existsSync(installerDir) + ? readdirSync(installerDir).filter(name => name.endsWith(extension)).map(name => path.join(installerDir, name)) + : []; + if (installers.length !== 1) throw new Error(`应有一个 ${platform} 安装包,实际找到 ${installers.length}`); + const installer = installers[0]; + const name = path.basename(installer); + const expected = `OpenTest-Community-${version}-${mode}-installer-${platform}-${target.arch}${extension}`; + if (name !== expected) throw new Error(`安装包名称不匹配:${name};应为 ${expected}`); + const sha256 = createHash('sha256').update(readFileSync(installer)).digest('hex'); + const bundledPlugins = discoverBundledPlugins(root); + const manifest = { + version, productVersion: version.replace(/-(?:dev|rc)\..*$/, ''), edition, mode, + platform, arch: target.arch, signed: false, notarized: false, + ...(bundledPlugins.length ? { bundledPlugins } : {}), automaticUpdate: false, + artifacts: [{ file: `installer/${name}`, sha256 }], + }; + const manifestPath = path.join(output, 'delivery.json'); + if (existsSync(manifestPath)) { + const existing = JSON.parse(readFileSync(manifestPath, 'utf8')); + const artifact = existing?.artifacts?.length === 1 ? existing.artifacts[0] : null; + const valid = existing.version === manifest.version && + existing.productVersion === manifest.productVersion && + existing.edition === manifest.edition && existing.mode === manifest.mode && + existing.platform === manifest.platform && existing.arch === manifest.arch && + existing.signed === manifest.signed && existing.notarized === manifest.notarized && + existing.automaticUpdate === false && + (!bundledPlugins.length || JSON.stringify(existing.bundledPlugins) === JSON.stringify(bundledPlugins)) && + artifact?.file === manifest.artifacts[0].file && artifact.sha256 === sha256; + if (!valid) { + console.error('交付清单字段不一致:', JSON.stringify({ + existing: { + version: existing.version, + productVersion: existing.productVersion, + edition: existing.edition, + mode: existing.mode, + platform: existing.platform, + arch: existing.arch, + signed: existing.signed, + notarized: existing.notarized, + automaticUpdate: existing.automaticUpdate, + bundledPlugins: existing.bundledPlugins, + artifacts: existing.artifacts, + }, + expected: manifest, + })); + throw new Error(`已有 delivery.json 与实际安装包不一致:${manifestPath}`); + } + return existing; + } else { + writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); + } + return manifest; +} + +if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) { + ensureDeliveryManifest({ platform: process.env.PACKAGE_PLATFORM, version: process.env.VERSION }); + console.info('安装包交付清单已核对'); +} diff --git a/tools/ci/prepare-electron.mjs b/tools/ci/prepare-electron.mjs new file mode 100644 index 00000000..204ea782 --- /dev/null +++ b/tools/ci/prepare-electron.mjs @@ -0,0 +1,29 @@ +import { createHash } from 'node:crypto'; +import { execFileSync } from 'node:child_process'; +import { createRequire } from 'node:module'; +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; +import path from 'node:path'; +import os from 'node:os'; +import { repositoryRoot } from '../runtime.mjs'; +import { currentPackagingPlatform, resolvePackagingPlatform, assertPackagingPlatformReady } from '../packaging-platforms.mjs'; + +const target = resolvePackagingPlatform(currentPackagingPlatform()); +assertPackagingPlatformReady(target); +if (target.platform === 'windows') throw new Error('Windows 使用 prepare-electron.ps1'); +const require = createRequire(path.join(repositoryRoot, 'apps/desktop/package.json')); +const root = path.dirname(require.resolve('electron/package.json')); +const version = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')).version; +if (!/^\d+\.\d+\.\d+$/.test(version)) throw new Error('Electron 版本无效'); +const archiveName = `electron-v${version}-${target.nodePlatform}-${target.arch}.zip`; +const expected = JSON.parse(readFileSync(path.join(root, 'checksums.json'), 'utf8'))[archiveName]; +if (!/^[0-9a-f]{64}$/.test(expected)) throw new Error('锁定依赖中缺少当前平台的 Electron 校验值'); +const archive = path.join(process.env.RUNNER_TEMP || os.tmpdir(), archiveName); +execFileSync('curl', ['--fail', '--location', '--retry', '3', '--output', archive, + `https://github.com/electron/electron/releases/download/v${version}/${archiveName}`], { stdio: 'inherit' }); +if (createHash('sha256').update(readFileSync(archive)).digest('hex') !== expected) throw new Error('Electron 下载校验失败'); +const dist = path.join(root, 'dist'); +mkdirSync(dist, { recursive: true }); +execFileSync('unzip', ['-q', '-o', archive, '-d', dist], { stdio: 'inherit' }); +if (readFileSync(path.join(dist, 'version'), 'utf8').trim().replace(/^v/, '') !== version) throw new Error('Electron 版本不匹配'); +writeFileSync(path.join(root, 'path.txt'), target.platform === 'mac' ? 'Electron.app/Contents/MacOS/Electron' : 'electron'); +console.info(`Electron ${version} ${target.nodePlatform}/${target.arch} 校验通过`); diff --git a/tools/ci/prepare-installer.mjs b/tools/ci/prepare-installer.mjs index b46ef04e..a4128c3a 100644 --- a/tools/ci/prepare-installer.mjs +++ b/tools/ci/prepare-installer.mjs @@ -3,16 +3,28 @@ import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); -const directory = path.join(root, '.runtime/ci-installer'); +const directory = process.env.OPENTEST_CI_INSTALLER_DIRECTORY + ? path.resolve(process.env.OPENTEST_CI_INSTALLER_DIRECTORY) + : path.join(root, '.runtime/ci-installer'); mkdirSync(directory, { recursive: true }); // Use the repository's distributable example, never a developer's local settings. const settings = JSON.parse(readFileSync(path.join(root, 'apps/desktop/local-settings.example.json'), 'utf8')); settings.updates = { ...settings.updates, enabled: false }; -const backendConfig = path.join(directory, 'application.properties'); -const desktopConfig = path.join(directory, 'local-settings.json'); -writeFileSync(backendConfig, '# Daily test installer: use the bundled backend defaults.\n'); -writeFileSync(desktopConfig, `${JSON.stringify(settings, null, 2)}\n`); +const base = JSON.parse(readFileSync(path.join(root, 'packaging.config.json'), 'utf8')); +const modes = {}; +for (const mode of ['test', 'production']) { + const licenseUrl = base.modes[mode].licenseUrl; + if (typeof licenseUrl !== 'string' || !licenseUrl.trim()) throw new Error(`${mode} 模式缺少授权服务地址`); + const backendConfig = path.join(directory, `application-${mode}.properties`); + const desktopConfig = path.join(directory, `local-settings-${mode}.json`); + const backendContents = mode === 'test' + ? `# Community installer: ${mode}\n` + : `# Community installer: ${mode}\ncloud.license.url=${licenseUrl}\n`; + writeFileSync(backendConfig, backendContents); + writeFileSync(desktopConfig, `${JSON.stringify(settings, null, 2)}\n`); + modes[mode] = { backendConfig, desktopConfig, licenseUrl }; +} writeFileSync(path.join(directory, 'runtime.env'), '# CI uses only explicit workflow environment values.\n'); // Optional prepared device plugins (/windows-amd64/plugins/devices/...), e.g. SonoDAQ in the community package. const nativePluginBinRoot = process.env.OPENTEST_CI_NATIVE_PLUGIN_BIN_ROOT @@ -21,8 +33,8 @@ if (nativePluginBinRoot && !(existsSync(nativePluginBinRoot) && statSync(nativeP throw new Error(`Missing device plugin directory: ${nativePluginBinRoot}`); writeFileSync(path.join(directory, 'packaging.json'), `${JSON.stringify({ schemaVersion: 1, - backendConfig, - desktopConfig, + // Each mode has its own generated configuration; never read developer settings. + modes, ...(nativePluginBinRoot ? { nativePluginBinRoot } : {}), }, null, 2)}\n`); -console.info('Prepared daily test installer settings; automatic updates are disabled.'); +console.info('已分别生成测试和生产模式配置:测试模式使用后端默认授权配置,生产模式使用仓库确认的正式服务地址;自动更新已关闭。'); diff --git a/tools/ci/resolve-plugin-revision.mjs b/tools/ci/resolve-plugin-revision.mjs new file mode 100644 index 00000000..be5f4b01 --- /dev/null +++ b/tools/ci/resolve-plugin-revision.mjs @@ -0,0 +1,36 @@ +import { execFileSync } from 'node:child_process'; +import { appendFileSync } from 'node:fs'; +import path from 'node:path'; +import { pathToFileURL } from 'node:url'; + +const repository = 'https://github.com/crysound/OpenTest-Plugins.git'; +const shaPattern = /^[0-9a-f]{40}$/i; + +export function resolvePluginRevision({ refType, refName }, { + lsRemote = refs => execFileSync('git', ['ls-remote', repository, ...refs], { + encoding: 'utf8', timeout: 60_000, windowsHide: true, + }), +} = {}) { + const ref = refType === 'tag' ? refName : 'main'; + if (typeof ref !== 'string' || !ref || /[\s\x00-\x1f\x7f~^:?*\[\\]/.test(ref) || ref.includes('..') || ref.includes('@{')) { + throw new Error('插件引用格式无效'); + } + const candidates = refType === 'tag' ? [`refs/tags/${ref}`] : ['refs/heads/main']; + const queries = candidates.flatMap(candidate => [candidate, `${candidate}^{}`]); + // 精确匹配返回的引用;附注标签优先使用剥离后的提交编号,网络失败直接向上传递。 + const refs = new Map(lsRemote(queries).trim().split(/\r?\n/).filter(Boolean).map(line => { + const [sha, name] = line.split(/\s+/); + return [name, sha]; + })); + for (const candidate of candidates) { + const sha = refs.get(`${candidate}^{}`) || refs.get(candidate); + if (shaPattern.test(sha || '')) return { ref, sha: sha.toLowerCase() }; + } + throw new Error(`无法解析插件引用 ${ref};标签构建必须先在插件仓库创建同名标签`); +} + +if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) { + const result = resolvePluginRevision({ refType: process.env.REF_TYPE, refName: process.env.REF_NAME }); + if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, `source=${result.ref}\nrevision=${result.sha}\n`); + console.info(`OpenTest-Plugins: ${result.ref} (${result.sha})`); +} diff --git a/tools/ci/stage-posix-device-plugin.mjs b/tools/ci/stage-posix-device-plugin.mjs new file mode 100644 index 00000000..fe765d76 --- /dev/null +++ b/tools/ci/stage-posix-device-plugin.mjs @@ -0,0 +1,41 @@ +import { execFileSync } from 'node:child_process'; +import { cpSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { repositoryRoot } from '../runtime.mjs'; +import { resolvePackagingPlatform, assertPackagingPlatformReady } from '../packaging-platforms.mjs'; + +const target = resolvePackagingPlatform(process.env.PACKAGE_PLATFORM); +assertPackagingPlatformReady(target); +if (!['mac', 'linux'].includes(target.platform)) throw new Error('此入口只构建 Linux/macOS 设备插件'); +const temp = process.env.RUNNER_TEMP || os.tmpdir(); +if (!process.env.NATIVE_PLUGIN_BIN) throw new Error('NATIVE_PLUGIN_BIN 必须设置为设备插件输出目录'); +const source = path.join(temp, 'opentest-plugins/plugins/opentest_sonodaq_plugin'); +const framework = path.join(repositoryRoot, 'apps/native'); +const build = path.join(temp, 'sonodaq-build'); +const mac = target.platform === 'mac'; +const sdk = path.join(source, mac ? 'macos/sonodaq-sdk-1.4.0-macos-arm64-cpp' : 'linux/sonodaq-sdk-1.4.0-linux-x64-cpp'); +const run = (command, args) => execFileSync(command, args, { stdio: 'inherit' }); +run('cmake', ['-S', source, '-B', build, '-DCMAKE_BUILD_TYPE=Release', '-DSONODAQ_BUILD_TESTS=OFF', + `-DOPENTEST_FRAMEWORK_ROOT=${framework}`, `-DSONODAQ_SDK_ROOT=${sdk}`, + ...(mac ? ['-DCMAKE_OSX_ARCHITECTURES=arm64', '-DCMAKE_OSX_DEPLOYMENT_TARGET=26.0'] : [])]); +run('cmake', ['--build', build, '--target', 'OpenTestSonoDaqDevicePlugin', '--parallel', '4']); +const install = path.join(temp, 'sonodaq-install'); +run('cmake', ['--install', build, '--prefix', install]); +const entry = `libOpenTestSonoDaqDevicePlugin.${mac ? 'dylib' : 'so'}`; +const stage = path.join(temp, 'sonodaq-stage/OpenTestSonoDaqDevicePlugin'); +run('python', [path.join(source, `package/publish-${mac ? 'macos' : 'linux'}.py`), + '--library', path.join(install, 'lib', entry), '--sdk', sdk, '--stage', stage, + '--output', path.join(temp, 'sonodaq-package'), '--framework', framework]); +const manifest = JSON.parse(readFileSync(path.join(stage, 'plugin-package.json'), 'utf8')); +if (manifest.packageId !== 'opentest.sonodaq.device' || manifest.entry !== entry) throw new Error('设备插件清单身份不匹配'); +const files = readdirSync(stage, { recursive: true, withFileTypes: true }).filter(e => e.isFile()) + .map(e => path.relative(stage, path.join(e.parentPath, e.name)).replaceAll('\\', '/')).sort(); +if (JSON.stringify(files) !== JSON.stringify([...manifest.files, 'plugin-package.json'].sort())) throw new Error('设备插件文件清单不匹配'); +const devices = path.join(process.env.NATIVE_PLUGIN_BIN, target.nativePlatform, 'plugins/devices'); +mkdirSync(devices, { recursive: true }); +cpSync(stage, path.join(devices, manifest.packageId), { recursive: true }); +writeFileSync(path.join(devices, 'plugins.json'), JSON.stringify({ schemaVersion: 1, plugins: [{ + packageId: manifest.packageId, pluginKind: manifest.pluginKind, + entry: `${manifest.packageId}/${entry}`, enabled: true, bundled: true, +}] }, null, 2) + '\n'); diff --git a/tools/ci/verify-posix-installer.mjs b/tools/ci/verify-posix-installer.mjs new file mode 100644 index 00000000..e6fa3c95 --- /dev/null +++ b/tools/ci/verify-posix-installer.mjs @@ -0,0 +1,160 @@ +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import { execFileSync } from 'node:child_process'; +import { accessSync, constants, existsSync, lstatSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, rmSync, writeFileSync } from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { pathToFileURL } from 'node:url'; +import { repositoryRoot } from '../runtime.mjs'; +import { resolvePackagingPlatform, assertPackagingPlatformReady } from '../packaging-platforms.mjs'; + +const sha256 = file => createHash('sha256').update(readFileSync(file)).digest('hex'); +const run = (command, args, options = {}) => execFileSync(command, args, { encoding: 'utf8', timeout: 300_000, stdio: ['pipe', 'pipe', 'pipe'], ...options }).trim(); +const files = root => readdirSync(root, { recursive: true, withFileTypes: true }).filter(e => e.isFile() || e.isSymbolicLink()) + .map(e => path.relative(root, path.join(e.parentPath, e.name))).sort(); + +export function assertSameTree(source, installed) { + const expected = files(source); + assert.ok(expected.length, `暂存目录为空:${source}`); + assert.deepEqual(files(installed), expected, `安装文件集合不匹配:${installed}`); + for (const file of expected) { + const from = path.join(source, file), to = path.join(installed, file); + const link = lstatSync(from).isSymbolicLink(); + assert.equal(lstatSync(to).isSymbolicLink(), link, `安装文件类型不匹配:${file}`); + assert.equal(link ? readlinkSync(to) : sha256(to), link ? readlinkSync(from) : sha256(from), `安装文件不匹配:${file}`); + } + return expected.length; +} + +export function assertBinary(file, platform) { + const bytes = readFileSync(file); + assert.ok(bytes.length >= 32, `二进制文件过短:${file}`); + if (platform === 'linux') { + assert.equal(bytes.subarray(0, 4).toString('hex'), '7f454c46'); + assert.equal(bytes[4], 2); // ELF64 + assert.equal(bytes[5], 1); // little-endian + assert.equal(bytes.readUInt16LE(18), 62); // x86-64 + } else { + // 本次只交付 ARM64,不接受其他架构或含混的通用二进制。 + assert.equal(bytes.readUInt32LE(0), 0xfeedfacf); + assert.equal(bytes.readUInt32LE(4), 0x0100000c); + } +} + +export function verifyInstaller({ platform = process.env.PACKAGE_PLATFORM, root = repositoryRoot } = {}) { + const target = resolvePackagingPlatform(platform); + assertPackagingPlatformReady(target); + assert.ok(['mac', 'linux'].includes(platform)); + const output = path.join(root, `dist/releases/${platform}-${target.arch}/production/unsigned`); + const delivery = JSON.parse(readFileSync(path.join(output, 'delivery.json'), 'utf8')); + if (process.env.VERSION) assert.equal(delivery.version, process.env.VERSION); + assert.equal(delivery.platform, platform); + assert.equal(delivery.arch, target.arch); + assert.equal(delivery.mode, 'production'); + assert.equal(delivery.edition, 'community'); + assert.equal(delivery.signed, false); + assert.equal(delivery.artifacts.length, 1); + const artifact = delivery.artifacts[0]; + const name = `OpenTest-Community-${delivery.version}-production-installer-${platform}-${target.arch}${target.installerExtension}`; + assert.equal(artifact.file, `installer/${name}`); + const installer = path.join(output, artifact.file); + assert.equal(sha256(installer), artifact.sha256); + const stages = readdirSync(path.join(root, '.runtime/packaging')).filter(n => n.startsWith('production-')); + assert.equal(stages.length, 1, '安装验证要求唯一的本次暂存目录'); + const stage = path.join(root, '.runtime/packaging', stages[0]); + const temp = mkdtempSync(path.join(os.tmpdir(), 'opentest-install-')); + const mount = path.join(temp, 'volume'); + let mounted = false, installedPackage, installedExecutable; + let report, primaryError, cleanupError; + try { + let installation; + if (platform === 'linux') { + installedPackage = run('dpkg-deb', ['--field', installer, 'Package']); + assert.match(installedPackage, /^[a-z0-9][a-z0-9+.-]+$/); + assert.equal(run('dpkg-deb', ['--field', installer, 'Architecture']), 'amd64'); + run('sudo', ['env', 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', installer]); + assert.equal(run('dpkg-query', ['-W', '-f=${Status}', installedPackage]), 'install ok installed'); + const executable = run('dpkg-query', ['-L', installedPackage]).split('\n').find(f => /^\/opt\/[^/]+\/opentest$/.test(f)); + assert.ok(executable, '安装包未注册桌面程序'); + installedExecutable = executable; + installation = path.dirname(executable); + assertBinary(executable, platform); + } else { + // Even a timed-out attach may have mounted the volume: attempt detach before removal. + mounted = true; + run('hdiutil', ['attach', '-readonly', '-nobrowse', '-mountpoint', mount, installer], { input: 'Y\n' }); + const app = path.join(temp, 'OpenTest.app'); + run('ditto', [path.join(mount, 'OpenTest.app'), app]); + run('codesign', ['--verify', '--deep', '--strict', app]); + installation = path.join(app, 'Contents'); + assertBinary(path.join(installation, 'MacOS/OpenTest'), platform); + } + const resources = path.join(installation, platform === 'mac' ? 'Resources' : 'resources'); + assert.ok(readFileSync(path.join(resources, 'app.asar')).length); + const counts = {}; + // macOS 签名会调整部分二进制;与最终应用目录比较,而非签名前暂存目录。 + const finalResources = platform === 'mac' + ? path.join(output, 'installer/mac-arm64/OpenTest.app/Contents/Resources') : path.join(stage, 'resources'); + for (const directory of ['java', 'backend', 'frontend', 'config', 'bundled-plugins']) { + counts[directory] = assertSameTree(path.join(finalResources, directory), path.join(resources, directory)); + } + const java = path.join(resources, 'java/bin/java'); + assertBinary(java, platform); + accessSync(java, constants.X_OK); + run(java, ['-version']); + const native = path.join(resources, 'app.asar.unpacked/packages/native-modules/dist'); + for (const module of ['mem-ring-reader/mem_ring_reader.node', 'unit-conversion/unit_conversion.node', 'algorithm-bridge/algorithm_bridge.node']) { + assertBinary(path.join(native, module), platform); + } + const nativeSource = platform === 'mac' + ? path.join(finalResources, 'app.asar.unpacked/packages/native-modules/dist') + // 打包器会剔除类型声明等开发文件,安装结果应与最终封装目录一致。 + : path.join(output, 'installer/linux-unpacked/resources/app.asar.unpacked/packages/native-modules/dist'); + counts.nativeModules = assertSameTree(nativeSource, native); + accessSync(path.join(resources, 'backend/native/OptDevHost'), constants.X_OK); + assertBinary(path.join(resources, 'backend/native/OptDevHost'), platform); + const devices = path.join(resources, 'backend/native/plugins/devices'); + const registry = JSON.parse(readFileSync(path.join(devices, 'plugins.json'), 'utf8')); + const sono = registry.plugins.find(p => p.packageId === 'opentest.sonodaq.device'); + assert.equal(sono?.bundled, true); + assert.equal(sono?.enabled, true); + const entry = `opentest.sonodaq.device/libOpenTestSonoDaqDevicePlugin.${platform === 'mac' ? 'dylib' : 'so'}`; + assert.equal(sono.entry, entry); + assertBinary(path.join(devices, entry), platform); + assert.equal(JSON.parse(readFileSync(path.join(resources, 'config/local-settings.json'), 'utf8')).updates.enabled, false); + report = { status: 'pass', platform, architecture: target.arch, mode: 'production', version: delivery.version, + installer: name, sha256: artifact.sha256, installationVerification: 'pass', javaRuntime: 'pass', + filesVerified: counts, automaticUpdates: 'disabled', verifiedAt: new Date().toISOString() }; + } catch (error) { + primaryError = error; + } finally { + try { + if (mounted) { + run('hdiutil', ['detach', mount]); + mounted = false; + } + if (installedPackage) { + run('sudo', ['env', 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'remove', '-y', installedPackage]); + if (installedExecutable) assert.ok(!existsSync(installedExecutable), '卸载后仍存在桌面程序'); + } + } catch (error) { + cleanupError = error; + } + // Never recursively remove a directory while its disk image is still mounted. + if (!mounted) { + try { rmSync(temp, { recursive: true, force: true }); } + catch (error) { cleanupError ||= error; } + } + } + if (primaryError) { + if (cleanupError) console.error(`清理失败(保留原始错误):${cleanupError.message}`); + throw primaryError; + } + if (cleanupError) throw cleanupError; + report.cleanup = 'pass'; + writeFileSync(path.join(output, 'installer-verification.json'), JSON.stringify(report, null, 2) + '\n'); + console.info(`安装验证通过:${name}`); + return report; +} + +if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) verifyInstaller(); diff --git a/tools/package.mjs b/tools/package.mjs index 26db19bb..d950bc22 100644 --- a/tools/package.mjs +++ b/tools/package.mjs @@ -91,7 +91,9 @@ export function createBuilderConfig({ directories: { app: path.join(stage, "app"), output, - buildResources: path.join(desktopRoot, "assets"), + buildResources: target.platform === 'mac' + ? path.join(stage, 'mac-build-resources') + : path.join(desktopRoot, "assets"), }, compression: "maximum", npmRebuild: false, @@ -202,6 +204,35 @@ export function createBuilderConfig({ return builder; } +export function licenseTextToRtf(text) { + // DMG text resources use legacy Mac encodings. ASCII RTF with Unicode escapes + // preserves every language, punctuation mark and paragraph without that loss. + const body = text.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n').split('').map(char => { + if (char === '\n') return '\\par\n'; + if (char === '\t') return '\\tab '; + if ('\\{}'.includes(char)) return `\\${char}`; + const code = char.charCodeAt(0); + return code >= 32 && code < 127 ? char : `\\u${code > 32767 ? code - 65536 : code}?`; + }).join(''); + return `{\\rtf1\\ansi\\ansicpg1252\\uc1 ${body}}`; +} + +export function prepareMacBuildResources(stage, source = path.join(desktopRoot, 'assets')) { + const destination = path.join(stage, 'mac-build-resources'); + const isTextLicense = name => /^(?:license|eula)(?:_[^.]+)?\.txt$/i.test(name); + cpSync(source, destination, { + recursive: true, + dereference: true, + filter: sourcePath => path.dirname(sourcePath) !== source || !isTextLicense(path.basename(sourcePath)), + }); + for (const name of readdirSync(source).filter(isTextLicense)) { + const target = path.join(destination, name.replace(/\.txt$/i, '.rtf')); + if (existsSync(path.join(source, path.basename(target)))) throw new Error(`许可协议存在重复格式:${name}`); + writeFileSync(target, licenseTextToRtf(readFileSync(path.join(source, name), 'utf8')), 'ascii'); + } + return destination; +} + export function resolveBundledPluginPackages(directory) { if (!directory) return []; const root = path.resolve(repositoryRoot, directory); @@ -380,6 +411,7 @@ export async function packageHost({ mode, kind, sign = false, platform = current packageArtifact: async (target) => { const { build, Platform, Arch } = requireDesktop("electron-builder"); const destination = path.join(output, target); + if (platform === 'mac') prepareMacBuildResources(stage); const builderConfig = createBuilderConfig({ config, stage, @@ -436,7 +468,7 @@ export async function packageHost({ mode, kind, sign = false, platform = current arch: platformTarget.arch, signed: sign, notarized: notarize, - automaticUpdate: platform !== 'linux', + automaticUpdate: false, artifacts: artifacts.map((file) => ({ file: path.relative(output, file), sha256: createHash("sha256").update(readFileSync(file)).digest("hex"), diff --git a/tools/publish-community-build.cjs b/tools/publish-community-build.cjs index 29517b5f..972ae1f2 100644 --- a/tools/publish-community-build.cjs +++ b/tools/publish-community-build.cjs @@ -11,8 +11,36 @@ function channelOf(version) { return !match[4] ? 'release' : match[4].startsWith('rc.') ? 'rc' : 'dev'; } +function posixInstallers({ version, root }) { + const result = []; + for (const [platform, arch, extension] of [['linux', 'x64', '.deb'], ['mac', 'arm64', '.dmg']]) { + const directory = path.join(root, platform); + if (!path.isAbsolute(directory)) throw new Error(`Invalid downloaded artifact directory: ${directory}`); + const report = JSON.parse(readFileSync(path.join(directory, 'installer-verification.json'), 'utf8')); + const delivery = JSON.parse(readFileSync(path.join(directory, 'delivery.json'), 'utf8')); + const expectedName = `OpenTest-Community-${version}-production-installer-${platform}-${arch}${extension}`; + if (report.status !== 'pass' || report.installationVerification !== 'pass' || report.cleanup !== 'pass' || + report.javaRuntime !== 'pass' || report.automaticUpdates !== 'disabled' || report.mode !== 'production' || + report.version !== version || report.platform !== platform || report.architecture !== arch || + report.installer !== expectedName || !/^[0-9a-f]{64}$/.test(report.sha256) || + delivery.version !== version || delivery.platform !== platform || delivery.arch !== arch || + delivery.edition !== 'community' || delivery.mode !== 'production' || delivery.signed !== false || + delivery.artifacts?.length !== 1 || delivery.artifacts[0].file !== `installer/${expectedName}` || + delivery.artifacts[0].sha256 !== report.sha256) throw new Error('Invalid POSIX installation verification'); + const data = readFileSync(path.join(directory, 'installer', expectedName)); + const digest = createHash('sha256').update(data).digest('hex'); + if (digest !== report.sha256 || (platform === 'linux' + ? data.toString('ascii', 0, 8) !== '!\n' + : data.length < 512 || data.toString('ascii', data.length - 512, data.length - 508) !== 'koly')) { + throw new Error('POSIX installer failed SHA-256 or format verification'); + } + result.push({ name: `OpenTest-Community-${version}-${platform === 'mac' ? 'macos' : platform}-${arch}-setup${extension}`, data, digest }); + } + return result; +} + /** - * Publish the community installer verified by the successful Windows build job. + * Publish the three community installers after all installation checks succeed. * Daily builds (A.B.C-dev.[.N]) come only from main and create tag vVERSION on the built commit. * Release candidates and releases come only from the pushed tag vVERSION. */ @@ -24,6 +52,9 @@ module.exports = async function publishCommunityBuild({ artifactDigest, installerPath, installerDigest, + posixRoot, + pluginsSource, + pluginsRevision, }) { const channel = channelOf(version); const tag = `v${version}`; @@ -35,6 +66,9 @@ module.exports = async function publishCommunityBuild({ throw new Error('Missing or invalid build artifact SHA-256'); } + if (typeof posixRoot !== 'string' || !path.isAbsolute(posixRoot)) throw new Error('Missing or invalid POSIX artifact root'); + if (typeof pluginsRevision !== 'string' || !/^[0-9a-f]{40}$/i.test(pluginsRevision)) throw new Error('Missing or invalid plugin revision'); + const repo = context.repo; const { data: artifact } = await github.rest.actions.getArtifact({ ...repo, artifact_id: Number(artifactId) }); if (artifact.expired || artifact.name !== `opentest-community-windows-x64-${context.sha}` || @@ -62,8 +96,10 @@ module.exports = async function publishCommunityBuild({ } const assetName = `OpenTest-Community-${version}-windows-x64-setup.exe`; - const runUrl = `${context.serverUrl}/${repo.owner}/${repo.repo}/actions/runs/${context.runId}`; const kind = { dev: 'Daily build', rc: 'Release candidate', release: 'Release' }[channel]; + // Verify every platform before creating the one draft. Only this job writes Releases. + const assets = [{ name: assetName, data: installer, digest }, + ...posixInstallers({ version, root: posixRoot })]; const { data: draft } = await github.rest.repos.createRelease({ ...repo, tag_name: tag, @@ -74,35 +110,49 @@ module.exports = async function publishCommunityBuild({ make_latest: 'false', generate_release_notes: channel !== 'dev', body: [ - `${kind} of the OpenTest community package / 社区包${{ dev: '日常构建', rc: '候选版本', release: '正式版本' }[channel]}.`, + `OpenTest 社区包${{ dev: '日常构建', rc: '候选版本', release: '正式版本' }[channel]}。`, + `${kind} of the OpenTest community package.`, '', + `源码提交:${context.sha}`, `Source commit: ${context.sha}`, - `Successful build: ${runUrl}`, + `成功构建:${context.serverUrl}/${repo.owner}/${repo.repo}/actions/runs/${context.runId}`, + `Successful build: ${context.serverUrl}/${repo.owner}/${repo.repo}/actions/runs/${context.runId}`, + `OpenTest-Plugins 插件提交:${pluginsRevision}${pluginsSource ? `(来源 ${pluginsSource})` : ''}`, + `OpenTest-Plugins commit: ${pluginsRevision}${pluginsSource ? ` (source ${pluginsSource})` : ''}`, '', - 'Includes OpenTest and the plugins from crysound/OpenTest-Plugins: the public business plugins and the SonoDAQ device plugin. Plugins from the website package are not included.', - '包含 OpenTest 和 crysound/OpenTest-Plugins 中的插件:公开业务插件和 SonoDAQ 设备插件。不含官网完整包中的其他插件。', - 'Unsigned Windows x64 build using the test license service; automatic updates are disabled.', - '未签名的 Windows x64 安装包,使用测试授权服务,已关闭自动更新。', - 'The build verified silent installation, installed x64 binaries and bundled runtime integrity. Cloud login, chart licensing and hardware functions are not covered by that check.', + '包含 OpenTest 和 crysound/OpenTest-Plugins 中的公开业务插件及 SonoDAQ 设备插件,不含官网完整包中的其他插件。', + 'Includes OpenTest and the public business plugins and SonoDAQ device plugin from crysound/OpenTest-Plugins; other plugins from the website package are not included.', + 'Windows x64 提供未签名的 EXE(可执行安装程序),保留 test 测试模式,使用后端默认配置且不写入 cloud.license.url;Linux x64 提供 DEB(Debian 软件包),macOS ARM64 提供 DMG(磁盘映像),两者使用 production 正式授权模式。', + 'Windows x64 provides an unsigned EXE installer in test mode using backend defaults without an explicit cloud.license.url; Linux x64 provides a DEB package and macOS ARM64 provides a DMG disk image, both in production license mode.', + 'Linux 和 macOS 连接正式许可证服务 https://update.opentest.com;当前没有独立测试服务器。Windows test 模式不意味着有独立测试服务。自动更新均已关闭。', + 'Linux and macOS connect to the production license service https://update.opentest.com; there is no separate test server. Windows test mode does not imply a separate test service. Automatic updates are disabled.', + 'macOS 保留多语言许可协议;应用及插件仅作本机临时签名,未取得开发者签名或 Apple 公证。首次打开请在“隐私与安全性”中点“仍要打开”;SonoDAQ 设备插件要求 macOS 26 或更高版本。', + 'macOS retains the multilingual license agreement. The application and plugins have only ad hoc signatures, without developer signing or Apple notarization. On first launch, choose Open Anyway in Privacy & Security; the SonoDAQ device plugin requires macOS 26 or later.', + '已验证安装过程、程序架构、随包文件完整性和 Java 运行时;未验证云端登录、付费算法授权或真实硬件功能。', + 'The build verified installation, executable architecture, bundled file integrity and the Java runtime; cloud login, paid algorithm authorization and physical hardware functionality were not verified.', '', - `SHA-256 (${assetName}): \`${digest}\``, + 'SHA-256(文件校验值):', + 'SHA-256 checksums:', + ...assets.map(asset => `${asset.name}: \`${asset.digest}\``), ].join('\n'), }); - // Keep the release as a draft until both attachments have uploaded successfully. - await github.rest.repos.uploadReleaseAsset({ - ...repo, - release_id: draft.id, - name: assetName, - headers: { 'content-type': 'application/octet-stream' }, - data: installer, - }); - await github.rest.repos.uploadReleaseAsset({ - ...repo, - release_id: draft.id, - name: `${assetName}.sha256`, - headers: { 'content-type': 'text/plain' }, - data: Buffer.from(`${digest} ${assetName}\n`), - }); + // Keep the release private until every installer and checksum has uploaded. + for (const asset of assets) { + await github.rest.repos.uploadReleaseAsset({ + ...repo, + release_id: draft.id, + name: asset.name, + headers: { 'content-type': 'application/octet-stream' }, + data: asset.data, + }); + await github.rest.repos.uploadReleaseAsset({ + ...repo, + release_id: draft.id, + name: `${asset.name}.sha256`, + headers: { 'content-type': 'text/plain' }, + data: Buffer.from(`${asset.digest} ${asset.name}\n`), + }); + } const { data: release } = await github.rest.repos.updateRelease({ ...repo, release_id: draft.id,