session-control-boundary.test.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import assert from "node:assert/strict";
  2. import { execFile } from "node:child_process";
  3. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  4. import { dirname, join, resolve } from "node:path";
  5. import { tmpdir } from "node:os";
  6. import { promisify } from "node:util";
  7. import { test } from "node:test";
  8. import ts from "typescript";
  9. const exec = promisify(execFile);
  10. const root = resolve(import.meta.dirname, "../..");
  11. async function withLintFixture(run) {
  12. const cwd = await mkdtemp(join(tmpdir(), "zcode-session-control-boundary-"));
  13. try {
  14. const { config } = ts.parseConfigFileTextToJson(
  15. ".oxlintrc.json",
  16. await readFile(join(root, ".oxlintrc.json"), "utf8"),
  17. );
  18. config.ignorePatterns = [];
  19. await writeFile(join(cwd, ".oxlintrc.json"), JSON.stringify(config));
  20. await run(async (file, source) => {
  21. await mkdir(dirname(join(cwd, file)), { recursive: true });
  22. await writeFile(join(cwd, file), source);
  23. const result = await exec(
  24. process.execPath,
  25. [join(root, "node_modules/oxlint/bin/oxlint"), "--format", "json", file],
  26. { cwd },
  27. ).catch((error) => error);
  28. return JSON.parse(result.stdout).diagnostics;
  29. });
  30. } finally {
  31. await rm(cwd, { recursive: true, force: true });
  32. }
  33. }
  34. test("会话控制核心拒绝环境 IO 与宿主实现依赖", async () => {
  35. await withLintFixture(async (lint) => {
  36. for (const specifier of [
  37. "node:fs/promises",
  38. "fs/promises",
  39. "node:child_process",
  40. "electron",
  41. "react",
  42. "@zcode/services",
  43. "@zcode/shared/zcode-protocol-v4",
  44. "@zcode/core",
  45. "../../../../services/src/node.js",
  46. ]) {
  47. const diagnostics = await lint(
  48. "packages/session-control/src/application/common/example.ts",
  49. `import { forbidden } from ${JSON.stringify(specifier)};\nexport const result = forbidden;\n`,
  50. );
  51. assert.ok(
  52. diagnostics.some((item) => item.code === "eslint(no-restricted-imports)"),
  53. `core unexpectedly allows ${specifier}`,
  54. );
  55. }
  56. });
  57. });
  58. test("会话控制核心通过注入访问时间与环境", async () => {
  59. await withLintFixture(async (lint) => {
  60. const diagnostics = await lint(
  61. "packages/session-control/src/domain/example.ts",
  62. "export const result = [process.env, window, fetch, setTimeout, setInterval, Date.now()];\n",
  63. );
  64. assert.equal(
  65. diagnostics.filter((item) => item.code === "eslint(no-restricted-globals)").length,
  66. 6,
  67. );
  68. });
  69. });
  70. test("CLI 与 App 用例不得相互导入", async () => {
  71. await withLintFixture(async (lint) => {
  72. for (const [scope, other] of [
  73. ["cli", "app"],
  74. ["app", "cli"],
  75. ]) {
  76. for (const specifier of [
  77. `../${other}/index.js`,
  78. `@zcode/session-control/${other}`,
  79. `#session-control/application/${other}/index`,
  80. ]) {
  81. const diagnostics = await lint(
  82. `packages/session-control/src/application/${scope}/example.ts`,
  83. `import { forbidden } from ${JSON.stringify(specifier)};\nexport const result = forbidden;\n`,
  84. );
  85. assert.ok(
  86. diagnostics.some((item) => item.code === "eslint(no-restricted-imports)"),
  87. `${scope} unexpectedly imports ${other}: ${specifier}`,
  88. );
  89. }
  90. }
  91. });
  92. });
  93. test("核心隔离检查拒绝动态导入和全局环境绕过", async () => {
  94. const { checkSessionControlSource } =
  95. await import("../architecture/session-control-boundary.mjs");
  96. const file = "packages/session-control/src/application/cli/example.ts";
  97. for (const source of [
  98. 'export const load = () => import("node:fs/promises");',
  99. 'export const load = () => require("node:child_process");',
  100. 'export const result = globalThis.fetch("https://example.com");',
  101. 'export const result = globalThis["Date"].now();',
  102. "export const result = Math.random();",
  103. 'export const result = Math["random"]();',
  104. "export const { random } = Math;",
  105. "export const result = globalThis.crypto.randomUUID();",
  106. ]) {
  107. assert.ok(
  108. checkSessionControlSource(file, source).length > 0,
  109. `unexpectedly allowed: ${source}`,
  110. );
  111. }
  112. for (const source of [
  113. 'import { rule } from "#session-control/domain/rule"; export const result = rule;',
  114. "export const result = Math.max(1, 2);",
  115. "export const controller = new AbortController();",
  116. ]) {
  117. assert.deepEqual(checkSessionControlSource(file, source), []);
  118. }
  119. });
  120. test("核心隔离检查限制跨目录和跨用例引用", async () => {
  121. const { checkSessionControlSource } =
  122. await import("../architecture/session-control-boundary.mjs");
  123. for (const scope of ["cli", "app"]) {
  124. const other = scope === "cli" ? "app" : "cli";
  125. const file = `packages/session-control/src/application/${scope}/example.ts`;
  126. for (const specifier of [
  127. `#session-control/application/${other}/index`,
  128. `../${other}/index.js`,
  129. `../${other}`,
  130. `#session-control/application/${other}`,
  131. "../../testing",
  132. "../../../../../services/src/node.js",
  133. "@zcode/services",
  134. ]) {
  135. assert.ok(
  136. checkSessionControlSource(file, `export { value } from ${JSON.stringify(specifier)};`)
  137. .length > 0,
  138. );
  139. }
  140. }
  141. });
  142. test("允许核心使用契约与纯规则且不限制外部适配器 IO", async () => {
  143. await withLintFixture(async (lint) => {
  144. const core = await lint(
  145. "packages/session-control/src/application/cli/example.ts",
  146. 'import { schema } from "../../contract/cli.js";\nimport { rule } from "../../domain/rule.js";\nexport const result = [schema, rule];\n',
  147. );
  148. assert.equal(core.filter((item) => item.code?.startsWith("eslint(no-restricted")).length, 0);
  149. const adapter = await lint(
  150. "packages/services/src/app-control/example.ts",
  151. 'import { readFile } from "node:fs/promises";\nexport const result = readFile;\n',
  152. );
  153. assert.equal(adapter.filter((item) => item.code?.startsWith("eslint(no-restricted")).length, 0);
  154. });
  155. });