model-effort.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { describe, expect, it } from 'vitest';
  2. import type { TuiModel } from '../../src/runtime/port.js';
  3. import {
  4. cycleTuiEffort,
  5. normalizeTuiEffortOptions,
  6. resolveTuiEffortChoice,
  7. supportsTuiEffort,
  8. } from '../../src/tui/features/model/effort.js';
  9. function model(effortOptions?: string[], variant?: string): TuiModel {
  10. return {
  11. providerId: 'custom_provider:byok',
  12. modelId: 'byok-large-5',
  13. ...(effortOptions ? { effortOptions } : {}),
  14. ...(variant !== undefined ? { variant } : {}),
  15. };
  16. }
  17. describe('normalizeTuiEffortOptions', () => {
  18. it('trims, drops blanks and de-duplicates while keeping order', () => {
  19. expect(normalizeTuiEffortOptions([' low ', 'medium', '', 'low', ' ', 'high'])).toEqual([
  20. 'low',
  21. 'medium',
  22. 'high',
  23. ]);
  24. });
  25. it('returns an empty list for missing options', () => {
  26. expect(normalizeTuiEffortOptions(undefined)).toEqual([]);
  27. expect(normalizeTuiEffortOptions([])).toEqual([]);
  28. });
  29. });
  30. describe('supportsTuiEffort', () => {
  31. it('is false without usable options', () => {
  32. expect(supportsTuiEffort(undefined)).toBe(false);
  33. expect(supportsTuiEffort(model())).toBe(false);
  34. expect(supportsTuiEffort(model([' ']))).toBe(false);
  35. });
  36. it('is true once at least one level is configured', () => {
  37. expect(supportsTuiEffort(model(['high']))).toBe(true);
  38. });
  39. });
  40. describe('resolveTuiEffortChoice', () => {
  41. it('restores the saved selection before the catalog default or midpoint', () => {
  42. const selected = {
  43. ...model(['low', 'medium', 'high', 'xhigh', 'max'], 'thinking'),
  44. selected: true,
  45. thinking: { effort: 'max' },
  46. defaultEffort: 'low',
  47. };
  48. expect(resolveTuiEffortChoice(selected)).toBe('max');
  49. expect(resolveTuiEffortChoice(selected, 'xhigh')).toBe('xhigh');
  50. expect(resolveTuiEffortChoice({ ...selected, selected: false })).toBe('low');
  51. });
  52. it('uses the catalog default before the midpoint and supports a fixed default', () => {
  53. expect(
  54. resolveTuiEffortChoice({ ...model(['low', 'medium', 'high']), defaultEffort: 'high' }),
  55. ).toBe('high');
  56. expect(resolveTuiEffortChoice({ ...model(), defaultEffort: 'max' })).toBe('max');
  57. expect(resolveTuiEffortChoice({ ...model(['low', 'high']), defaultEffort: 'removed' })).toBe(
  58. 'high',
  59. );
  60. });
  61. it('does not restore a saved or catalog effort while thinking is off', () => {
  62. for (const variant of ['', 'none-thinking']) {
  63. expect(
  64. resolveTuiEffortChoice(
  65. { ...model(['low', 'high'], variant), defaultEffort: 'high' },
  66. 'high',
  67. ),
  68. ).toBeUndefined();
  69. }
  70. expect(
  71. resolveTuiEffortChoice(
  72. { ...model(['low', 'high']), thinkingConfig: { mode: 'forced_off' } },
  73. 'high',
  74. ),
  75. ).toBeUndefined();
  76. });
  77. it('falls back to the middle level like Runtime does', () => {
  78. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high']))).toBe('medium');
  79. });
  80. it('uses the higher midpoint for even-length lists', () => {
  81. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high', 'max']))).toBe('high');
  82. });
  83. it('keeps a stored level that still exists', () => {
  84. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high']), ' high ')).toBe('high');
  85. });
  86. it('falls back when the stored level was removed from the provider config', () => {
  87. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high']), 'xhigh')).toBe('medium');
  88. });
  89. it('keeps Thinking Off suppressing the configured default', () => {
  90. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high'], ''))).toBeUndefined();
  91. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high'], 'none-thinking'))).toBeUndefined();
  92. });
  93. it('applies the configured default when the legacy variant is thinking-on', () => {
  94. // `thinking` means thinking is on, which is exactly when an effort applies.
  95. // Treating it as "no effort" made Runtime omit the level and the provider
  96. // fall back to its own default.
  97. expect(resolveTuiEffortChoice(model(['xhigh'], 'thinking'))).toBe('xhigh');
  98. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high'], 'thinking'), 'removed')).toBe(
  99. 'medium',
  100. );
  101. });
  102. it('keeps a genuine non-thinking model variant suppressing the default', () => {
  103. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high'], 'fast'))).toBeUndefined();
  104. });
  105. it('lets a valid stored effort coexist with the thinking-on variant', () => {
  106. expect(resolveTuiEffortChoice(model(['low', 'medium', 'high'], 'thinking'), 'high')).toBe(
  107. 'high',
  108. );
  109. });
  110. it('returns undefined when the model exposes no levels', () => {
  111. expect(resolveTuiEffortChoice(model())).toBeUndefined();
  112. expect(resolveTuiEffortChoice(undefined, 'high')).toBeUndefined();
  113. });
  114. });
  115. describe('cycleTuiEffort', () => {
  116. const options = ['low', 'medium', 'high', 'xhigh', 'max'];
  117. it('moves one step in each direction', () => {
  118. expect(cycleTuiEffort(options, 'medium', 1)).toBe('high');
  119. expect(cycleTuiEffort(options, 'medium', -1)).toBe('low');
  120. });
  121. it('clamps at both ends instead of wrapping around', () => {
  122. expect(cycleTuiEffort(options, 'low', -1)).toBe('low');
  123. expect(cycleTuiEffort(options, 'max', 1)).toBe('max');
  124. });
  125. it('starts from the middle level when the current value is unknown', () => {
  126. expect(cycleTuiEffort(options, undefined, 1)).toBe('xhigh');
  127. expect(cycleTuiEffort(options, 'removed', -1)).toBe('medium');
  128. });
  129. it('returns undefined without options', () => {
  130. expect(cycleTuiEffort([], 'low', 1)).toBeUndefined();
  131. expect(cycleTuiEffort(undefined, 'low', 1)).toBeUndefined();
  132. });
  133. });