|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + */ |
| 5 | +// #3852: a watched slot arg re-ships the occurrence's record on every server |
| 6 | +// commit, and each re-ship re-runs the fill's per-key async-arg memo — a new |
| 7 | +// flight superseding the one still in the air, by design. ABANDONED_FLIGHTS |
| 8 | +// does not judge that memo; a user memo churning flights inside the fill |
| 9 | +// still warns. |
| 10 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 11 | +import { createMemo, createRoot, flush, Loading, OBSERVE } from "solid-js"; |
| 12 | +import type { DiagnosticEvent } from "solid-js"; |
| 13 | +import { attribution } from "solid-js/attribution"; |
| 14 | +import { dynamic } from "../src/index.js"; |
| 15 | +import { installServerComponents, createFrameHost } from "../frames/src/client.js"; |
| 16 | +import { createJSONDataTable, createJSONSerializer } from "../serialization/src/serializer.js"; |
| 17 | +import { createServerReference } from "../server-functions/src/client.js"; |
| 18 | +import { createChunk } from "../server-functions/src/shared.js"; |
| 19 | + |
| 20 | +const settle = () => new Promise(r => setTimeout(r)); |
| 21 | +const getReply = createServerReference("reply/get"); |
| 22 | + |
| 23 | +function makeHost() { |
| 24 | + const table = createJSONDataTable(); |
| 25 | + return createFrameHost({ |
| 26 | + applyData: (c: any) => table.apply(c), |
| 27 | + resolve: (ref: any) => table.resolve(ref) |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +const disposers: Array<() => void> = []; |
| 32 | +beforeEach(() => installServerComponents(makeHost())); |
| 33 | +afterEach(() => { |
| 34 | + for (const d of disposers.splice(0)) d(); |
| 35 | + attribution.disable(); |
| 36 | + flush(); |
| 37 | + vi.unstubAllGlobals(); |
| 38 | + vi.restoreAllMocks(); |
| 39 | +}); |
| 40 | + |
| 41 | +function capture() { |
| 42 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 43 | + const events: DiagnosticEvent[] = []; |
| 44 | + disposers.push( |
| 45 | + OBSERVE!.diagnostics.subscribe(e => { |
| 46 | + if (e.code === "ABANDONED_FLIGHTS") events.push(e); |
| 47 | + }) |
| 48 | + ); |
| 49 | + attribution.enable({ log: false, hotRuns: false, hotTime: false, waterfalls: false }); |
| 50 | + return events; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * The chat reply's status slot: `progress` a scalar the server re-ships on |
| 55 | + * every commit, `stats` a promise still pending until generation ends. |
| 56 | + */ |
| 57 | +async function streamReply(fill: (p: any) => any) { |
| 58 | + let resolveStats!: (v: string) => void; |
| 59 | + const statsPromise = new Promise<string>(r => (resolveStats = r)); |
| 60 | + const dataRecords: any[] = []; |
| 61 | + const ser = createJSONSerializer({ onData: (r: any) => dataRecords.push(r) }); |
| 62 | + ser.write("arg:status#0:stats", statsPromise); |
| 63 | + const initialData = dataRecords.splice(0); |
| 64 | + |
| 65 | + let controller!: ReadableStreamDefaultController; |
| 66 | + const body = new ReadableStream({ start: c => (controller = c) }); |
| 67 | + const send = (chunk: any) => controller.enqueue(createChunk(JSON.stringify(chunk))); |
| 68 | + const record = (progress: string) => ({ |
| 69 | + type: "slot", |
| 70 | + id: "srv", |
| 71 | + version: 1, |
| 72 | + key: "status#0", |
| 73 | + args: { progress, stats: { $ref: "arg:status#0:stats" } } |
| 74 | + }); |
| 75 | + vi.stubGlobal("fetch", async () => { |
| 76 | + send({ type: "start", id: "srv", version: 1 }); |
| 77 | + send(record("0 tokens")); |
| 78 | + for (const r of initialData) send({ type: "data", id: "srv", version: 1, ...r }); |
| 79 | + send({ |
| 80 | + type: "html", |
| 81 | + id: "srv", |
| 82 | + version: 1, |
| 83 | + html: "<article><!--slot:status#0:start--><!--slot:status#0:end--></article>" |
| 84 | + }); |
| 85 | + return new Response(body, { headers: { "X-Frame-Stream": "srv" } }); |
| 86 | + }); |
| 87 | + |
| 88 | + const Reply = dynamic(() => getReply("hi") as any); |
| 89 | + let div!: HTMLDivElement; |
| 90 | + disposers.push( |
| 91 | + createRoot(d => { |
| 92 | + <div ref={div}> |
| 93 | + <Loading fallback={<span>typing</span>}> |
| 94 | + <Reply status={fill} /> |
| 95 | + </Loading> |
| 96 | + </div>; |
| 97 | + document.body.appendChild(div); |
| 98 | + return () => (d(), div.remove()); |
| 99 | + }) |
| 100 | + ); |
| 101 | + const tick = async () => { |
| 102 | + flush(); |
| 103 | + await settle(); |
| 104 | + flush(); |
| 105 | + await settle(); |
| 106 | + }; |
| 107 | + await tick(); |
| 108 | + |
| 109 | + for (const n of [1, 2, 3, 4, 5]) { |
| 110 | + send(record(`${n * 10} tokens`)); |
| 111 | + await tick(); |
| 112 | + } |
| 113 | + expect(div.querySelector(".progress")!.textContent).toBe("50 tokens"); |
| 114 | + |
| 115 | + resolveStats("done"); |
| 116 | + await settle(); |
| 117 | + for (const r of dataRecords.splice(0)) send({ type: "data", id: "srv", version: 1, ...r }); |
| 118 | + send({ type: "complete", id: "srv", version: 1 }); |
| 119 | + controller.close(); |
| 120 | + await tick(); |
| 121 | + expect(div.querySelector(".stats")!.textContent).toBe("done"); |
| 122 | +} |
| 123 | + |
| 124 | +describe("ABANDONED_FLIGHTS and re-shipped slot args (#3852)", () => { |
| 125 | + test("a pending arg re-read on every re-ship does not warn", async () => { |
| 126 | + const events = capture(); |
| 127 | + await streamReply(p => ( |
| 128 | + <p> |
| 129 | + <span class="progress">{p.progress}</span> |
| 130 | + <Loading fallback="…"> |
| 131 | + <span class="stats">{p.stats}</span> |
| 132 | + </Loading> |
| 133 | + </p> |
| 134 | + )); |
| 135 | + expect(events).toEqual([]); |
| 136 | + }); |
| 137 | + |
| 138 | + test("a user memo minting a fresh flight per re-ship still warns", async () => { |
| 139 | + const events = capture(); |
| 140 | + await streamReply(p => { |
| 141 | + const ticker = createMemo( |
| 142 | + () => { |
| 143 | + const line = p.progress; |
| 144 | + return new Promise<string>(r => setTimeout(() => r(line), 1000)); |
| 145 | + }, |
| 146 | + { name: "ticker" } |
| 147 | + ); |
| 148 | + return ( |
| 149 | + <p> |
| 150 | + <span class="progress">{p.progress}</span> |
| 151 | + <Loading fallback="…"> |
| 152 | + <span class="ticker">{ticker()}</span> |
| 153 | + </Loading> |
| 154 | + <Loading fallback="…"> |
| 155 | + <span class="stats">{p.stats}</span> |
| 156 | + </Loading> |
| 157 | + </p> |
| 158 | + ); |
| 159 | + }); |
| 160 | + expect(events.map(e => e.nodeName)).toEqual(["ticker"]); |
| 161 | + }); |
| 162 | +}); |
0 commit comments