entry.test.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Keyless entry-package tests — run on every host, no kernel or binary
  3. * required. Cover the JavaScript API's pure surface: grant-argv construction, the
  4. * resolution contract (platform package → fallback), and probe verdicts over
  5. * fake launchers. Requires built `lib/` (`pnpm build:ts`).
  6. */
  7. import assert from 'node:assert/strict';
  8. import fs from 'node:fs';
  9. import os from 'node:os';
  10. import path from 'node:path';
  11. import {
  12. LAUNCHER_BIN,
  13. LAUNCHER_FAILURE_EXIT,
  14. grantArgs,
  15. launcherPath,
  16. probe,
  17. } from '@deepseek-ai/node-addon-system/landlock-run';
  18. await assert.rejects(import('@deepseek-ai/node-addon-system'), {
  19. code: 'ERR_PACKAGE_PATH_NOT_EXPORTED',
  20. });
  21. // --- constants are part of the CLI contract ---
  22. assert.equal(LAUNCHER_BIN, 'landlock-run');
  23. assert.equal(LAUNCHER_FAILURE_EXIT, 125);
  24. // --- grantArgs: flag spelling, ordering, and empty grants ---
  25. assert.deepEqual(grantArgs({}), []);
  26. assert.deepEqual(grantArgs({ readOnly: ['/'] }), ['--ro', '/']);
  27. assert.deepEqual(
  28. grantArgs({ readOnly: ['/', '/opt'], readWrite: ['/tmp/work'] }),
  29. ['--ro', '/', '--ro', '/opt', '--rw', '/tmp/work'],
  30. );
  31. assert.deepEqual(grantArgs({ readWrite: ['/a'], readOnly: ['/b'] }), ['--ro', '/b', '--rw', '/a']);
  32. // --- launcherPath: resolves the platform package next to its package.json ---
  33. const platformPackage = `@deepseek-ai/node-addon-system-${process.platform}-${process.arch}`;
  34. const resolvedViaSeam = launcherPath((specifier) => {
  35. assert.equal(specifier, `${platformPackage}/package.json`);
  36. return path.join('/fake-install', specifier);
  37. });
  38. assert.equal(resolvedViaSeam, path.join('/fake-install', platformPackage, 'bin', LAUNCHER_BIN));
  39. // --- launcherPath: unresolvable package falls back to an absolute, package-boundary path ---
  40. const fallback = launcherPath(() => {
  41. throw new Error('not installed');
  42. });
  43. assert.ok(path.isAbsolute(fallback), 'fallback path must be absolute');
  44. assert.ok(
  45. fallback.includes(path.join('node_modules', ...platformPackage.split('/'), 'bin', LAUNCHER_BIN)),
  46. `fallback must point at the platform package layout: ${fallback}`,
  47. );
  48. // --- launcherPath: default resolution agrees with this workspace's layout ---
  49. const defaultPath = launcherPath();
  50. assert.ok(path.isAbsolute(defaultPath));
  51. assert.ok(defaultPath.endsWith(path.join('bin', LAUNCHER_BIN)), defaultPath);
  52. // --- probe: a missing launcher is unusable, indistinguishable from an unenforcing kernel ---
  53. assert.equal(probe(path.join(os.tmpdir(), 'nalr-no-such-launcher')), 'unusable');
  54. // --- probe: verdict parsing over fake launchers (POSIX shells only) ---
  55. if (process.platform !== 'win32') {
  56. const fakeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nalr-fake-launcher-'));
  57. const fake = (name, script) => {
  58. const file = path.join(fakeDir, name);
  59. fs.writeFileSync(file, `#!/bin/sh\n${script}\n`, { mode: 0o755 });
  60. return file;
  61. };
  62. assert.equal(probe(fake('full', 'echo "landlock: fully enforced"; exit 0')), 'full');
  63. assert.equal(probe(fake('partial', 'echo "landlock: partially enforced (older ABI)"; exit 0')), 'partial');
  64. assert.equal(probe(fake('failing', `exit ${LAUNCHER_FAILURE_EXIT}`)), 'unusable');
  65. assert.equal(probe(fake('hanging', 'sleep 10'), { timeoutMs: 200 }), 'unusable');
  66. fs.rmSync(fakeDir, { recursive: true, force: true });
  67. }
  68. console.log('entry.test: ok');