resource.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { createHash } from 'node:crypto';
  2. import { chmodSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
  3. import { tmpdir } from 'node:os';
  4. import path from 'node:path';
  5. import { describe, expect, it } from 'vitest';
  6. import {
  7. installMcodeToolsLauncher,
  8. validateMcodeToolsResource,
  9. } from '../../src/resource.js';
  10. function fixture(): { dataDir: string; resourceDir: string } {
  11. const root = mkdtempSync(path.join(tmpdir(), 'mcode-tools-host-resource-'));
  12. const resourceDir = path.join(root, 'embedded', 'mcode-tools');
  13. mkdirSync(resourceDir, { recursive: true });
  14. const cli = 'console.log("embedded")\n';
  15. writeFileSync(path.join(resourceDir, 'cli.mjs'), cli);
  16. writeFileSync(
  17. path.join(resourceDir, 'manifest.json'),
  18. `${JSON.stringify({
  19. schemaVersion: 3,
  20. packageName: '@minimax/mcode-tools-test',
  21. version: '0.0.0-test',
  22. gitSha: 'abc1234',
  23. buildEnv: 'test',
  24. bedrockLane: 'oauth',
  25. nodeRange: '>=20',
  26. entry: 'cli.mjs',
  27. auth: {
  28. mode: 'shared-broker',
  29. protocol: {
  30. name: '@mavis/oauth-lease-protocol',
  31. version: '0.1.0-beta.0',
  32. wireVersion: 1,
  33. },
  34. },
  35. nativePackages: [],
  36. resources: [{
  37. path: 'cli.mjs',
  38. sha256: createHash('sha256').update(cli).digest('hex'),
  39. }],
  40. }, null, 2)}\n`,
  41. );
  42. return { dataDir: path.join(root, 'profile'), resourceDir };
  43. }
  44. describe('mcode-tools embedded resource', () => {
  45. it('validates the exact shared-broker artifact and installs a POSIX launcher', async () => {
  46. const input = fixture();
  47. expect(validateMcodeToolsResource({
  48. resourceDir: input.resourceDir,
  49. expectedBuildEnv: 'test',
  50. }).manifest.packageName).toBe('@minimax/mcode-tools-test');
  51. const installed = await installMcodeToolsLauncher({
  52. ...input,
  53. expectedBuildEnv: 'test',
  54. executable: '/path with spaces/node',
  55. platform: 'darwin',
  56. region: 'cn',
  57. bedrockLane: 'oauth2',
  58. brokerEndpoint: '/profile/run/mcode-auth-lease-v1.sock',
  59. brokerCapabilityFile: '/profile/run/mcode-auth-lease-v1.cap',
  60. });
  61. const launcher = readFileSync(installed.regionalLauncherPath, 'utf8');
  62. expect(launcher).toContain("export MCODE_AUTH_PROVIDER=shared-broker");
  63. expect(launcher).toContain(
  64. "export MCODE_EXTRA_HEADERS='bedrock_lane:oauth2,bedrock-lane:oauth2'",
  65. );
  66. expect(launcher).toContain('IS_SANDBOX');
  67. expect(launcher).toContain('MCODE_API_BASE_URL');
  68. expect(launcher).toContain('MCODE_AUTH_BASE_URL');
  69. expect(launcher).toContain('MCODE_CLIENT_ID');
  70. expect(launcher).toContain('MCODE_SCOPE');
  71. expect(launcher).toContain("'/path with spaces/node'");
  72. });
  73. it('clears inherited auth and routing overrides in the Windows launcher', async () => {
  74. const input = fixture();
  75. const installed = await installMcodeToolsLauncher({
  76. ...input,
  77. expectedBuildEnv: 'test',
  78. executable: 'C:\\Program Files\\node.exe',
  79. platform: 'win32',
  80. region: 'cn',
  81. brokerEndpoint: '\\\\.\\pipe\\mcode-auth-lease-v1',
  82. brokerCapabilityFile: 'C:\\profile\\run\\mcode-auth-lease-v1.cap',
  83. });
  84. const launcher = readFileSync(installed.regionalLauncherPath, 'utf8');
  85. for (const name of [
  86. 'IS_SANDBOX',
  87. 'MCODE_API_BASE_URL',
  88. 'MCODE_AUTH_BASE_URL',
  89. 'MCODE_CLIENT_ID',
  90. 'MCODE_SCOPE',
  91. ]) {
  92. expect(launcher).toContain(`set "${name}="`);
  93. }
  94. });
  95. it('rejects a modified cli', () => {
  96. const input = fixture();
  97. chmodSync(path.join(input.resourceDir, 'cli.mjs'), 0o600);
  98. writeFileSync(path.join(input.resourceDir, 'cli.mjs'), 'modified\n');
  99. expect(() => validateMcodeToolsResource({
  100. resourceDir: input.resourceDir,
  101. expectedBuildEnv: 'test',
  102. })).toThrow(/sha256/u);
  103. });
  104. });