@marimo-team/islands 0.25.3-dev40 → 0.25.3-dev42
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-C7__jAZL.js → ErrorBoundary-Bb_hjI-F.js} +1 -1
- package/dist/{chat-ui-DGpBSln7.js → chat-ui-DlUp75HS.js} +4 -4
- package/dist/{code-visibility-DO4MlViW.js → code-visibility-BseAT0Di.js} +3 -3
- package/dist/{common-Ci7TC3e0.js → common--apjoMvk.js} +5 -5
- package/dist/{data-grid-overlay-editor-DpH_AKdC.js → data-grid-overlay-editor-C2eZGIzb.js} +1 -1
- package/dist/{error-entries-D576fzV9.js → error-entries-D3ouOLDe.js} +1 -1
- package/dist/{feedback-button-BV5AJ3nl.js → feedback-button-Cy7x7fiM.js} +3 -3
- package/dist/{formats-DIVMVjeV.js → formats-C24TvExI.js} +1 -1
- package/dist/{glide-data-editor-CRK-O0KE.js → glide-data-editor-CYonCTY5.js} +3 -3
- package/dist/{globals-H_ILLmr7.js → globals-W7yeDcvx.js} +1 -1
- package/dist/{html-to-image-D3m5Ob5H.js → html-to-image-VZvb968z.js} +2 -2
- package/dist/main.js +13 -13
- package/dist/{number-overlay-editor-BQ2UqdED.js → number-overlay-editor-_5SNWHjp.js} +1 -1
- package/dist/{process-output-C6qc7hPS.js → process-output-CCPgTShO.js} +2 -2
- package/dist/{reveal-component-Cs-0GwxY.js → reveal-component-DV1u0Sl1.js} +4 -4
- package/dist/{toDate-CSY-j1Fi.js → toDate-C0jaysq2.js} +93 -94
- package/dist/{vega-component-Df45z17m.js → vega-component-CF9IzE2r.js} +2 -2
- package/package.json +2 -4
- package/src/__tests__/host-imports.test.ts +1 -0
- package/src/core/attachments/state.ts +2 -2
- package/src/core/host/AGENTS.md +6 -2
- package/src/core/host/__tests__/attachments.test.ts +9 -10
- package/src/core/host/__tests__/document.test.ts +18 -16
- package/src/core/host/__tests__/events.test.ts +5 -2
- package/src/core/host/__tests__/fixtures.ts +6 -4
- package/src/core/host/events.ts +18 -2
- package/src/core/host/notebook-client.ts +46 -13
- package/src/core/host/resources/attachments.ts +3 -3
- package/src/core/host/resources/document.ts +3 -3
- package/src/core/websocket/__tests__/useNotebookSession.hook.test.tsx +1 -1
- package/src/core/host/__tests__/protocol.test.ts +0 -229
- package/src/core/host/__tests__/stream.test.ts +0 -209
- package/src/core/host/protocol.ts +0 -1948
- package/src/core/host/resources/resource.ts +0 -47
- package/src/core/host/stream.ts +0 -211
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
3
|
import { describe, expect, it } from "vitest";
|
|
4
|
-
import type
|
|
4
|
+
import type * as Protocol from "@marimo-team/client/protocol";
|
|
5
5
|
import { document } from "../resources/document";
|
|
6
|
-
import {
|
|
6
|
+
import { createReducer } from "@marimo-team/client/reducer";
|
|
7
7
|
import { fixtureEvents } from "./fixtures";
|
|
8
8
|
|
|
9
|
-
const
|
|
9
|
+
const reducer = createReducer({ document });
|
|
10
10
|
const snapshot = fixtureEvents("notebook-events.sse");
|
|
11
11
|
const edits = fixtureEvents("edits.sse");
|
|
12
12
|
|
|
13
|
-
function replay(
|
|
14
|
-
|
|
13
|
+
function replay(
|
|
14
|
+
events: readonly Protocol.EventOutput[],
|
|
15
|
+
from = reducer.initial,
|
|
16
|
+
) {
|
|
17
|
+
return events.reduce((state, event) => reducer.reduce(state, event), from);
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
const confirmed = () => replay(snapshot);
|
|
@@ -33,12 +36,12 @@ describe("document resource", () => {
|
|
|
33
36
|
confirmed(),
|
|
34
37
|
);
|
|
35
38
|
expect(current(pending)).toEqual(first);
|
|
36
|
-
const changed =
|
|
39
|
+
const changed = reducer.reduce(pending, edit);
|
|
37
40
|
expect(current(changed)?.revision).toEqual(edit.data.document);
|
|
38
41
|
expect([...current(changed)!.cells.keys()].toSorted()).toEqual(
|
|
39
42
|
edit.data.document.order.toSorted(),
|
|
40
43
|
);
|
|
41
|
-
const saved = current(
|
|
44
|
+
const saved = current(reducer.reduce(changed, save));
|
|
42
45
|
expect(saved?.revision).toEqual(save.data.document);
|
|
43
46
|
expect(saved?.cells.get("c3")?.code).toBe("x = 22");
|
|
44
47
|
expect(saved?.cells.has("k7f2")).toBe(false);
|
|
@@ -55,7 +58,7 @@ describe("document resource", () => {
|
|
|
55
58
|
order: revision.order.toReversed(),
|
|
56
59
|
version: revision.version + 1,
|
|
57
60
|
};
|
|
58
|
-
const changed =
|
|
61
|
+
const changed = reducer.reduce(state, {
|
|
59
62
|
event: "document",
|
|
60
63
|
data: { document: reordered },
|
|
61
64
|
id: "200",
|
|
@@ -66,17 +69,17 @@ describe("document resource", () => {
|
|
|
66
69
|
});
|
|
67
70
|
|
|
68
71
|
const empty = { ...revision, order: [] };
|
|
69
|
-
const opening =
|
|
72
|
+
const opening = reducer.reduce(reducer.initial, {
|
|
70
73
|
event: "document",
|
|
71
74
|
data: { document: empty },
|
|
72
75
|
});
|
|
73
76
|
expect(current(opening)).toBeNull();
|
|
74
77
|
expect(
|
|
75
|
-
current(
|
|
78
|
+
current(reducer.reduce(opening, { event: "ready", data: {}, id: "0" })),
|
|
76
79
|
).toEqual({ revision: empty, cells: new Map() });
|
|
77
80
|
expect(
|
|
78
81
|
current(
|
|
79
|
-
|
|
82
|
+
reducer.reduce(reducer.initial, { event: "ready", data: {}, id: "0" }),
|
|
80
83
|
),
|
|
81
84
|
).toBeNull();
|
|
82
85
|
});
|
|
@@ -89,7 +92,6 @@ describe("document resource", () => {
|
|
|
89
92
|
}
|
|
90
93
|
const partial = replay([cell, removed], state);
|
|
91
94
|
expect(current(partial)).toBe(current(state));
|
|
92
|
-
expect(stream.resume(partial)).toBe(partial);
|
|
93
95
|
const completed = replay(
|
|
94
96
|
[published, { event: "ready", data: {}, id: "113" }],
|
|
95
97
|
partial,
|
|
@@ -98,7 +100,7 @@ describe("document resource", () => {
|
|
|
98
100
|
});
|
|
99
101
|
|
|
100
102
|
it("removes a notebook without waiting for individual cell removals", () => {
|
|
101
|
-
const removed =
|
|
103
|
+
const removed = reducer.reduce(confirmed(), {
|
|
102
104
|
event: "notebook.removed",
|
|
103
105
|
data: { id: "n1", time: "2026-10-01T09:30:00Z" },
|
|
104
106
|
id: "200",
|
|
@@ -110,15 +112,15 @@ describe("document resource", () => {
|
|
|
110
112
|
const state = confirmed();
|
|
111
113
|
const revision = current(state)!.revision;
|
|
112
114
|
expect(() =>
|
|
113
|
-
|
|
115
|
+
reducer.reduce(state, {
|
|
114
116
|
event: "document",
|
|
115
117
|
data: { document: { ...revision, order: ["missing"] } },
|
|
116
118
|
id: "200",
|
|
117
119
|
}),
|
|
118
120
|
).toThrow("Document records do not match");
|
|
119
|
-
const partial =
|
|
121
|
+
const partial = reducer.reduce(state, edits[0]);
|
|
120
122
|
expect(() =>
|
|
121
|
-
|
|
123
|
+
reducer.reduce(partial, { event: "ready", data: {}, id: "200" }),
|
|
122
124
|
).toThrow("incomplete document update");
|
|
123
125
|
});
|
|
124
126
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
-
import { parseNotebookEvent } from "../events";
|
|
5
|
+
import { type NotebookEvent, parseNotebookEvent } from "../events";
|
|
6
6
|
|
|
7
7
|
describe("notebook messages", () => {
|
|
8
8
|
it("normalizes session and host envelopes for native marimo messages", () => {
|
|
@@ -10,6 +10,9 @@ describe("notebook messages", () => {
|
|
|
10
10
|
op: "variables",
|
|
11
11
|
variables: [{ name: "x", value: Number.NaN }],
|
|
12
12
|
};
|
|
13
|
+
expectTypeOf<
|
|
14
|
+
Extract<NotebookEvent, { event: "marimo" }>["data"]["marimo"]
|
|
15
|
+
>().toBeUnknown();
|
|
13
16
|
const host = { event: "marimo", data: { marimo: notification } };
|
|
14
17
|
expect(parseNotebookEvent({ data: notification })).toEqual(host);
|
|
15
18
|
expect(parseNotebookEvent({ ...host, id: "42" })).toEqual({
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import NodeFs from "node:fs";
|
|
4
4
|
import NodeUrl from "node:url";
|
|
5
|
-
import
|
|
5
|
+
import * as Protocol from "@marimo-team/client/protocol";
|
|
6
6
|
|
|
7
7
|
/** Read the protocol's portable examples as transport messages. */
|
|
8
8
|
export function fixtureMessages(name: string) {
|
|
@@ -33,7 +33,9 @@ export function fixtureMessages(name: string) {
|
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
/** Read the protocol's portable examples as decoded
|
|
37
|
-
export function fixtureEvents(name: string):
|
|
38
|
-
return fixtureMessages(name).map(
|
|
36
|
+
/** Read the protocol's portable examples as decoded protocol events. */
|
|
37
|
+
export function fixtureEvents(name: string): Protocol.EventOutput[] {
|
|
38
|
+
return fixtureMessages(name).map(({ id, ...message }) =>
|
|
39
|
+
Protocol.Event.parse(id === undefined ? message : { ...message, id }),
|
|
40
|
+
);
|
|
39
41
|
}
|
package/src/core/host/events.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import * as Protocol from "
|
|
4
|
+
import * as Protocol from "@marimo-team/client/protocol";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Notebook stream events, including native marimo notifications.
|
|
@@ -29,16 +29,29 @@ const knownNames = new Set<string>(
|
|
|
29
29
|
Protocol.Event.options.flatMap((schema) => schema.shape.event.options),
|
|
30
30
|
);
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* A native marimo message carried by a host stream.
|
|
34
|
+
*
|
|
35
|
+
* The protocol leaves extension events to their producer. The payload is
|
|
36
|
+
* passed through for the session handler to interpret.
|
|
37
|
+
*/
|
|
38
|
+
const MarimoEvent = z.object({
|
|
39
|
+
event: z.literal("marimo"),
|
|
40
|
+
id: z.string().min(1).optional(),
|
|
41
|
+
data: z.object({ marimo: z.unknown() }),
|
|
42
|
+
});
|
|
43
|
+
|
|
32
44
|
/** A decoded event, or the name and cursor of an unrecognized event. */
|
|
33
45
|
export type NotebookEvent =
|
|
34
46
|
| Protocol.EventOutput
|
|
47
|
+
| z.output<typeof MarimoEvent>
|
|
35
48
|
| {
|
|
36
49
|
readonly event: "unknown";
|
|
37
50
|
readonly name: string;
|
|
38
51
|
readonly id?: string;
|
|
39
52
|
};
|
|
40
53
|
|
|
41
|
-
const sessionEnvelope = z.object({ data:
|
|
54
|
+
const sessionEnvelope = z.object({ data: z.unknown() });
|
|
42
55
|
|
|
43
56
|
/**
|
|
44
57
|
* Decode a host event or a legacy `{ data: notification }` session message.
|
|
@@ -54,6 +67,9 @@ export function parseNotebookEvent(value: unknown): NotebookEvent {
|
|
|
54
67
|
};
|
|
55
68
|
}
|
|
56
69
|
const parsed = envelope.parse(value);
|
|
70
|
+
if (parsed.event === "marimo") {
|
|
71
|
+
return MarimoEvent.parse(parsed);
|
|
72
|
+
}
|
|
57
73
|
if (knownNames.has(parsed.event)) {
|
|
58
74
|
return Protocol.Event.parse(parsed);
|
|
59
75
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
3
|
import { Logger } from "@/utils/Logger";
|
|
4
|
+
import type { Attachment } from "@marimo-team/client/protocol";
|
|
5
|
+
import { createReducer } from "@marimo-team/client/reducer";
|
|
4
6
|
import { type Atom, atom, type createStore } from "jotai/vanilla";
|
|
5
7
|
import {
|
|
6
8
|
type ConnectionStatus,
|
|
@@ -10,11 +12,16 @@ import {
|
|
|
10
12
|
type NotebookTransport,
|
|
11
13
|
TransportReadyState,
|
|
12
14
|
} from "./connection";
|
|
13
|
-
import { parseNotebookEvent } from "./events";
|
|
14
|
-
import type { Attachment } from "./protocol";
|
|
15
|
+
import { type NotebookEvent, parseNotebookEvent } from "./events";
|
|
15
16
|
import { attachments } from "./resources/attachments";
|
|
16
17
|
import { type Document, document } from "./resources/document";
|
|
17
|
-
|
|
18
|
+
|
|
19
|
+
/** Events that may arrive without a cursor after `ready`. */
|
|
20
|
+
const UNCURSORED = new Set<NotebookEvent["event"]>([
|
|
21
|
+
"reset",
|
|
22
|
+
"marimo",
|
|
23
|
+
"unknown",
|
|
24
|
+
]);
|
|
18
25
|
|
|
19
26
|
/** A connection to a notebook on a marimo host. */
|
|
20
27
|
export interface NotebookClient {
|
|
@@ -64,8 +71,8 @@ export function createNotebookClient(store: ReturnType<typeof createStore>) {
|
|
|
64
71
|
state: ConnectionState.NOT_STARTED,
|
|
65
72
|
});
|
|
66
73
|
// One atom holds the whole stream, which resource atoms derive from.
|
|
67
|
-
const
|
|
68
|
-
const streamAtom = atom(
|
|
74
|
+
const reducer = createReducer({ document, attachments });
|
|
75
|
+
const streamAtom = atom(reducer.initial);
|
|
69
76
|
// A resumed stream is live, but presence waits for this connection's ready.
|
|
70
77
|
const readyAtom = atom(false);
|
|
71
78
|
const documentAtom = atom(
|
|
@@ -103,6 +110,8 @@ export function createNotebookClient(store: ReturnType<typeof createStore>) {
|
|
|
103
110
|
let terminal = false;
|
|
104
111
|
let checkingHealth = false;
|
|
105
112
|
let streamKey: string | undefined;
|
|
113
|
+
// The id of the last live event. Null before `ready` and after `reset`.
|
|
114
|
+
let cursor: string | null = null;
|
|
106
115
|
const transport = createTransport(() => {
|
|
107
116
|
if (messageFormat === "session") {
|
|
108
117
|
return url();
|
|
@@ -118,15 +127,23 @@ export function createNotebookClient(store: ReturnType<typeof createStore>) {
|
|
|
118
127
|
identity.searchParams.delete("access_token");
|
|
119
128
|
identity.searchParams.sort();
|
|
120
129
|
const key = identity.toString();
|
|
130
|
+
const current = store.get(streamAtom);
|
|
131
|
+
// A live stream keeps its state and resumes from the cursor. An
|
|
132
|
+
// incomplete snapshot starts over.
|
|
121
133
|
const state =
|
|
122
|
-
key
|
|
123
|
-
?
|
|
124
|
-
:
|
|
134
|
+
key !== streamKey
|
|
135
|
+
? reducer.initial
|
|
136
|
+
: current.phase === "snapshot"
|
|
137
|
+
? { ...reducer.initial, notebookId: current.notebookId }
|
|
138
|
+
: current;
|
|
139
|
+
if (state.phase === "snapshot") {
|
|
140
|
+
cursor = null;
|
|
141
|
+
}
|
|
125
142
|
streamKey = key;
|
|
126
143
|
store.set(readyAtom, false);
|
|
127
144
|
store.set(streamAtom, state);
|
|
128
|
-
if (
|
|
129
|
-
next.searchParams.set("cursor",
|
|
145
|
+
if (cursor !== null) {
|
|
146
|
+
next.searchParams.set("cursor", cursor);
|
|
130
147
|
}
|
|
131
148
|
return next.toString();
|
|
132
149
|
});
|
|
@@ -188,7 +205,22 @@ export function createNotebookClient(store: ReturnType<typeof createStore>) {
|
|
|
188
205
|
let event;
|
|
189
206
|
try {
|
|
190
207
|
event = parseNotebookEvent(decodeJson(message.data));
|
|
191
|
-
|
|
208
|
+
const state = store.get(streamAtom);
|
|
209
|
+
if (
|
|
210
|
+
state.phase === "live" &&
|
|
211
|
+
event.id === undefined &&
|
|
212
|
+
!UNCURSORED.has(event.event)
|
|
213
|
+
) {
|
|
214
|
+
throw new Error("Live notebook event is missing its cursor");
|
|
215
|
+
}
|
|
216
|
+
if (event.event !== "marimo" && event.event !== "unknown") {
|
|
217
|
+
store.set(streamAtom, reducer.reduce(state, event));
|
|
218
|
+
}
|
|
219
|
+
if (event.event === "reset") {
|
|
220
|
+
cursor = null;
|
|
221
|
+
} else if (event.event === "ready" || state.phase === "live") {
|
|
222
|
+
cursor = event.id ?? cursor;
|
|
223
|
+
}
|
|
192
224
|
if (event.event === "ready" || event.event === "reset") {
|
|
193
225
|
store.set(readyAtom, event.event === "ready");
|
|
194
226
|
}
|
|
@@ -198,7 +230,8 @@ export function createNotebookClient(store: ReturnType<typeof createStore>) {
|
|
|
198
230
|
onMessageError(error);
|
|
199
231
|
return;
|
|
200
232
|
}
|
|
201
|
-
store.set(streamAtom,
|
|
233
|
+
store.set(streamAtom, reducer.initial);
|
|
234
|
+
cursor = null;
|
|
202
235
|
terminal = true;
|
|
203
236
|
setStatus({
|
|
204
237
|
...disconnected,
|
|
@@ -256,7 +289,7 @@ export function createNotebookClient(store: ReturnType<typeof createStore>) {
|
|
|
256
289
|
transport.removeEventListener("message", onMessage);
|
|
257
290
|
transport.close();
|
|
258
291
|
reconnect = undefined;
|
|
259
|
-
store.set(streamAtom,
|
|
292
|
+
store.set(streamAtom, reducer.initial);
|
|
260
293
|
setStatus({ state: ConnectionState.NOT_STARTED });
|
|
261
294
|
},
|
|
262
295
|
{ once: true },
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
|
-
import type { Attachment } from "
|
|
4
|
-
import type { Resource } from "
|
|
3
|
+
import type { Attachment } from "@marimo-team/client/protocol";
|
|
4
|
+
import type { Resource } from "@marimo-team/client/reducer";
|
|
5
5
|
|
|
6
6
|
/** Attachment records retained with the stream's resume cursor. */
|
|
7
|
-
export const attachments: Resource<readonly Attachment[]> = {
|
|
7
|
+
export const attachments: Resource<readonly Attachment[], "attachment"> = {
|
|
8
8
|
initial: [],
|
|
9
9
|
on: {
|
|
10
10
|
attachment: (state, { data: { attachment } }) =>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
|
-
import type * as Protocol from "
|
|
4
|
-
import type { Resource } from "
|
|
3
|
+
import type * as Protocol from "@marimo-team/client/protocol";
|
|
4
|
+
import type { Resource } from "@marimo-team/client/reducer";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* The notebook's content at one revision.
|
|
@@ -58,7 +58,7 @@ function publish(
|
|
|
58
58
|
return { current: { revision, cells }, pending: null };
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
export const document: Resource<DocumentState> = {
|
|
61
|
+
export const document: Resource<DocumentState, "cell" | "document"> = {
|
|
62
62
|
initial: { current: null, pending: null },
|
|
63
63
|
on: {
|
|
64
64
|
cell: (state, { data }) => {
|
|
@@ -45,7 +45,7 @@ import { notebookAtom } from "@/core/cells/cells";
|
|
|
45
45
|
import { AppConfigSchema } from "@/core/config/config-schema";
|
|
46
46
|
import { ConnectionNotice } from "@/components/editor/alerts/connection-notice";
|
|
47
47
|
import { kernelStartupErrorAtom } from "@/core/errors/state";
|
|
48
|
-
import type { RunEvent } from "
|
|
48
|
+
import type { RunEvent } from "@marimo-team/client/protocol";
|
|
49
49
|
import type { NotificationPayload } from "@/core/kernel/messages";
|
|
50
50
|
import { useRuntimeManager } from "@/core/runtime/config";
|
|
51
51
|
import { initialRunCompletedAtom } from "../../kernel/state";
|
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
-
|
|
3
|
-
import NodeFs from "node:fs";
|
|
4
|
-
import NodeUrl from "node:url";
|
|
5
|
-
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
6
|
-
import * as Protocol from "../protocol";
|
|
7
|
-
|
|
8
|
-
const fixtures = new NodeUrl.URL(
|
|
9
|
-
"../../../../../protocols/host/fixtures/",
|
|
10
|
-
import.meta.url,
|
|
11
|
-
);
|
|
12
|
-
|
|
13
|
-
function readFixture(name: string): unknown {
|
|
14
|
-
return JSON.parse(
|
|
15
|
-
NodeFs.readFileSync(new NodeUrl.URL(name, fixtures), "utf8"),
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const records = {
|
|
20
|
-
"record.json": Protocol.HostRecord,
|
|
21
|
-
"cell.json": Protocol.Cell,
|
|
22
|
-
"cell-withheld.json": Protocol.Cell,
|
|
23
|
-
"analysis.json": Protocol.Analysis,
|
|
24
|
-
"diagnostic.json": Protocol.Error,
|
|
25
|
-
"edit.json": Protocol.EditRequest,
|
|
26
|
-
"edit-conflict-request.json": Protocol.EditRequest,
|
|
27
|
-
"edit-response.json": Protocol.EditResponse,
|
|
28
|
-
"execution-restored.json": Protocol.Execution,
|
|
29
|
-
"run-cells-request.json": Protocol.RunRequest,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
describe("host protocol fixtures", () => {
|
|
33
|
-
it.each(Object.entries(records))("parses %s unchanged", (name, schema) => {
|
|
34
|
-
const input = readFixture(name);
|
|
35
|
-
const original = structuredClone(input);
|
|
36
|
-
expect(schema.parse(input)).toEqual(original);
|
|
37
|
-
expect(input).toEqual(original);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it.each(NodeFs.readdirSync(new NodeUrl.URL("problems/", fixtures)))(
|
|
41
|
-
"parses problem %s",
|
|
42
|
-
(name) => {
|
|
43
|
-
const input = readFixture(`problems/${name}`);
|
|
44
|
-
expect(Protocol.Problem.parse(input)).toEqual(input);
|
|
45
|
-
},
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
it.each(NodeFs.readdirSync(fixtures).filter((name) => name.endsWith(".sse")))(
|
|
49
|
-
"parses events in %s unchanged",
|
|
50
|
-
(name) => {
|
|
51
|
-
const stream = NodeFs.readFileSync(
|
|
52
|
-
new NodeUrl.URL(name, fixtures),
|
|
53
|
-
"utf8",
|
|
54
|
-
);
|
|
55
|
-
let count = 0;
|
|
56
|
-
for (const frame of stream.split(/\r?\n\r?\n/)) {
|
|
57
|
-
const event = /^event: (.+)$/m.exec(frame)?.[1];
|
|
58
|
-
if (!event && !/^data:/m.test(frame)) {
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
const data = frame
|
|
62
|
-
.split(/\r?\n/)
|
|
63
|
-
.filter((line) => line.startsWith("data:"))
|
|
64
|
-
.map((line) => line.slice(5).trimStart())
|
|
65
|
-
.join("\n");
|
|
66
|
-
const id = /^id: (.+)$/m.exec(frame)?.[1];
|
|
67
|
-
const payload: unknown = JSON.parse(data);
|
|
68
|
-
const input = { event, data: payload, ...(id ? { id } : {}) };
|
|
69
|
-
const original = structuredClone(input);
|
|
70
|
-
expect(Protocol.Event.parse(input)).toEqual(original);
|
|
71
|
-
expect(input).toEqual(original);
|
|
72
|
-
count += 1;
|
|
73
|
-
}
|
|
74
|
-
expect(count).toBeGreaterThan(0);
|
|
75
|
-
},
|
|
76
|
-
);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe("host protocol bindings", () => {
|
|
80
|
-
it("preserves native marimo payloads without interpreting them", () => {
|
|
81
|
-
expectTypeOf<Protocol.MarimoEvent["data"]["marimo"]>().toBeUnknown();
|
|
82
|
-
const input = {
|
|
83
|
-
event: "marimo",
|
|
84
|
-
id: "42",
|
|
85
|
-
data: {
|
|
86
|
-
marimo: {
|
|
87
|
-
future_payload: { value: Number.NaN },
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
expect(Protocol.Event.parse(input)).toEqual(input);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("accepts additive fields and preserves arbitrary notebook settings", () => {
|
|
95
|
-
const cell = Protocol.Cell.parse(readFixture("cell.json"));
|
|
96
|
-
const input = {
|
|
97
|
-
...cell,
|
|
98
|
-
future: { enabled: true },
|
|
99
|
-
config: { ...cell.config, future: 42 },
|
|
100
|
-
};
|
|
101
|
-
const original = structuredClone(input);
|
|
102
|
-
expect(Protocol.Cell.parse(input)).toEqual(cell);
|
|
103
|
-
expect(input).toEqual(original);
|
|
104
|
-
|
|
105
|
-
const { document } = Protocol.EditResponse.parse(
|
|
106
|
-
readFixture("edit-response.json"),
|
|
107
|
-
);
|
|
108
|
-
const configured = {
|
|
109
|
-
...document,
|
|
110
|
-
app_config: { extension: { values: [1, null, false] } },
|
|
111
|
-
};
|
|
112
|
-
expect(Protocol.Document.parse(configured)).toEqual(configured);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("requires source in edits while allowing withheld source in records", () => {
|
|
116
|
-
expectTypeOf<Protocol.Cell["code"]>().toEqualTypeOf<string | null>();
|
|
117
|
-
expectTypeOf<Protocol.CellInput["code"]>().toEqualTypeOf<string>();
|
|
118
|
-
const cell = Protocol.Cell.parse(readFixture("cell.json"));
|
|
119
|
-
const { code, ...missing } = cell;
|
|
120
|
-
expect(Protocol.Cell.safeParse(missing).success).toBe(false);
|
|
121
|
-
expect(Protocol.Cell.parse({ ...cell, code: "" }).code).toBe("");
|
|
122
|
-
for (const input of [{ id: cell.id }, { id: cell.id, code: null }]) {
|
|
123
|
-
expect(Protocol.CellInput.safeParse(input).success).toBe(false);
|
|
124
|
-
}
|
|
125
|
-
expect(Protocol.CellInput.parse({ id: cell.id, code }).code).toBe(code);
|
|
126
|
-
expect(Protocol.CellInput.parse({ id: cell.id, code: "" }).code).toBe("");
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it("applies the defaults declared for cell inputs", () => {
|
|
130
|
-
expect(
|
|
131
|
-
Protocol.CellInput.parse({ id: "c3", code: "x = 1", config: {} }),
|
|
132
|
-
).toEqual({
|
|
133
|
-
id: "c3",
|
|
134
|
-
code: "x = 1",
|
|
135
|
-
name: "_",
|
|
136
|
-
config: {
|
|
137
|
-
column: null,
|
|
138
|
-
disabled: false,
|
|
139
|
-
hide_code: false,
|
|
140
|
-
expand_output: false,
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it.each(["run_id", "runtime_id", "caused_by"] as const)(
|
|
146
|
-
"requires the nullable execution field %s",
|
|
147
|
-
(field) => {
|
|
148
|
-
const execution = Protocol.Execution.parse(
|
|
149
|
-
readFixture("execution-restored.json"),
|
|
150
|
-
);
|
|
151
|
-
const missing = Object.fromEntries(
|
|
152
|
-
Object.entries(execution).filter(([key]) => key !== field),
|
|
153
|
-
);
|
|
154
|
-
expect(Protocol.Execution.safeParse(missing).success).toBe(false);
|
|
155
|
-
expect(
|
|
156
|
-
Protocol.Execution.safeParse({ ...execution, [field]: null }).success,
|
|
157
|
-
).toBe(true);
|
|
158
|
-
},
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
it("preserves snapshot metadata omissions and validates live timestamps", () => {
|
|
162
|
-
const cell = Protocol.Cell.parse(readFixture("cell.json"));
|
|
163
|
-
expect(Protocol.CellMessage.parse({ cell })).toEqual({ cell });
|
|
164
|
-
const message = { cell, time: "2026-01-01T12:00:00+05:30" };
|
|
165
|
-
expect(Protocol.CellMessage.parse(message)).toEqual(message);
|
|
166
|
-
for (const time of [
|
|
167
|
-
null,
|
|
168
|
-
"yesterday",
|
|
169
|
-
"2026-02-30T12:00:00Z",
|
|
170
|
-
"2026-01-01T12:00:00",
|
|
171
|
-
]) {
|
|
172
|
-
expect(Protocol.CellMessage.safeParse({ cell, time }).success).toBe(
|
|
173
|
-
false,
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("rejects invalid cell fields without coercing their values", () => {
|
|
179
|
-
const cell = Protocol.Cell.parse(readFixture("cell.json"));
|
|
180
|
-
for (const patch of [
|
|
181
|
-
{ id: "" },
|
|
182
|
-
{ code: 42 },
|
|
183
|
-
{ etag: 42 },
|
|
184
|
-
{ version: "2" },
|
|
185
|
-
{ version: 0 },
|
|
186
|
-
{ version: 1.5 },
|
|
187
|
-
{ config: { ...cell.config, disabled: "false" } },
|
|
188
|
-
]) {
|
|
189
|
-
expect(Protocol.Cell.safeParse({ ...cell, ...patch }).success).toBe(
|
|
190
|
-
false,
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
const result = Protocol.Cell.safeParse({ ...cell, code: 42 });
|
|
194
|
-
expect(result.success).toBe(false);
|
|
195
|
-
if (result.success) {
|
|
196
|
-
throw new Error("Expected an invalid cell");
|
|
197
|
-
}
|
|
198
|
-
expect(result.error.issues).toEqual([
|
|
199
|
-
expect.objectContaining({ path: ["code"], code: "invalid_type" }),
|
|
200
|
-
]);
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
it("requires the fields of the selected change kind", () => {
|
|
204
|
-
const change = { kind: "delete_cell", id: "c3", base_etag: "*" };
|
|
205
|
-
expect(Protocol.Change.parse(change)).toEqual(change);
|
|
206
|
-
expect(
|
|
207
|
-
Protocol.Change.safeParse({ kind: "delete_cell", id: "c3" }).success,
|
|
208
|
-
).toBe(false);
|
|
209
|
-
expect(
|
|
210
|
-
Protocol.Change.safeParse({ ...change, kind: "unknown" }).success,
|
|
211
|
-
).toBe(false);
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it("requires custom payloads while allowing any JSON value", () => {
|
|
215
|
-
for (const data of [
|
|
216
|
-
null,
|
|
217
|
-
false,
|
|
218
|
-
42,
|
|
219
|
-
"text",
|
|
220
|
-
{ nested: [1, null, false] },
|
|
221
|
-
]) {
|
|
222
|
-
const custom = { type: "example.event", data };
|
|
223
|
-
expect(Protocol.Custom.parse(custom)).toEqual(custom);
|
|
224
|
-
}
|
|
225
|
-
expect(Protocol.Custom.safeParse({ type: "example.event" }).success).toBe(
|
|
226
|
-
false,
|
|
227
|
-
);
|
|
228
|
-
});
|
|
229
|
-
});
|