|
| 1 | +/** |
| 2 | + * is-main 契约测试:直接入口 / shim 入口 / 被异名入口 import |
| 3 | + * 运行: npm test -w @sfmc-bds/bds-tools(需先 build) |
| 4 | + * |
| 5 | + * 注:仓库根 .gitignore 忽略名为 test 的路径,故用例放在包根而非 test/。 |
| 6 | + */ |
| 7 | + |
| 8 | +import assert from "node:assert/strict"; |
| 9 | +import { spawnSync } from "node:child_process"; |
| 10 | +import fs from "node:fs"; |
| 11 | +import path from "node:path"; |
| 12 | +import { after, describe, it } from "node:test"; |
| 13 | +import { fileURLToPath } from "node:url"; |
| 14 | + |
| 15 | +const pkgRoot = path.dirname(fileURLToPath(import.meta.url)); |
| 16 | +const distDir = path.join(pkgRoot, "dist"); |
| 17 | + |
| 18 | +/** 与 recovery.js → dist/recovery.js 同构:根目录 shim 与 dist 模块同名 */ |
| 19 | +const MODULE_NAME = "_sfmc_is_main_fixture.js"; |
| 20 | +const distModule = path.join(distDir, MODULE_NAME); |
| 21 | +const rootShim = path.join(pkgRoot, MODULE_NAME); |
| 22 | +const alienEntry = path.join(pkgRoot, "_sfmc_alien_entry.js"); |
| 23 | + |
| 24 | +const moduleSrc = ` |
| 25 | +import { isMainModule } from "./is-main.js"; |
| 26 | +console.log(isMainModule(import.meta.url) ? "MAIN" : "NOT_MAIN"); |
| 27 | +`; |
| 28 | + |
| 29 | +const shimSrc = ` |
| 30 | +import path from "node:path"; |
| 31 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 32 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 33 | +await import(pathToFileURL(path.join(__dirname, "dist", ${JSON.stringify(MODULE_NAME)})).href); |
| 34 | +`; |
| 35 | + |
| 36 | +const alienSrc = ` |
| 37 | +import path from "node:path"; |
| 38 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 39 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 40 | +await import(pathToFileURL(path.join(__dirname, "dist", ${JSON.stringify(MODULE_NAME)})).href); |
| 41 | +`; |
| 42 | + |
| 43 | +describe("isMainModule", () => { |
| 44 | + it("dist 直接作为 argv 入口时为 true", () => { |
| 45 | + fs.mkdirSync(distDir, { recursive: true }); |
| 46 | + fs.writeFileSync(distModule, moduleSrc); |
| 47 | + const r = spawnSync(process.execPath, [distModule], { encoding: "utf8" }); |
| 48 | + assert.equal(r.status, 0, r.stderr); |
| 49 | + assert.equal(r.stdout.trim(), "MAIN"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("同名根目录 shim 动态 import dist 时为 true(recovery.js 契约)", () => { |
| 53 | + fs.mkdirSync(distDir, { recursive: true }); |
| 54 | + fs.writeFileSync(distModule, moduleSrc); |
| 55 | + fs.writeFileSync(rootShim, shimSrc); |
| 56 | + const r = spawnSync(process.execPath, [rootShim], { encoding: "utf8" }); |
| 57 | + assert.equal(r.status, 0, r.stderr); |
| 58 | + assert.equal(r.stdout.trim(), "MAIN"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("被异名入口 import 时为 false(check-update 加载 bds-manager)", () => { |
| 62 | + fs.mkdirSync(distDir, { recursive: true }); |
| 63 | + fs.writeFileSync(distModule, moduleSrc); |
| 64 | + fs.writeFileSync(alienEntry, alienSrc); |
| 65 | + const r = spawnSync(process.execPath, [alienEntry], { encoding: "utf8" }); |
| 66 | + assert.equal(r.status, 0, r.stderr); |
| 67 | + assert.equal(r.stdout.trim(), "NOT_MAIN"); |
| 68 | + }); |
| 69 | + |
| 70 | + after(() => { |
| 71 | + for (const p of [distModule, rootShim, alienEntry]) { |
| 72 | + try { |
| 73 | + fs.unlinkSync(p); |
| 74 | + } catch { |
| 75 | + /* ignore */ |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | +}); |
0 commit comments