File size: 6,392 Bytes
f59fbe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node
import { access, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { basename, dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";

import { runTests, runVSCodeCommand } from "@vscode/test-electron";

import { isMainModule } from "./vsix-targets.mjs";

const scriptDir = dirname(fileURLToPath(import.meta.url));
const appDir = resolve(scriptDir, "..");
const defaultCachePath = join(tmpdir(), "kimi-vscode-test-cache");

export async function runExtensionHostSmoke(options = {}) {
  const version = options.version ?? "stable";
  const cacheRoot = resolve(options.cachePath ?? process.env.VSCODE_TEST_CACHE ?? defaultCachePath);
  const vsixPath = resolve(options.vsixPath ?? defaultVsixPath());
  await access(vsixPath);
  await mkdir(cacheRoot, { recursive: true });

  // A stable request must never reuse an older stable download. @vscode/test-electron
  // falls back to a cached build when version discovery fails, which would turn an
  // offline run into a false green. Exact versions remain cached by version.
  const disposableCache = version === "stable";
  const cachePath = disposableCache
    ? await mkdtemp(join(cacheRoot, "stable-"))
    : cacheRoot;

  const root = await mkdtemp(join(tmpdir(), "kvh-"));
  const paths = {
    root,
    extensions: join(root, "ext"),
    installUserData: join(root, "install"),
    userData: join(root, "user"),
    kimiHome: join(root, "home"),
    osHome: join(root, "os-home"),
    workspace: join(root, "ws"),
    harness: join(root, "harness"),
    report: join(root, "extension-host-report.json"),
  };

  try {
    await Promise.all([
      paths.extensions,
      paths.installUserData,
      paths.userData,
      paths.kimiHome,
      paths.osHome,
      paths.workspace,
      paths.harness,
    ].map((path) => mkdir(path, { recursive: true })));
    await writeFile(join(paths.workspace, "README.md"), "# Kimi VSIX Extension Host smoke\n", "utf8");
    await writeHarnessManifest(paths.harness);

    const installProfileArgs = [
      `--extensions-dir=${paths.extensions}`,
      `--user-data-dir=${paths.installUserData}`,
    ];
    const profileArgs = [
      `--extensions-dir=${paths.extensions}`,
      `--user-data-dir=${paths.userData}`,
    ];
    const downloadOptions = { version, cachePath };
    const install = await runVSCodeCommand(
      ["--install-extension", vsixPath, "--force", ...installProfileArgs],
      downloadOptions,
    );
    const installOutput = `${install.stdout}\n${install.stderr}`;
    if (!/successfully installed|was successfully installed/i.test(installOutput)) {
      throw new Error(`VSIX installation did not report success:\n${installOutput.trim()}`);
    }

    await runTests({
      ...downloadOptions,
      extensionDevelopmentPath: paths.harness,
      extensionTestsPath: join(appDir, "test", "extension-host", "index.cjs"),
      launchArgs: [
        paths.workspace,
        ...profileArgs,
        "--disable-workspace-trust",
        "--skip-welcome",
        "--skip-release-notes",
      ],
      extensionTestsEnv: {
        KIMI_CODE_HOME: paths.kimiHome,
        KIMI_VSCODE_SMOKE_OS_HOME: paths.osHome,
        KIMI_VSCODE_SMOKE_REPORT: paths.report,
        KIMI_VSCODE_SMOKE_VSIX: basename(vsixPath),
      },
    });

    const report = JSON.parse(await readFile(paths.report, "utf8"));
    if (typeof report.vscode !== "string" || report.vscode.length === 0) {
      throw new Error("Extension Host smoke did not report its actual VS Code version");
    }
    if (version !== "stable" && report.vscode !== version) {
      throw new Error(
        `Extension Host ran VS Code ${report.vscode}, expected requested version ${version}`,
      );
    }

    return { version, vscodeVersion: report.vscode, vsixPath, cachePath };
  } finally {
    await Promise.all([
      rm(root, { recursive: true, force: true }),
      disposableCache ? rm(cachePath, { recursive: true, force: true }) : Promise.resolve(),
    ]);
  }
}

function defaultVsixPath() {
  const arch = process.arch === "arm64" ? "arm64" : process.arch === "x64" ? "x64" : process.arch;
  return join(appDir, "artifacts", "vsix", `kimi-code-${process.platform}-${arch}.vsix`);
}

async function writeHarnessManifest(directory) {
  await writeFile(
    join(directory, "package.json"),
    JSON.stringify({
      name: "kimi-vscode-extension-host-smoke",
      displayName: "Kimi VSCode Extension Host Smoke",
      publisher: "local-test",
      version: "0.0.0",
      engines: { vscode: "^1.70.0" },
      main: "./extension.cjs",
      activationEvents: ["*"],
    }),
    "utf8",
  );
  await writeFile(
    join(directory, "extension.cjs"),
    "exports.activate = function activate() {}; exports.deactivate = function deactivate() {};\n",
    "utf8",
  );
}

function parseArguments(argv) {
  const options = {};
  for (let index = 0; index < argv.length; index += 1) {
    const argument = argv[index];
    if (argument === "--") continue;
    if (argument === "--version") {
      options.version = requiredValue(argv[++index], argument);
    } else if (argument === "--vsix") {
      options.vsixPath = requiredValue(argv[++index], argument);
    } else if (argument === "--cache-path") {
      options.cachePath = requiredValue(argv[++index], argument);
    } else if (argument === "--help" || argument === "-h") {
      options.help = true;
    } else {
      throw new Error(`Unknown option: ${argument}`);
    }
  }
  return options;
}

function requiredValue(value, flag) {
  if (value === undefined || value.startsWith("-")) throw new Error(`${flag} requires a value`);
  return value;
}

async function main() {
  const options = parseArguments(process.argv.slice(2));
  if (options.help) {
    console.log("Usage: node scripts/extension-host-smoke.mjs [--version <stable|x.y.z>] [--vsix <path>] [--cache-path <path>]");
    return;
  }
  const result = await runExtensionHostSmoke(options);
  console.log(
    `VSIX Extension Host smoke passed: ${result.vsixPath} on VS Code ${result.vscodeVersion} (requested ${result.version})`,
  );
}

if (isMainModule(import.meta.url)) {
  main().catch((error) => {
    console.error(`VSIX Extension Host smoke failed: ${error instanceof Error ? error.stack ?? error.message : String(error)}`);
    process.exitCode = 1;
  });
}