headless-config.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { mkdtemp, rm, writeFile } from 'node:fs/promises';
  2. import { tmpdir } from 'node:os';
  3. import { join } from 'node:path';
  4. import { afterEach, describe, expect, it } from 'vitest';
  5. import { loadTuiRuntimeConfig } from '../../src/headless/config.js';
  6. const roots: string[] = [];
  7. afterEach(async () => {
  8. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  9. });
  10. describe('explicit headless config', () => {
  11. it('loads an explicit config source without changing the active dataDir', async () => {
  12. const root = await mkdtemp(join(tmpdir(), 'mcode-headless-config-'));
  13. roots.push(root);
  14. const configPath = join(root, 'run.yaml');
  15. await writeFile(
  16. configPath,
  17. [
  18. 'defaultModel: provider/model',
  19. 'permissionMode: auto',
  20. 'provider:',
  21. ' provider:',
  22. ' options:',
  23. ' apiKey: sk-test',
  24. 'runawayGuard:',
  25. ' enabled: false',
  26. 'toolResultCompaction:',
  27. ' enabled: false',
  28. ' maxInlineKiB: 48',
  29. ' mcpDetailsMaxInlineKiB: 40',
  30. ' watermarkKiB: 384',
  31. ' minSavingsKiB: 192',
  32. ' minCandidateKiB: 3',
  33. ' keepRecentRounds: 2',
  34. 'telemetry:',
  35. ' enabled: true',
  36. ].join('\n'),
  37. );
  38. await expect(
  39. loadTuiRuntimeConfig(configPath, { dataDir: '/desktop/data' }),
  40. ).resolves.toMatchObject({
  41. dataDir: '/desktop/data',
  42. defaultModel: 'provider/model',
  43. permissionMode: 'auto',
  44. provider: { provider: { options: { apiKey: 'sk-test' } } },
  45. runawayGuard: { enabled: false },
  46. toolResultCompaction: {
  47. enabled: false,
  48. maxInlineKiB: 48,
  49. mcpDetailsMaxInlineKiB: 40,
  50. watermarkKiB: 384,
  51. minSavingsKiB: 192,
  52. minCandidateKiB: 3,
  53. keepRecentRounds: 2,
  54. },
  55. telemetry: { enabled: true },
  56. });
  57. });
  58. it('classifies invalid YAML as a config error and shares canonical field fallback semantics', async () => {
  59. const root = await mkdtemp(join(tmpdir(), 'mcode-headless-config-invalid-'));
  60. roots.push(root);
  61. const malformed = join(root, 'malformed.yaml');
  62. const invalidMode = join(root, 'mode.yaml');
  63. await writeFile(malformed, 'provider: [');
  64. await writeFile(invalidMode, 'permissionMode: maybe\n');
  65. const base = { dataDir: '/desktop/data' };
  66. await expect(loadTuiRuntimeConfig(malformed, base)).rejects.toMatchObject({
  67. kind: 'config',
  68. });
  69. await expect(loadTuiRuntimeConfig(invalidMode, base)).resolves.toMatchObject({
  70. dataDir: '/desktop/data',
  71. permissionMode: 'auto',
  72. runawayGuard: { enabled: true },
  73. toolResultCompaction: {
  74. enabled: true,
  75. maxInlineKiB: 64,
  76. mcpDetailsMaxInlineKiB: 32,
  77. watermarkKiB: 256,
  78. minSavingsKiB: 256,
  79. minCandidateKiB: 2,
  80. keepRecentRounds: 5,
  81. },
  82. telemetry: { enabled: false },
  83. });
  84. });
  85. });