credential-keyring-assets.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import assert from "node:assert/strict";
  2. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { test } from "vitest";
  6. import { build } from "esbuild";
  7. import { REMOTE_SERVER_NATIVE_EXTERNALS } from "../../server/buildRemoteValidation.js";
  8. import {
  9. keyringPlatformPackage,
  10. stageCredentialKeyring,
  11. } from "../../../scripts/stage-credential-keyring.mjs";
  12. test("remote CJS build preserves the native keyring dependency without bundling platform binaries", async () => {
  13. const result = await build({
  14. stdin: {
  15. contents: 'export const open = () => import("@napi-rs/keyring");',
  16. resolveDir: join(import.meta.dirname, ".."),
  17. },
  18. bundle: true,
  19. write: false,
  20. platform: "node",
  21. format: "cjs",
  22. metafile: true,
  23. external: REMOTE_SERVER_NATIVE_EXTERNALS,
  24. });
  25. assert.match(result.outputFiles[0]!.text, /@napi-rs\/keyring/);
  26. assert.equal(
  27. Object.keys(result.metafile!.inputs).some((name) => name.endsWith(".node")),
  28. false,
  29. );
  30. });
  31. test("keyring platform selection covers supported desktop architectures", () => {
  32. for (const arch of ["x64", "arm64"]) {
  33. assert.equal(keyringPlatformPackage(`darwin-${arch}`), `@napi-rs/keyring-darwin-${arch}`);
  34. assert.equal(keyringPlatformPackage(`win32-${arch}`), `@napi-rs/keyring-win32-${arch}-msvc`);
  35. assert.equal(keyringPlatformPackage(`linux-${arch}`), `@napi-rs/keyring-linux-${arch}-gnu`);
  36. }
  37. assert.throws(() => keyringPlatformPackage("unknown-x64"));
  38. });
  39. test("stages wrapper and target binary with licenses, omitting other platform binaries", async () => {
  40. const dir = await mkdtemp(join(tmpdir(), "keyring-assets-"));
  41. try {
  42. const packageName = keyringPlatformPackage("linux-arm64");
  43. for (const name of ["@napi-rs/keyring", packageName, "@napi-rs/keyring-win32-x64-msvc"]) {
  44. const root = join(dir, "source", "node_modules", name);
  45. await mkdir(root, { recursive: true });
  46. await writeFile(
  47. join(root, "package.json"),
  48. JSON.stringify({
  49. name,
  50. main: name === packageName ? "keyring.linux-arm64-gnu.node" : "index.js",
  51. }),
  52. );
  53. await writeFile(join(root, "index.js"), "module.exports = {}");
  54. await writeFile(join(root, "LICENSE"), "fixture-license");
  55. }
  56. await writeFile(
  57. join(dir, "source/node_modules", packageName, "keyring.linux-arm64-gnu.node"),
  58. "fixture-native",
  59. );
  60. await stageCredentialKeyring({
  61. sourceRoot: join(dir, "source"),
  62. targetRoot: join(dir, "output"),
  63. platformKey: "linux-arm64",
  64. });
  65. assert.equal(
  66. await readFile(join(dir, "output/node_modules", packageName, "LICENSE"), "utf8"),
  67. "fixture-license",
  68. );
  69. await assert.rejects(
  70. readFile(join(dir, "output/node_modules/@napi-rs/keyring-win32-x64-msvc/package.json")),
  71. { code: "ENOENT" },
  72. );
  73. } finally {
  74. await rm(dir, { recursive: true, force: true });
  75. }
  76. });