import { expect, test } from "@playwright/test"; import { readFileSync } from "node:fs"; import { resolve } from "node:path"; function readSource(path: string) { return readFileSync(resolve(process.cwd(), path), "utf8"); } function readFunctionBlock(source: string, start: string, end: string) { const startIndex = source.indexOf(start); const endIndex = source.indexOf(end, startIndex + start.length); expect(startIndex, `Missing ${start}`).toBeGreaterThanOrEqual(0); expect(endIndex, `Missing ${end}`).toBeGreaterThan(startIndex); return source.slice(startIndex, endIndex); } test.describe("home to canvas entry performance", () => { test("navigates after project creation without waiting for a list refresh", () => { const source = readSource("components/home/lovart-home.tsx"); const composerFlow = readFunctionBlock( source, "const createCanvasFromComposer = async () =>", "const openNewCanvas = async () =>" ); const blankCanvasFlow = readFunctionBlock( source, "const openNewCanvas = async () =>", "const prepareCanvasProject = useCallback" ); expect(composerFlow).toContain("prepareCanvasNavigation(canvasId)"); expect(composerFlow).toMatch(/router\.push\(`\/canvas\/\$\{canvasId\}`\)/); expect(composerFlow).not.toContain("await mutateCanvas()"); expect(blankCanvasFlow).toContain('fetch("/api/canvas"'); expect(blankCanvasFlow).toMatch(/router\.push\(`\/canvas\/\$\{canvasId\}`\)/); expect(blankCanvasFlow).not.toContain('router.push("/canvas?new=1")'); }); test("starts editor and chat downloads before the user opens the panel", () => { const homeSource = readSource("components/home/lovart-home.tsx"); const editorSource = readSource( "app/canvas/[id]/CanvasEditorLegacyPageClient.tsx" ); const workspacePreloadSource = readSource( "lib/client/canvas-workspace-preload.ts" ); expect(homeSource).toContain("requestIdleCallback(preloadCanvasWorkspace"); expect(homeSource).toContain("timeout: 1200"); expect(homeSource).toContain( "window.setTimeout(preloadCanvasWorkspace, 450)" ); expect(workspacePreloadSource).toContain("preloadCanvasEditor()"); expect(workspacePreloadSource).toContain("preloadCanvasChatPanel()"); expect(editorSource).toContain("preloadCanvasChatPanel();"); expect(editorSource).toContain("preloadCanvasChatData(id);"); const entryWarmup = readFunctionBlock( editorSource, "useEffect(() => {\n preloadCanvasChatPanel();", "const hydrateCanvasProject" ); expect(entryWarmup).toContain( 'scheduleCanvasIdleTask(() => {\n void import("@/components/canvas/CanvasFileListPanel");' ); expect(entryWarmup).not.toContain( 'import("@/components/canvas/CanvasChatPanel")' ); }); test("reuses in-flight editor and chat module downloads", () => { const editorLoader = readSource( "app/canvas/[id]/canvas-editor-loader.ts" ); const chatLoader = readSource( "components/canvas/canvas-chat-panel-loader.ts" ); expect(editorLoader).toContain( 'canvasEditorModulePromise ??= import("./CanvasEditorLegacyPageClient")' ); expect(chatLoader).toContain( 'canvasChatPanelModulePromise ??= import("./CanvasChatPanel")' ); }); });