The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createElement } from "react"; |
| 2 | import { expect, it, vi } from "vite-plus/test"; |
| 3 | import JSZip from "jszip"; |
| 4 | import "~/index.css"; |
| 5 | import { act, render } from "../../../../test/render"; |
| 6 | import Spreadsheet from "./Spreadsheet"; |
| 7 | import { createWorkerClient } from "./worker-rpc"; |
| 8 | import type { SheetMethods } from "./sheet-protocol"; |
| 9 | import { maximumTrackSize } from "./sheet-sizing"; |
| 10 | |
| 11 | it.each([false, true])( |
| 12 | "reflows preview tracks through sheet switches, frozen and hidden tracks: %s", |
| 13 | async (frozen) => { |
| 14 | const archive = new JSZip(); |
| 15 | archive.file( |
| 16 | "xl/workbook.xml", |
| 17 | '<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Budget" sheetId="1" r:id="rId1"/><sheet name="Other" sheetId="2" r:id="rId2"/></sheets></workbook>', |
| 18 | ); |
| 19 | archive.file( |
| 20 | "xl/_rels/workbook.xml.rels", |
| 21 | '<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml"/></Relationships>', |
| 22 | ); |
| 23 | for (const sheet of [1, 2]) |
| 24 | archive.file( |
| 25 | `xl/worksheets/sheet${sheet}.xml`, |
| 26 | `<worksheet>${frozen ? '<sheetViews><sheetView workbookViewId="0"><pane xSplit="1" ySplit="1" state="frozen" topLeftCell="B2"/></sheetView></sheetViews><cols><col min="2" max="2" width="0" hidden="1"/></cols>' : ""}<sheetData><row r="1"><c r="A1"><v>42</v></c></row>${frozen ? '<row r="2" ht="0" hidden="1"/>' : ""}</sheetData>${frozen ? '<mergeCells><mergeCell ref="A1:C1"/></mergeCells>' : ""}</worksheet>`, |
| 27 | ); |
| 28 | const namespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"; |
| 29 | archive.file( |
| 30 | "xl/worksheets/_rels/sheet1.xml.rels", |
| 31 | '<Relationships><Relationship Id="drawing" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing1.xml"/></Relationships>', |
| 32 | ); |
| 33 | archive.file( |
| 34 | "xl/drawings/drawing1.xml", |
| 35 | `<xdr:wsDr xmlns:xdr="${namespace}" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><xdr:twoCellAnchor><xdr:from><xdr:col>10</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>10</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from><xdr:to><xdr:col>13</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>17</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:to><xdr:graphicFrame><xdr:nvGraphicFramePr><xdr:cNvPr id="2" name="Sample chart"/><xdr:cNvGraphicFramePr/></xdr:nvGraphicFramePr><xdr:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/></xdr:xfrm><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart"><c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" r:id="chart"/></a:graphicData></a:graphic></xdr:graphicFrame><xdr:clientData/></xdr:twoCellAnchor></xdr:wsDr>`, |
| 36 | ); |
| 37 | archive.file( |
| 38 | "xl/drawings/_rels/drawing1.xml.rels", |
| 39 | '<Relationships><Relationship Id="chart" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="../charts/chart1.xml"/></Relationships>', |
| 40 | ); |
| 41 | archive.file( |
| 42 | "xl/charts/chart1.xml", |
| 43 | '<c:chartSpace xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"><c:chart><c:plotArea><c:barChart><c:barDir val="col"/><c:grouping val="clustered"/><c:ser><c:idx val="0"/><c:order val="0"/><c:cat><c:strLit><c:ptCount val="1"/><c:pt idx="0"><c:v>Sample</c:v></c:pt></c:strLit></c:cat><c:val><c:numLit><c:formatCode>General</c:formatCode><c:ptCount val="1"/><c:pt idx="0"><c:v>42</c:v></c:pt></c:numLit></c:val></c:ser></c:barChart></c:plotArea></c:chart></c:chartSpace>', |
| 44 | ); |
| 45 | const sheetXml = await archive.file("xl/worksheets/sheet1.xml")!.async("string"); |
| 46 | archive.file( |
| 47 | "xl/worksheets/sheet1.xml", |
| 48 | sheetXml.replace( |
| 49 | "</worksheet>", |
| 50 | '<drawing xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="drawing"/></worksheet>', |
| 51 | ), |
| 52 | ); |
| 53 | const bytes = await archive.generateAsync({ type: "arraybuffer" }); |
| 54 | const client = createWorkerClient<SheetMethods>( |
| 55 | new Worker(new URL("./spreadsheet.worker.ts", import.meta.url), { type: "module" }), |
| 56 | ); |
| 57 | try { |
| 58 | const opened = await client.call("open", { |
| 59 | bytes, |
| 60 | view: { hiddenGridlines: [], themeFonts: { major: "Calibri Light", minor: "Calibri" } }, |
| 61 | }); |
| 62 | expect(opened!.layout.chartWidth).toBeGreaterThan(0); |
| 63 | expect(opened!.layout.chartHeight).toBeGreaterThan(0); |
| 64 | if (frozen) { |
| 65 | const viewport = { |
| 66 | sheet: 0, |
| 67 | width: 400, |
| 68 | height: 200, |
| 69 | ratio: 1, |
| 70 | offset: { x: 0, y: 0 }, |
| 71 | wider: undefined, |
| 72 | }; |
| 73 | const clipped = await client.call("frame", { ...viewport, x: 32, y: 10 }); |
| 74 | expect(clipped!.grid!.colOffsets[2]! - clipped!.grid!.colOffsets[1]!).toBeCloseTo(32, 0); |
| 75 | expect(clipped!.grid!.rowOffsets[2]! - clipped!.grid!.rowOffsets[1]!).toBeCloseTo(10, 0); |
| 76 | clipped!.bitmap.close(); |
| 77 | await client.call("resize", { sheet: 0, axis: "column", index: 2, delta: 1 }); |
| 78 | await client.call("resize", { sheet: 0, axis: "row", index: 2, delta: 1 }); |
| 79 | const resized = await client.call("frame", { ...viewport, x: 0, y: 0 }); |
| 80 | expect(resized!.grid!.colOffsets[2]! - resized!.grid!.colOffsets[1]!).toBeCloseTo(65, 0); |
| 81 | expect(resized!.grid!.rowOffsets[2]! - resized!.grid!.rowOffsets[1]!).toBeCloseTo(21, 0); |
| 82 | resized!.bitmap.close(); |
| 83 | } |
| 84 | const other = await client.call("position", { sheet: 1, cell: { row: 0, col: 0 } }); |
| 85 | const widened = await client.call("resize", { |
| 86 | sheet: 0, |
| 87 | axis: "column", |
| 88 | index: 0, |
| 89 | delta: 81, |
| 90 | }); |
| 91 | expect(widened.chartWidth - opened!.layout.chartWidth).toBeCloseTo(frozen ? 1 : 81, 0); |
| 92 | const taller = await client.call("resize", { sheet: 0, axis: "row", index: 0, delta: 40 }); |
| 93 | expect(taller.chartHeight - opened!.layout.chartHeight).toBeCloseTo(frozen ? 1 : 40, 0); |
| 94 | await expect( |
| 95 | client.call("resize", { sheet: 0, axis: "row", index: 0, delta: NaN }), |
| 96 | ).rejects.toThrow("Invalid track resize"); |
| 97 | expect(await client.call("position", { sheet: 1, cell: { row: 0, col: 0 } })).toEqual(other); |
| 98 | await client.call("show", { sheet: 1 }); |
| 99 | await client.call("show", { sheet: 0 }); |
| 100 | const position = await client.call("position", { sheet: 0, cell: { row: 0, col: 0 } }); |
| 101 | if (!frozen) { |
| 102 | expect(position!.next.x - position!.start.x).toBeCloseTo(145, 0); |
| 103 | expect(position!.next.y - position!.start.y).toBeCloseTo(60, 0); |
| 104 | } |
| 105 | expect( |
| 106 | await client.call("cells", { sheet: 0, range: { top: 0, bottom: 0, left: 0, right: 0 } }), |
| 107 | ).toEqual([["42"]]); |
| 108 | const frame = await client.call("frame", { |
| 109 | sheet: 0, |
| 110 | x: 0, |
| 111 | y: 0, |
| 112 | width: 400, |
| 113 | height: 200, |
| 114 | ratio: 1, |
| 115 | offset: { x: 0, y: 0 }, |
| 116 | wider: undefined, |
| 117 | }); |
| 118 | expect(frame?.grid?.colOffsets[1]).toBeCloseTo(145, 0); |
| 119 | expect(frame?.grid?.rowOffsets[1]).toBeCloseTo(60, 0); |
| 120 | if (frozen) { |
| 121 | expect(frame?.frozen.width).toBeCloseTo(145, 0); |
| 122 | expect(frame?.frozen.height).toBeCloseTo(60, 0); |
| 123 | expect(frame?.grid?.colIndices).not.toContain(1); |
| 124 | expect(frame?.grid?.rowIndices).not.toContain(1); |
| 125 | } |
| 126 | frame?.bitmap.close(); |
| 127 | await client.call("resize", { sheet: 0, axis: "column", index: 0, delta: 10_000 }); |
| 128 | await client.call("resize", { sheet: 0, axis: "row", index: 0, delta: -10_000 }); |
| 129 | const limited = await client.call("frame", { |
| 130 | sheet: 0, |
| 131 | x: 0, |
| 132 | y: 0, |
| 133 | width: 2000, |
| 134 | height: 200, |
| 135 | ratio: 1, |
| 136 | offset: { x: 0, y: 0 }, |
| 137 | wider: undefined, |
| 138 | }); |
| 139 | expect(limited!.grid!.colOffsets[1]).toBeCloseTo(maximumTrackSize.column, 0); |
| 140 | expect(limited!.grid!.rowOffsets[1]).toBeCloseTo(16, 0); |
| 141 | limited!.bitmap.close(); |
| 142 | } finally { |
| 143 | client.close(); |
| 144 | } |
| 145 | }, |
| 146 | ); |
| 147 | |
| 148 | it("requests current dimensions after resizing and scrolling away and back before a frame arrives", async () => { |
| 149 | const archive = new JSZip(); |
| 150 | archive.file( |
| 151 | "xl/workbook.xml", |
| 152 | '<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Sample" sheetId="1" r:id="sheet"/></sheets></workbook>', |
| 153 | ); |
| 154 | archive.file( |
| 155 | "xl/_rels/workbook.xml.rels", |
| 156 | '<Relationships><Relationship Id="sheet" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>', |
| 157 | ); |
| 158 | archive.file( |
| 159 | "xl/worksheets/sheet1.xml", |
| 160 | '<worksheet><sheetData><row r="1"><c r="A1"><v>42</v></c></row><row r="1000"><c r="A1000"><v>99</v></c></row></sheetData></worksheet>', |
| 161 | ); |
| 162 | const bytes = await archive.generateAsync({ type: "arraybuffer" }); |
| 163 | const container = document.createElement("div"); |
| 164 | Object.assign(container.style, { display: "flex", width: "500px", height: "300px" }); |
| 165 | const onError = vi.fn(); |
| 166 | await using screen = await render( |
| 167 | createElement(Spreadsheet, { |
| 168 | bytes, |
| 169 | view: { hiddenGridlines: [], themeFonts: { major: "Calibri Light", minor: "Calibri" } }, |
| 170 | onDetails: vi.fn(), |
| 171 | onError, |
| 172 | }), |
| 173 | { container }, |
| 174 | ); |
| 175 | await expect.element(screen.getByRole("grid", { name: "Sample" })).toBeVisible(); |
| 176 | const root = screen.getByRole("grid", { name: "Sample" }).element(); |
| 177 | const canvas = root.querySelector("canvas")!; |
| 178 | await vi.waitFor(() => { |
| 179 | expect(canvas.getContext("2d")!.getImageData(10, 10, 1, 1).data[0]).toBeGreaterThan(0); |
| 180 | expect(canvas.getContext("2d")!.getImageData(10, 10, 1, 1).data[0]).toBeLessThan(255); |
| 181 | }); |
| 182 | const postMessage = Worker.prototype.postMessage; |
| 183 | const pending: { worker: Worker; message: unknown; options: unknown }[] = []; |
| 184 | using _frames = vi.spyOn(Worker.prototype, "postMessage").mockImplementation(function ( |
| 185 | this: Worker, |
| 186 | message: unknown, |
| 187 | options?: Transferable[] | StructuredSerializeOptions, |
| 188 | ) { |
| 189 | if ( |
| 190 | typeof message === "object" && |
| 191 | message !== null && |
| 192 | "method" in message && |
| 193 | message.method === "frame" |
| 194 | ) { |
| 195 | pending.push({ worker: this, message, options }); |
| 196 | return; |
| 197 | } |
| 198 | Reflect.apply(postMessage, this, [message, options]); |
| 199 | }); |
| 200 | // Synthetic pointer events have no browser pointer to capture. |
| 201 | using _capture = vi.spyOn(root, "setPointerCapture").mockImplementation(() => {}); |
| 202 | using _release = vi.spyOn(root, "releasePointerCapture").mockImplementation(() => {}); |
| 203 | const rect = canvas.getBoundingClientRect(); |
| 204 | const pointer = { bubbles: true, pointerId: 1, button: 0, clientY: rect.top + 10 }; |
| 205 | await act(() => { |
| 206 | root.dispatchEvent(new PointerEvent("pointerdown", { ...pointer, clientX: rect.left + 108 })); |
| 207 | root.dispatchEvent(new PointerEvent("pointermove", { ...pointer, clientX: rect.left + 148 })); |
| 208 | root.dispatchEvent(new PointerEvent("pointerup", { ...pointer, clientX: rect.left + 148 })); |
| 209 | }); |
| 210 | await vi.waitFor(() => expect(pending).toHaveLength(1)); |
| 211 | await act(() => root.scrollTo({ top: 2000 })); |
| 212 | await vi.waitFor(() => expect(pending).toHaveLength(2)); |
| 213 | await act(() => root.scrollTo({ top: 0 })); |
| 214 | await vi.waitFor(() => expect(pending).toHaveLength(3)); |
| 215 | expect(pending[2]!.message).toMatchObject({ input: { x: 0, y: 0 } }); |
| 216 | const frame = pending[2]!; |
| 217 | if (typeof frame.message !== "object" || frame.message === null || !("id" in frame.message)) |
| 218 | throw new Error("Missing frame request id"); |
| 219 | frame.worker.dispatchEvent( |
| 220 | new MessageEvent("message", { |
| 221 | data: { id: frame.message.id, ok: false, superseded: false, message: "Frame failed" }, |
| 222 | }), |
| 223 | ); |
| 224 | await expect |
| 225 | .element(screen.getByRole("status")) |
| 226 | .toHaveTextContent("These cells could not be drawn."); |
| 227 | await act(() => root.dispatchEvent(new Event("scroll"))); |
| 228 | await vi.waitFor(() => expect(pending).toHaveLength(4)); |
| 229 | const retry = pending[3]!; |
| 230 | expect(retry.message).toMatchObject({ input: { x: 0, y: 0 } }); |
| 231 | Reflect.apply(postMessage, retry.worker, [retry.message, retry.options]); |
| 232 | await expect.element(screen.getByRole("status")).not.toBeInTheDocument(); |
| 233 | const ratio = window.devicePixelRatio || 1; |
| 234 | await vi.waitFor(() => { |
| 235 | expect([ |
| 236 | ...canvas.getContext("2d")!.getImageData(Math.round(140 * ratio), Math.round(5 * ratio), 1, 1) |
| 237 | .data, |
| 238 | ]).toEqual([211, 236, 221, 255]); |
| 239 | }); |
| 240 | expect(onError).not.toHaveBeenCalled(); |
| 241 | }); |