tui-provider-editor.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { McodeProviderView } from "../../src/provider/contract.js";
  3. import { TuiProviderEditor } from "../../src/tui/features/provider/editor.js";
  4. import { stripAnsi } from "../../src/tui/rendering/text.js";
  5. const provider: McodeProviderView = {
  6. providerId: "custom_provider:work",
  7. name: "Work",
  8. kind: "custom",
  9. active: true,
  10. enabled: true,
  11. readOnly: false,
  12. hasApiKey: true,
  13. baseUrl: "https://models.example/v1",
  14. apiFormat: "openai-completions",
  15. configRevision: "rev-1",
  16. models: [{ modelId: "chat", selected: true }, { modelId: "reasoner" }],
  17. };
  18. function setup(onSave = vi.fn(async () => ({ success: true }))) {
  19. const onSaved = vi.fn();
  20. const onCancel = vi.fn();
  21. const editor = new TuiProviderEditor({
  22. provider,
  23. onSave,
  24. onSaved,
  25. onCancel,
  26. requestRender: vi.fn(),
  27. });
  28. const save = () => {
  29. for (let i = 0; i < 4; i++) editor.handleInput("\u001b[B");
  30. editor.handleInput("\r");
  31. };
  32. return { editor, onSave, onSaved, onCancel, save };
  33. }
  34. describe("TuiProviderEditor", () => {
  35. it("omits unchanged credentials and models from the revision-checked update", async () => {
  36. const { save, onSave } = setup();
  37. save();
  38. await vi.waitFor(() => expect(onSave).toHaveBeenCalledOnce());
  39. expect(onSave).toHaveBeenCalledWith({
  40. providerId: provider.providerId,
  41. expectedRevision: "rev-1",
  42. name: "Work",
  43. baseUrl: provider.baseUrl,
  44. apiFormat: "openai-completions",
  45. modelId: "chat",
  46. saveAndUse: false,
  47. });
  48. });
  49. it("retains the draft on test failure and redacts the replacement Key from errors", async () => {
  50. const onSave = vi
  51. .fn()
  52. .mockResolvedValueOnce({
  53. success: false,
  54. status: { lastErrorMessage: "Rejected new-secret" },
  55. })
  56. .mockResolvedValueOnce({ success: true });
  57. const h = setup(onSave);
  58. h.editor.handleInput("\r");
  59. h.editor.handleInput("new-secret");
  60. h.editor.handleInput("\r");
  61. h.save();
  62. await vi.waitFor(() =>
  63. expect(stripAnsi(h.editor.render(200).join("\n"))).toContain(
  64. "Changes were not saved",
  65. ),
  66. );
  67. expect(h.onSaved).not.toHaveBeenCalled();
  68. expect(stripAnsi(h.editor.render(200).join("\n"))).not.toContain(
  69. "new-secret",
  70. );
  71. h.editor.handleInput("\r");
  72. await vi.waitFor(() => expect(h.onSaved).toHaveBeenCalledWith(true));
  73. expect(onSave.mock.calls[1]?.[0]).toEqual(onSave.mock.calls[0]?.[0]);
  74. });
  75. it("cancels a replacement without saving it", () => {
  76. const h = setup();
  77. h.editor.handleInput("\r");
  78. h.editor.handleInput("discarded-key");
  79. h.editor.handleInput("\u001b");
  80. h.editor.handleInput("\u001b");
  81. expect(h.onCancel).toHaveBeenCalledOnce();
  82. expect(h.onSave).not.toHaveBeenCalled();
  83. expect(stripAnsi(h.editor.render(100).join("\n"))).not.toContain(
  84. "discarded-key",
  85. );
  86. });
  87. it("validates the URL and persists an explicitly edited model list", async () => {
  88. const h = setup();
  89. h.editor.handleInput("\u001b[B");
  90. h.editor.handleInput("\r");
  91. h.editor.handleInput("\u0001");
  92. h.editor.handleInput("\u000b");
  93. h.editor.handleInput("ftp://invalid");
  94. h.editor.handleInput("\r");
  95. expect(stripAnsi(h.editor.render(120).join("\n"))).toContain(
  96. "Base URL must use http or https",
  97. );
  98. h.editor.handleInput("\u001b");
  99. h.editor.handleInput("\u001b[B");
  100. h.editor.handleInput("\r");
  101. h.editor.handleInput("\u0001");
  102. h.editor.handleInput("\u000b");
  103. h.editor.handleInput("chat, new-model, new-model");
  104. h.editor.handleInput("\r");
  105. h.editor.handleInput("\u001b[B");
  106. h.editor.handleInput("\u001b[B");
  107. h.editor.handleInput("\r");
  108. await vi.waitFor(() => expect(h.onSave).toHaveBeenCalledOnce());
  109. expect(h.onSave).toHaveBeenCalledWith(
  110. expect.objectContaining({
  111. models: [{ modelId: "chat" }, { modelId: "new-model" }],
  112. baseUrl: provider.baseUrl,
  113. }),
  114. );
  115. });
  116. it("does not reopen a disposed editor after a pending save", async () => {
  117. let resolve!: (result: { success: boolean }) => void;
  118. const h = setup(
  119. vi.fn(
  120. () =>
  121. new Promise((done) => {
  122. resolve = done;
  123. }),
  124. ),
  125. );
  126. h.save();
  127. h.editor.dispose();
  128. resolve({ success: true });
  129. await Promise.resolve();
  130. expect(h.onSaved).not.toHaveBeenCalled();
  131. });
  132. });