headless-contract.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { describe, expect, it } from 'vitest';
  2. import { createExecResult, isExecResult } from '../../src/headless/contract.js';
  3. describe('mcode exec result contract', () => {
  4. it('emits the single schemaVersion=1 contract with parsed structured output', () => {
  5. const result = createExecResult(
  6. {
  7. sessionId: 'session-1',
  8. turnId: 'turn-1',
  9. status: 'succeeded',
  10. answer: '{"findings":[]}',
  11. usage: { inputTokens: 10, outputTokens: 2 },
  12. durationMs: 25,
  13. },
  14. {
  15. runId: 'run-1',
  16. outputSchema: {
  17. type: 'object',
  18. properties: { findings: { type: 'array' } },
  19. required: ['findings'],
  20. },
  21. model: {
  22. providerId: 'custom_provider:review',
  23. modelId: 'gpt',
  24. protocol: 'openai-responses',
  25. structuredOutputMode: 'native_strict',
  26. },
  27. usage: { inputTokens: 10, outputTokens: 2, totalTokens: 12 },
  28. },
  29. );
  30. expect(result).toEqual({
  31. schemaVersion: 1,
  32. type: 'exec.result',
  33. runId: 'run-1',
  34. sessionId: 'session-1',
  35. turnId: 'turn-1',
  36. status: 'succeeded',
  37. output: { findings: [] },
  38. model: {
  39. providerId: 'custom_provider:review',
  40. modelId: 'gpt',
  41. protocol: 'openai-responses',
  42. structuredOutputMode: 'native_strict',
  43. },
  44. usage: { inputTokens: 10, outputTokens: 2, totalTokens: 12 },
  45. durationMs: 25,
  46. });
  47. expect(isExecResult(result)).toBe(true);
  48. });
  49. it('fails closed when a Provider claims success with invalid structured output', () => {
  50. expect(
  51. createExecResult(
  52. {
  53. sessionId: 'session-1',
  54. turnId: 'turn-1',
  55. status: 'succeeded',
  56. answer: 'not json',
  57. durationMs: 25,
  58. },
  59. { runId: 'run-1', outputSchema: { type: 'object' } },
  60. ),
  61. ).toMatchObject({
  62. status: 'failed',
  63. error: { code: 'STRUCTURED_OUTPUT_INVALID' },
  64. });
  65. });
  66. it('validates parsed output against the requested schema', () => {
  67. expect(
  68. createExecResult(
  69. {
  70. sessionId: 'session-1',
  71. turnId: 'turn-1',
  72. status: 'succeeded',
  73. answer: '{"findings":"none"}',
  74. durationMs: 25,
  75. },
  76. {
  77. runId: 'run-1',
  78. outputSchema: {
  79. type: 'object',
  80. properties: { findings: { type: 'array' } },
  81. required: ['findings'],
  82. },
  83. },
  84. ),
  85. ).toMatchObject({
  86. status: 'failed',
  87. error: {
  88. code: 'STRUCTURED_OUTPUT_INVALID',
  89. message: expect.stringContaining('did not match --output-schema'),
  90. },
  91. });
  92. });
  93. it('never exposes an interactive blocked result', () => {
  94. expect(
  95. createExecResult(
  96. {
  97. sessionId: 'session-1',
  98. turnId: 'turn-1',
  99. status: 'awaiting-user-continuation',
  100. answer: null,
  101. durationMs: 25,
  102. },
  103. { runId: 'run-1' },
  104. ),
  105. ).toMatchObject({
  106. status: 'failed',
  107. error: { code: 'INTERACTION_NOT_AVAILABLE' },
  108. });
  109. });
  110. it('adds optional usage provenance without changing the numeric usage contract', () => {
  111. const result = createExecResult(
  112. { sessionId: 'session-1', turnId: 'turn-1', status: 'cancelled', durationMs: 25 },
  113. { runId: 'run-1', usage: { inputTokens: 10, outputTokens: 2, totalTokens: 12 }, usageSource: 'completed_responses', usageIncomplete: true },
  114. );
  115. expect(result).toMatchObject({ usage: { totalTokens: 12 }, usageSource: 'completed_responses', usageIncomplete: true });
  116. expect(isExecResult(result)).toBe(true);
  117. expect(isExecResult({ ...result, usageSource: 'unknown' })).toBe(false);
  118. expect(isExecResult({ ...result, usageIncomplete: 'false' })).toBe(false);
  119. });
  120. });