@marimo-team/islands 0.25.3-dev388 → 0.25.3-dev391
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ErrorBoundary-_mFP3LkM.js → ErrorBoundary-DNRG0xHi.js} +1 -1
- package/dist/{chat-ui-BKSYhGMh.js → chat-ui-CWVNPm2l.js} +769 -769
- package/dist/{code-visibility-DkMNjdNe.js → code-visibility-Ca8sDptK.js} +10 -10
- package/dist/{common-Ces9ooG_.js → common-B3NYpak4.js} +4910 -4902
- package/dist/{data-grid-overlay-editor-uNMslVKY.js → data-grid-overlay-editor-Cz7uMmtm.js} +1 -1
- package/dist/{error-entries-rtPWQkJk.js → error-entries-CnXf4kpP.js} +821 -686
- package/dist/{feedback-button-i960qUnq.js → feedback-button-BQbnZsf5.js} +7 -7
- package/dist/{glide-data-editor-_EEhlGzS.js → glide-data-editor-Bxc30klU.js} +3 -3
- package/dist/{globals-DKkjIolp.js → globals-CDatnx0Y.js} +1 -1
- package/dist/{html-to-image-B5nTOeMI.js → html-to-image-Bu_-vH_Y.js} +4782 -4765
- package/dist/main.js +6233 -6172
- package/dist/{number-overlay-editor-D5U7jbrh.js → number-overlay-editor-ChGu5C-s.js} +1 -1
- package/dist/{process-output-DNxpsd4n.js → process-output-Rbqr1aYD.js} +2 -2
- package/dist/{reveal-component-BM9TozVQ.js → reveal-component-CSxWJFAP.js} +32 -32
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/__mocks__/executions.ts +48 -8
- package/src/__mocks__/notebook.ts +20 -42
- package/src/components/app-config/__tests__/add-model-discovery.test.tsx +255 -0
- package/src/components/app-config/__tests__/model-discovery.test.tsx +183 -0
- package/src/components/app-config/ai-config.tsx +20 -24
- package/src/components/app-config/model-discovery.tsx +118 -184
- package/src/components/app-config/model-provider-config.ts +25 -0
- package/src/components/editor/Output.tsx +5 -4
- package/src/components/editor/__tests__/data-attributes.test.tsx +1 -6
- package/src/components/editor/__tests__/dynamic-favicon.test.tsx +11 -16
- package/src/components/editor/__tests__/output-persistence.test.tsx +6 -24
- package/src/components/editor/actions/__tests__/useSetOutputsExpanded.test.ts +6 -8
- package/src/components/editor/actions/useSetOutputsExpanded.ts +12 -4
- package/src/components/editor/cell/cell-context-menu.tsx +3 -2
- package/src/components/editor/errors/__tests__/send-error-report-button.test.tsx +89 -72
- package/src/components/editor/notebook-cell.tsx +15 -10
- package/src/components/editor/output/console/process-output.ts +4 -2
- package/src/components/editor/renderers/cells-renderer.tsx +3 -1
- package/src/components/editor/renderers/grid-layout/__tests__/grid-layout.test.tsx +5 -11
- package/src/components/editor/renderers/grid-layout/grid-layout.tsx +9 -8
- package/src/components/editor/renderers/types.ts +2 -2
- package/src/components/editor/renderers/vertical-layout/utils.ts +4 -4
- package/src/components/editor/renderers/vertical-layout/vertical-layout.tsx +5 -8
- package/src/components/scratchpad/scratchpad.tsx +6 -2
- package/src/components/slides/__tests__/cell-output-block.test.tsx +7 -8
- package/src/components/slides/__tests__/minimap-actions.test.tsx +3 -3
- package/src/components/slides/__tests__/slide-cell-view.test.tsx +1 -0
- package/src/components/slides/slide-cell-view.tsx +1 -3
- package/src/components/slides/slide.tsx +3 -3
- package/src/core/ai/context/__tests__/registry.test.ts +7 -11
- package/src/core/ai/context/providers/__tests__/cell-output.test.ts +113 -35
- package/src/core/ai/context/providers/__tests__/error.test.ts +74 -191
- package/src/core/ai/context/providers/cell-output.ts +11 -6
- package/src/core/cells/__tests__/__snapshots__/cells.test.ts.snap +8 -103
- package/src/core/cells/__tests__/cells.test.ts +40 -292
- package/src/core/cells/__tests__/clear-outputs.test.ts +151 -0
- package/src/core/cells/__tests__/harness.ts +14 -12
- package/src/core/cells/__tests__/layout.test.ts +20 -26
- package/src/core/cells/__tests__/notebook-outline.test.ts +49 -43
- package/src/core/cells/__tests__/session.test.ts +26 -146
- package/src/core/cells/actions.ts +49 -9
- package/src/core/cells/cell.ts +0 -13
- package/src/core/cells/cells.ts +53 -62
- package/src/core/cells/focus.ts +6 -1
- package/src/core/cells/layout.ts +2 -3
- package/src/core/cells/outputs.ts +2 -2
- package/src/core/cells/session.ts +3 -38
- package/src/core/cells/types.ts +6 -8
- package/src/core/cells/utils.ts +4 -3
- package/src/core/codemirror/ai/__tests__/resources.test.ts +7 -11
- package/src/core/errors/__tests__/error-entries.test.ts +52 -18
- package/src/core/errors/error-entries.ts +21 -11
- package/src/core/executions/__tests__/freshness.test.ts +7 -4
- package/src/core/executions/__tests__/hooks.test.tsx +5 -1
- package/src/core/executions/__tests__/interrupt.test.ts +2 -0
- package/src/core/executions/__tests__/output-lifecycle.test.ts +201 -0
- package/src/core/executions/__tests__/output.test.ts +87 -3
- package/src/core/executions/__tests__/select.test.ts +17 -10
- package/src/core/executions/atoms.ts +73 -15
- package/src/core/executions/freshness.ts +1 -1
- package/src/core/executions/hooks.ts +18 -8
- package/src/core/executions/index.ts +6 -0
- package/src/core/executions/output.ts +122 -38
- package/src/core/executions/select.ts +2 -2
- package/src/core/export/__tests__/hooks.test.ts +20 -29
- package/src/core/export/hooks.ts +7 -9
- package/src/core/islands/__tests__/iframe-resizer.test.ts +91 -0
- package/src/core/islands/__tests__/main.test.ts +17 -0
- package/src/core/islands/components/__tests__/web-components.test.tsx +13 -15
- package/src/core/islands/components/output-wrapper.tsx +15 -20
- package/src/core/islands/iframe-resizer.ts +38 -0
- package/src/core/islands/main.ts +3 -0
- package/src/core/lifecycle/render-policy.ts +7 -4
- package/src/core/network/__tests__/ai.test.ts +41 -0
- package/src/core/network/ai.ts +12 -0
- package/src/core/notebook/document/__tests__/codemirror.test.ts +2 -0
- package/src/core/notebook/replica/__tests__/replica.test.ts +19 -0
- package/src/core/notebook/replica/__tests__/results.test.ts +45 -16
- package/src/core/notebook/replica/index.ts +1 -1
- package/src/core/notebook/replica/replica.ts +19 -4
- package/src/core/notebook/replica/resources/executions.ts +100 -9
- package/src/core/notebook/replica/resources/results.ts +46 -31
- package/src/core/transports/__tests__/static.test.ts +5 -3
- package/src/stories/cell.stories.tsx +40 -30
- package/src/stories/layout/vertical/one-column.stories.tsx +15 -7
package/package.json
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
type NotebookBackend,
|
|
15
15
|
type NotebookReplica,
|
|
16
16
|
type Results,
|
|
17
|
+
type Result,
|
|
17
18
|
} from "@/core/notebook/replica";
|
|
18
19
|
import { activeNotebookReplicaAtom } from "@/core/session/active-notebook";
|
|
19
20
|
|
|
@@ -52,7 +53,7 @@ function completed(
|
|
|
52
53
|
};
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
function mirror(records: Protocol.Execution[]): Executions {
|
|
56
|
+
function mirror(records: readonly Protocol.Execution[]): Executions {
|
|
56
57
|
const executions = new Map<Protocol.ExecutionId, Execution>();
|
|
57
58
|
const latest = new Map<Protocol.CellId, Protocol.ExecutionId>();
|
|
58
59
|
for (const record of records) {
|
|
@@ -66,18 +67,34 @@ function mirror(records: Protocol.Execution[]): Executions {
|
|
|
66
67
|
}
|
|
67
68
|
executions.set(record.id, {
|
|
68
69
|
record,
|
|
70
|
+
outputVersion: 0,
|
|
69
71
|
items: new Map(),
|
|
70
72
|
variables: new Map(),
|
|
71
73
|
});
|
|
72
74
|
}
|
|
73
|
-
|
|
75
|
+
const scratch = records.findLast(
|
|
76
|
+
(record) => record.cell_id === null && record.outcome !== null,
|
|
77
|
+
);
|
|
78
|
+
return {
|
|
79
|
+
runs: new Map(),
|
|
80
|
+
executions,
|
|
81
|
+
latest,
|
|
82
|
+
results: results(records),
|
|
83
|
+
scratch:
|
|
84
|
+
scratch === undefined ? null : { record: scratch, outputVersion: 0 },
|
|
85
|
+
};
|
|
74
86
|
}
|
|
75
87
|
|
|
76
|
-
function results(records: Protocol.Execution[]): Results {
|
|
77
|
-
const results = new Map<Protocol.CellId,
|
|
88
|
+
function results(records: readonly Protocol.Execution[]): Results {
|
|
89
|
+
const results = new Map<Protocol.CellId, Result | null>();
|
|
78
90
|
for (const record of records) {
|
|
79
|
-
if (record.cell_id !== null
|
|
80
|
-
results.
|
|
91
|
+
if (record.cell_id !== null) {
|
|
92
|
+
if (!results.has(record.cell_id)) {
|
|
93
|
+
results.set(record.cell_id, null);
|
|
94
|
+
}
|
|
95
|
+
if (record.status === "completed") {
|
|
96
|
+
results.set(record.cell_id, { record, outputVersion: 0 });
|
|
97
|
+
}
|
|
81
98
|
}
|
|
82
99
|
}
|
|
83
100
|
return results;
|
|
@@ -95,7 +112,24 @@ export const MockExecutions = {
|
|
|
95
112
|
});
|
|
96
113
|
},
|
|
97
114
|
|
|
98
|
-
|
|
115
|
+
/** A completed execution that showed `display`. */
|
|
116
|
+
shown(
|
|
117
|
+
cellId: string,
|
|
118
|
+
display: Protocol.DisplayData,
|
|
119
|
+
overrides: Partial<Protocol.Execution> = {},
|
|
120
|
+
): Protocol.Execution {
|
|
121
|
+
return completed(cellId, {
|
|
122
|
+
outcome: { kind: "value", display },
|
|
123
|
+
...overrides,
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
/** A completed execution that ended in `error`, a ValueError by default. */
|
|
128
|
+
failed(
|
|
129
|
+
cellId: string,
|
|
130
|
+
error: Partial<Protocol.Error> = {},
|
|
131
|
+
overrides: Partial<Protocol.Execution> = {},
|
|
132
|
+
): Protocol.Execution {
|
|
99
133
|
return completed(cellId, {
|
|
100
134
|
outcome: {
|
|
101
135
|
kind: "error",
|
|
@@ -108,11 +142,17 @@ export const MockExecutions = {
|
|
|
108
142
|
severity: "error",
|
|
109
143
|
range: null,
|
|
110
144
|
related: [],
|
|
145
|
+
...error,
|
|
111
146
|
},
|
|
112
147
|
},
|
|
148
|
+
...overrides,
|
|
113
149
|
});
|
|
114
150
|
},
|
|
115
151
|
|
|
152
|
+
errored(cellId: string): Protocol.Execution {
|
|
153
|
+
return MockExecutions.failed(cellId);
|
|
154
|
+
},
|
|
155
|
+
|
|
116
156
|
interrupted(cellId: string): Protocol.Execution {
|
|
117
157
|
return completed(cellId, { outcome: { kind: "interrupted" } });
|
|
118
158
|
},
|
|
@@ -173,7 +213,7 @@ export const MockExecutions = {
|
|
|
173
213
|
* with nothing keeping it from running.
|
|
174
214
|
*/
|
|
175
215
|
inputs(
|
|
176
|
-
records: Protocol.Execution[],
|
|
216
|
+
records: readonly Protocol.Execution[],
|
|
177
217
|
extra: Partial<Pick<CellExecutionInputs, "requested" | "blocked">> = {},
|
|
178
218
|
): CellExecutionInputs {
|
|
179
219
|
return {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
|
+
import type * as Protocol from "@marimo-team/client/protocol";
|
|
4
|
+
import type { createStore } from "jotai";
|
|
3
5
|
import { createRef } from "react";
|
|
4
6
|
import { vi } from "vitest";
|
|
5
7
|
import type { CellActions } from "@/core/cells/actions";
|
|
6
|
-
import type
|
|
8
|
+
import { type NotebookState, notebookAtom } from "@/core/cells/cells";
|
|
7
9
|
import { CellId } from "@/core/cells/ids";
|
|
8
10
|
import {
|
|
9
11
|
type CellData,
|
|
@@ -12,6 +14,7 @@ import {
|
|
|
12
14
|
} from "@/core/cells/types";
|
|
13
15
|
import type { MarimoError } from "@/core/kernel/messages";
|
|
14
16
|
import { Objects } from "@/utils/objects";
|
|
17
|
+
import { MockExecutions } from "./executions";
|
|
15
18
|
|
|
16
19
|
export const MockNotebook = {
|
|
17
20
|
*cellIds() {
|
|
@@ -134,57 +137,32 @@ export const MockNotebook = {
|
|
|
134
137
|
},
|
|
135
138
|
|
|
136
139
|
/**
|
|
137
|
-
*
|
|
140
|
+
* Set `store` to a notebook whose cells each ended in a host error.
|
|
141
|
+
*
|
|
142
|
+
* The host reports one error per execution, so each cell gets one.
|
|
138
143
|
*/
|
|
139
|
-
|
|
144
|
+
withErrors: (
|
|
145
|
+
store: ReturnType<typeof createStore>,
|
|
140
146
|
errors: {
|
|
141
147
|
cellId: CellId;
|
|
142
148
|
cellName: string;
|
|
143
|
-
|
|
149
|
+
code?: string;
|
|
150
|
+
error: Partial<Protocol.Error>;
|
|
144
151
|
}[],
|
|
145
152
|
): NotebookState => {
|
|
146
153
|
const cellData: Record<string, Partial<CellData>> = {};
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
154
|
+
for (const { cellId, cellName, code } of errors) {
|
|
155
|
+
cellData[cellId] = {
|
|
156
|
+
name: cellName,
|
|
157
|
+
...(code === undefined ? {} : { code }),
|
|
151
158
|
};
|
|
152
159
|
}
|
|
153
|
-
|
|
154
160
|
const notebookState = MockNotebook.notebookState({ cellData });
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
output: {
|
|
161
|
-
channel: "marimo-error" as const,
|
|
162
|
-
data: error.errorData,
|
|
163
|
-
mimetype: "application/vnd.marimo+error" as const,
|
|
164
|
-
timestamp: Date.now(),
|
|
165
|
-
},
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
161
|
+
store.set(notebookAtom, notebookState);
|
|
162
|
+
MockExecutions.seed(
|
|
163
|
+
store,
|
|
164
|
+
errors.map(({ cellId, error }) => MockExecutions.failed(cellId, error)),
|
|
165
|
+
);
|
|
169
166
|
return notebookState;
|
|
170
167
|
},
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Create a single cell with errors for quick testing
|
|
174
|
-
*/
|
|
175
|
-
cellWithErrors: (cellName: string, errorData: MarimoError[]) => {
|
|
176
|
-
const cellId = CellId.create();
|
|
177
|
-
return {
|
|
178
|
-
cellId,
|
|
179
|
-
cellName,
|
|
180
|
-
errorData,
|
|
181
|
-
notebookState: MockNotebook.notebookStateWithErrors([
|
|
182
|
-
{
|
|
183
|
-
cellId,
|
|
184
|
-
cellName,
|
|
185
|
-
errorData,
|
|
186
|
-
},
|
|
187
|
-
]),
|
|
188
|
-
};
|
|
189
|
-
},
|
|
190
168
|
};
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
act,
|
|
5
|
+
fireEvent,
|
|
6
|
+
render,
|
|
7
|
+
screen,
|
|
8
|
+
waitFor,
|
|
9
|
+
} from "@testing-library/react";
|
|
10
|
+
import { createStore, Provider } from "jotai";
|
|
11
|
+
import { Controller, useForm } from "react-hook-form";
|
|
12
|
+
import { describe, expect, it, vi } from "vitest";
|
|
13
|
+
import { userConfigAtom } from "@/core/config/config";
|
|
14
|
+
import {
|
|
15
|
+
defaultUserConfig,
|
|
16
|
+
type UserConfig,
|
|
17
|
+
} from "@/core/config/config-schema";
|
|
18
|
+
import { listProviderModels } from "@/core/network/ai";
|
|
19
|
+
import type { EditRequests } from "@/core/network/types";
|
|
20
|
+
import { AddModelForm } from "../ai-config";
|
|
21
|
+
import { aiSettingsSubTabAtom, pendingConfigSavesAtom } from "../state";
|
|
22
|
+
import {
|
|
23
|
+
activeUserConfigCategoryAtom,
|
|
24
|
+
UserConfigForm,
|
|
25
|
+
} from "../user-config-form";
|
|
26
|
+
|
|
27
|
+
const saveUserConfig = vi.hoisted(() =>
|
|
28
|
+
vi.fn<EditRequests["saveUserConfig"]>(),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
vi.mock("@/core/network/ai", () => ({ listProviderModels: vi.fn() }));
|
|
32
|
+
vi.mock("@/core/network/requests", async (importOriginal) => ({
|
|
33
|
+
...(await importOriginal<typeof import("@/core/network/requests")>()),
|
|
34
|
+
useRequestClient: () => ({ saveUserConfig }),
|
|
35
|
+
}));
|
|
36
|
+
vi.mock("@uidotdev/usehooks", async (importOriginal) => ({
|
|
37
|
+
...(await importOriginal<typeof import("@uidotdev/usehooks")>()),
|
|
38
|
+
useMediaQuery: () => true,
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
Element.prototype.scrollIntoView = vi.fn();
|
|
42
|
+
|
|
43
|
+
const search = () =>
|
|
44
|
+
screen.getByRole("button", { name: "Search available models" });
|
|
45
|
+
|
|
46
|
+
async function openAddModel(providerName: string) {
|
|
47
|
+
fireEvent.click(screen.getByRole("button", { name: "Add model" }));
|
|
48
|
+
fireEvent.click(screen.getByRole("combobox"));
|
|
49
|
+
fireEvent.click(
|
|
50
|
+
await screen.findByRole("option", {
|
|
51
|
+
name: new RegExp(providerName, "i"),
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function ModelForm({
|
|
57
|
+
providerName,
|
|
58
|
+
savedConfig,
|
|
59
|
+
onSubmit,
|
|
60
|
+
}: {
|
|
61
|
+
providerName: string;
|
|
62
|
+
savedConfig: UserConfig;
|
|
63
|
+
onSubmit: (config: UserConfig) => void;
|
|
64
|
+
}) {
|
|
65
|
+
const form = useForm<UserConfig>({ defaultValues: savedConfig });
|
|
66
|
+
return (
|
|
67
|
+
<>
|
|
68
|
+
<Controller
|
|
69
|
+
control={form.control}
|
|
70
|
+
name={`ai.custom_providers.${providerName}.api_key`}
|
|
71
|
+
render={() => <span />}
|
|
72
|
+
/>
|
|
73
|
+
<button
|
|
74
|
+
type="button"
|
|
75
|
+
onClick={() =>
|
|
76
|
+
form.setValue(
|
|
77
|
+
`ai.custom_providers.${providerName}.base_url`,
|
|
78
|
+
"https://changed.example.com/v1",
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
>
|
|
82
|
+
Change URL
|
|
83
|
+
</button>
|
|
84
|
+
<AddModelForm
|
|
85
|
+
form={form}
|
|
86
|
+
config={savedConfig}
|
|
87
|
+
customModels={[]}
|
|
88
|
+
customProviderNames={[providerName]}
|
|
89
|
+
onSubmit={onSubmit}
|
|
90
|
+
/>
|
|
91
|
+
</>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
describe("AddModelForm discovery", () => {
|
|
96
|
+
it.each(["gateway", "openrouter"])(
|
|
97
|
+
"waits for %s changes to be saved and only saves a selected model on Add",
|
|
98
|
+
async (providerName) => {
|
|
99
|
+
vi.mocked(listProviderModels)
|
|
100
|
+
.mockReset()
|
|
101
|
+
.mockResolvedValue({
|
|
102
|
+
models: [{ id: "model-a", name: "Model A" }],
|
|
103
|
+
});
|
|
104
|
+
const config = defaultUserConfig();
|
|
105
|
+
config.ai = {
|
|
106
|
+
...config.ai,
|
|
107
|
+
custom_providers: {
|
|
108
|
+
[providerName]: { base_url: "https://original.example.com/v1" },
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
const store = createStore();
|
|
112
|
+
store.set(userConfigAtom, config);
|
|
113
|
+
const onSubmit = vi.fn();
|
|
114
|
+
const { rerender } = render(
|
|
115
|
+
<Provider store={store}>
|
|
116
|
+
<ModelForm
|
|
117
|
+
providerName={providerName}
|
|
118
|
+
savedConfig={config}
|
|
119
|
+
onSubmit={onSubmit}
|
|
120
|
+
/>
|
|
121
|
+
</Provider>,
|
|
122
|
+
);
|
|
123
|
+
await openAddModel(providerName);
|
|
124
|
+
expect(search()).toBeEnabled();
|
|
125
|
+
fireEvent.click(search());
|
|
126
|
+
fireEvent.click(
|
|
127
|
+
await screen.findByRole("button", { name: "Model A — model-a" }),
|
|
128
|
+
);
|
|
129
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
130
|
+
|
|
131
|
+
fireEvent.click(screen.getByRole("button", { name: "Change URL" }));
|
|
132
|
+
expect(search()).toBeDisabled();
|
|
133
|
+
const savedConfig = {
|
|
134
|
+
...config,
|
|
135
|
+
ai: {
|
|
136
|
+
...config.ai,
|
|
137
|
+
custom_providers: {
|
|
138
|
+
[providerName]: { base_url: "https://changed.example.com/v1" },
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
act(() => {
|
|
143
|
+
store.set(pendingConfigSavesAtom, 1);
|
|
144
|
+
store.set(userConfigAtom, savedConfig);
|
|
145
|
+
});
|
|
146
|
+
rerender(
|
|
147
|
+
<Provider store={store}>
|
|
148
|
+
<ModelForm
|
|
149
|
+
providerName={providerName}
|
|
150
|
+
savedConfig={savedConfig}
|
|
151
|
+
onSubmit={onSubmit}
|
|
152
|
+
/>
|
|
153
|
+
</Provider>,
|
|
154
|
+
);
|
|
155
|
+
expect(search()).toBeDisabled();
|
|
156
|
+
act(() => store.set(pendingConfigSavesAtom, 0));
|
|
157
|
+
await waitFor(() => expect(search()).toBeEnabled());
|
|
158
|
+
fireEvent.focus(screen.getByLabelText("Model"));
|
|
159
|
+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
160
|
+
|
|
161
|
+
vi.mocked(listProviderModels).mockResolvedValue({
|
|
162
|
+
models: [{ id: "model-b", name: "Model B" }],
|
|
163
|
+
});
|
|
164
|
+
fireEvent.change(screen.getByLabelText("Model"), {
|
|
165
|
+
target: { value: "" },
|
|
166
|
+
});
|
|
167
|
+
fireEvent.click(search());
|
|
168
|
+
fireEvent.click(
|
|
169
|
+
await screen.findByRole("button", { name: "Model B — model-b" }),
|
|
170
|
+
);
|
|
171
|
+
expect(listProviderModels).toHaveBeenCalledTimes(2);
|
|
172
|
+
expect(listProviderModels).toHaveBeenLastCalledWith(providerName);
|
|
173
|
+
|
|
174
|
+
fireEvent.click(screen.getByRole("button", { name: /^Add$/ }));
|
|
175
|
+
expect(onSubmit).toHaveBeenCalledWith(
|
|
176
|
+
expect.objectContaining({
|
|
177
|
+
ai: expect.objectContaining({
|
|
178
|
+
models: expect.objectContaining({
|
|
179
|
+
custom_models: [`${providerName}/model-b`],
|
|
180
|
+
}),
|
|
181
|
+
}),
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
},
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
it("keeps model entry focused across settings saves and only adds models on Add", async () => {
|
|
188
|
+
const config = defaultUserConfig();
|
|
189
|
+
config.ai = {
|
|
190
|
+
...config.ai,
|
|
191
|
+
custom_providers: {
|
|
192
|
+
gateway: { base_url: "https://gateway.example.com/v1" },
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
const store = createStore();
|
|
196
|
+
store.set(userConfigAtom, config);
|
|
197
|
+
store.set(activeUserConfigCategoryAtom, "ai");
|
|
198
|
+
store.set(aiSettingsSubTabAtom, "ai-models");
|
|
199
|
+
saveUserConfig.mockReset().mockResolvedValue(null);
|
|
200
|
+
vi.mocked(listProviderModels)
|
|
201
|
+
.mockReset()
|
|
202
|
+
.mockResolvedValue({
|
|
203
|
+
models: [{ id: "second-model", name: "Second model" }],
|
|
204
|
+
});
|
|
205
|
+
render(
|
|
206
|
+
<Provider store={store}>
|
|
207
|
+
<UserConfigForm />
|
|
208
|
+
</Provider>,
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
await openAddModel("gateway");
|
|
212
|
+
fireEvent.change(screen.getByLabelText("Model"), {
|
|
213
|
+
target: { value: "first-model" },
|
|
214
|
+
});
|
|
215
|
+
await act(async () => {
|
|
216
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
217
|
+
});
|
|
218
|
+
fireEvent.click(screen.getByRole("button", { name: /^Add$/ }));
|
|
219
|
+
await waitFor(() => expect(saveUserConfig).toHaveBeenCalledOnce());
|
|
220
|
+
await waitFor(() => expect(store.get(pendingConfigSavesAtom)).toBe(0));
|
|
221
|
+
|
|
222
|
+
await openAddModel("gateway");
|
|
223
|
+
fireEvent.click(search());
|
|
224
|
+
await screen.findByRole("button", { name: "Second model — second-model" });
|
|
225
|
+
const pendingSave = Promise.withResolvers<null>();
|
|
226
|
+
saveUserConfig.mockReturnValueOnce(pendingSave.promise);
|
|
227
|
+
const input = screen.getByLabelText("Model");
|
|
228
|
+
act(() => input.focus());
|
|
229
|
+
fireEvent.change(input, { target: { value: "second" } });
|
|
230
|
+
await waitFor(() => expect(saveUserConfig).toHaveBeenCalledTimes(2));
|
|
231
|
+
expect(store.get(pendingConfigSavesAtom)).toBe(1);
|
|
232
|
+
expect(screen.getByLabelText("Model")).toBe(input);
|
|
233
|
+
expect(input).toHaveFocus();
|
|
234
|
+
|
|
235
|
+
await act(async () => pendingSave.resolve(null));
|
|
236
|
+
await screen.findByRole("button", { name: "Second model — second-model" });
|
|
237
|
+
expect(screen.getByLabelText("Model")).toBe(input);
|
|
238
|
+
expect(input).toHaveFocus();
|
|
239
|
+
expect(input).toHaveValue("second");
|
|
240
|
+
expect(store.get(userConfigAtom).ai?.models?.custom_models).toEqual([
|
|
241
|
+
"gateway/first-model",
|
|
242
|
+
]);
|
|
243
|
+
|
|
244
|
+
fireEvent.click(
|
|
245
|
+
screen.getByRole("button", { name: "Second model — second-model" }),
|
|
246
|
+
);
|
|
247
|
+
fireEvent.click(screen.getByRole("button", { name: /^Add$/ }));
|
|
248
|
+
await waitFor(() =>
|
|
249
|
+
expect(store.get(userConfigAtom).ai?.models?.custom_models).toEqual([
|
|
250
|
+
"gateway/second-model",
|
|
251
|
+
"gateway/first-model",
|
|
252
|
+
]),
|
|
253
|
+
);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
act,
|
|
5
|
+
fireEvent,
|
|
6
|
+
render,
|
|
7
|
+
screen,
|
|
8
|
+
waitFor,
|
|
9
|
+
} from "@testing-library/react";
|
|
10
|
+
import { useState } from "react";
|
|
11
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
12
|
+
import { listProviderModels } from "@/core/network/ai";
|
|
13
|
+
import { HTTPError } from "@/utils/errors";
|
|
14
|
+
import { ModelDiscovery } from "../model-discovery";
|
|
15
|
+
|
|
16
|
+
vi.mock("@/core/network/ai", () => ({ listProviderModels: vi.fn() }));
|
|
17
|
+
vi.mock("@/core/wasm/utils", () => ({ isWasm: () => false }));
|
|
18
|
+
|
|
19
|
+
function Discovery({
|
|
20
|
+
configuration = "saved",
|
|
21
|
+
provider = "gateway",
|
|
22
|
+
disabled = false,
|
|
23
|
+
}) {
|
|
24
|
+
const [value, setValue] = useState("");
|
|
25
|
+
return (
|
|
26
|
+
<ModelDiscovery
|
|
27
|
+
provider={provider}
|
|
28
|
+
configuration={{ base_url: `https://${configuration}.example.com/v1` }}
|
|
29
|
+
label="Model"
|
|
30
|
+
inputId="model"
|
|
31
|
+
disabled={disabled}
|
|
32
|
+
value={value}
|
|
33
|
+
onValueChange={setValue}
|
|
34
|
+
/>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const search = () =>
|
|
39
|
+
screen.getByRole("button", { name: "Search available models" });
|
|
40
|
+
|
|
41
|
+
describe("ModelDiscovery", () => {
|
|
42
|
+
beforeEach(() => vi.mocked(listProviderModels).mockReset());
|
|
43
|
+
|
|
44
|
+
it("loads on demand and fills the editable model field on selection", async () => {
|
|
45
|
+
vi.mocked(listProviderModels).mockResolvedValue({
|
|
46
|
+
models: [{ id: "org/model", name: "Model name" }],
|
|
47
|
+
});
|
|
48
|
+
render(<Discovery />);
|
|
49
|
+
expect(listProviderModels).not.toHaveBeenCalled();
|
|
50
|
+
|
|
51
|
+
fireEvent.change(screen.getByLabelText("Model"), {
|
|
52
|
+
target: { value: "manual-id" },
|
|
53
|
+
});
|
|
54
|
+
expect(screen.getByLabelText("Model")).toHaveValue("manual-id");
|
|
55
|
+
fireEvent.change(screen.getByLabelText("Model"), { target: { value: "" } });
|
|
56
|
+
fireEvent.click(search());
|
|
57
|
+
await screen.findByRole("button", { name: "Model name — org/model" });
|
|
58
|
+
fireEvent.keyDown(screen.getByLabelText("Model"), { key: "Escape" });
|
|
59
|
+
fireEvent.keyDown(screen.getByLabelText("Model"), { key: "ArrowDown" });
|
|
60
|
+
await waitFor(() =>
|
|
61
|
+
expect(
|
|
62
|
+
screen.getByRole("button", { name: "Model name — org/model" }),
|
|
63
|
+
).toHaveFocus(),
|
|
64
|
+
);
|
|
65
|
+
fireEvent.click(
|
|
66
|
+
screen.getByRole("button", { name: "Model name — org/model" }),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(listProviderModels).toHaveBeenCalledExactlyOnceWith("gateway");
|
|
70
|
+
expect(screen.getByLabelText("Model")).toHaveValue("org/model");
|
|
71
|
+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("shows backend error details and allows retrying", async () => {
|
|
75
|
+
vi.mocked(listProviderModels)
|
|
76
|
+
.mockRejectedValueOnce(
|
|
77
|
+
new HTTPError(400, "Bad Request", {
|
|
78
|
+
detail: "Provider rejected credentials.",
|
|
79
|
+
}),
|
|
80
|
+
)
|
|
81
|
+
.mockResolvedValueOnce({ models: [] });
|
|
82
|
+
render(<Discovery />);
|
|
83
|
+
fireEvent.click(search());
|
|
84
|
+
expect(await screen.findByRole("alert")).toHaveTextContent(
|
|
85
|
+
"Provider rejected credentials.",
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
fireEvent.click(search());
|
|
89
|
+
expect(await screen.findByText("No models found")).toBeInTheDocument();
|
|
90
|
+
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("keeps focus in the input when reopening after navigating an open catalog", async () => {
|
|
94
|
+
vi.mocked(listProviderModels).mockResolvedValue({
|
|
95
|
+
models: [{ id: "model-a", name: "Model A" }],
|
|
96
|
+
});
|
|
97
|
+
render(<Discovery />);
|
|
98
|
+
fireEvent.click(search());
|
|
99
|
+
const option = await screen.findByRole("button", {
|
|
100
|
+
name: "Model A — model-a",
|
|
101
|
+
});
|
|
102
|
+
const input = screen.getByLabelText("Model");
|
|
103
|
+
fireEvent.keyDown(input, { key: "ArrowDown" });
|
|
104
|
+
expect(option).toHaveFocus();
|
|
105
|
+
fireEvent.keyDown(input, { key: "Escape" });
|
|
106
|
+
await waitFor(() =>
|
|
107
|
+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument(),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
act(() => input.focus());
|
|
111
|
+
await screen.findByRole("dialog");
|
|
112
|
+
await waitFor(() => expect(input).toHaveFocus());
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("discards outstanding results when the saved configuration changes", async () => {
|
|
116
|
+
const pending =
|
|
117
|
+
Promise.withResolvers<Awaited<ReturnType<typeof listProviderModels>>>();
|
|
118
|
+
vi.mocked(listProviderModels).mockReturnValueOnce(pending.promise);
|
|
119
|
+
const { rerender } = render(<Discovery />);
|
|
120
|
+
fireEvent.click(search());
|
|
121
|
+
await waitFor(() => expect(listProviderModels).toHaveBeenCalledOnce());
|
|
122
|
+
const input = screen.getByLabelText("Model");
|
|
123
|
+
act(() => input.focus());
|
|
124
|
+
|
|
125
|
+
rerender(<Discovery configuration="changed" disabled={true} />);
|
|
126
|
+
expect(screen.getByLabelText("Model")).toBe(input);
|
|
127
|
+
expect(input).toHaveFocus();
|
|
128
|
+
expect(search()).toBeDisabled();
|
|
129
|
+
await act(async () =>
|
|
130
|
+
pending.resolve({ models: [{ id: "stale", name: "Stale model" }] }),
|
|
131
|
+
);
|
|
132
|
+
expect(screen.queryByText("Stale model — stale")).not.toBeInTheDocument();
|
|
133
|
+
|
|
134
|
+
rerender(<Discovery configuration="saved-again" />);
|
|
135
|
+
expect(search()).toBeEnabled();
|
|
136
|
+
expect(listProviderModels).toHaveBeenCalledOnce();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("discards a loaded catalog when settings change without replacing the input", async () => {
|
|
140
|
+
vi.mocked(listProviderModels).mockResolvedValue({
|
|
141
|
+
models: [{ id: "old-model", name: "Old model" }],
|
|
142
|
+
});
|
|
143
|
+
const { rerender } = render(<Discovery />);
|
|
144
|
+
fireEvent.click(search());
|
|
145
|
+
await screen.findByRole("button", { name: "Old model — old-model" });
|
|
146
|
+
const input = screen.getByLabelText("Model");
|
|
147
|
+
act(() => input.focus());
|
|
148
|
+
|
|
149
|
+
rerender(<Discovery configuration="changed" />);
|
|
150
|
+
expect(screen.getByLabelText("Model")).toBe(input);
|
|
151
|
+
expect(input).toHaveFocus();
|
|
152
|
+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
153
|
+
expect(listProviderModels).toHaveBeenCalledOnce();
|
|
154
|
+
|
|
155
|
+
const pending =
|
|
156
|
+
Promise.withResolvers<Awaited<ReturnType<typeof listProviderModels>>>();
|
|
157
|
+
vi.mocked(listProviderModels).mockReturnValueOnce(pending.promise);
|
|
158
|
+
fireEvent.click(search());
|
|
159
|
+
await waitFor(() => expect(listProviderModels).toHaveBeenCalledTimes(2));
|
|
160
|
+
expect(screen.queryByText("Old model — old-model")).not.toBeInTheDocument();
|
|
161
|
+
|
|
162
|
+
await act(async () =>
|
|
163
|
+
pending.resolve({ models: [{ id: "new-model", name: "New model" }] }),
|
|
164
|
+
);
|
|
165
|
+
await screen.findByRole("button", { name: "New model — new-model" });
|
|
166
|
+
expect(screen.queryByText("Old model — old-model")).not.toBeInTheDocument();
|
|
167
|
+
expect(listProviderModels).toHaveBeenCalledTimes(2);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("requires a new search when the provider changes", async () => {
|
|
171
|
+
vi.mocked(listProviderModels).mockResolvedValue({ models: [] });
|
|
172
|
+
const { rerender } = render(<Discovery />);
|
|
173
|
+
fireEvent.click(search());
|
|
174
|
+
await screen.findByText("No models found");
|
|
175
|
+
|
|
176
|
+
rerender(<Discovery provider="another-provider" />);
|
|
177
|
+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
178
|
+
expect(listProviderModels).toHaveBeenCalledOnce();
|
|
179
|
+
fireEvent.click(search());
|
|
180
|
+
await screen.findByText("No models found");
|
|
181
|
+
expect(listProviderModels).toHaveBeenLastCalledWith("another-provider");
|
|
182
|
+
});
|
|
183
|
+
});
|