mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-09 03:07:46 +08:00
The canvas now fills the window and the chat floats on its right. draw.io is served from our own origin (public/drawio, downloaded at build time and checked against the release's SHA-256), so the app can drive the editor directly: - every AI change becomes a version card with a thumbnail; compare, restore or undo it, and Ctrl+Z on the canvas takes an AI change back in one step - shapes the AI just changed are highlighted on the canvas - selected shapes appear as a chip and are sent to the model - draw.io's own simple UI: its toolbar, menus (auto layout, Mermaid, CSV import) and page tabs; the shape library starts closed - new start screen, session rename and switcher, open .drawio files - settings split into Models & keys, Appearance, Advanced and About - Bedrock in the model settings also takes an API key instead of access keys - phones switch between canvas and chat - dark mode uses draw.io's own dark mode, with neutral greys around it With an external draw.io (NEXT_PUBLIC_DRAWIO_BASE_URL) the features above that need same origin are off. The chat logic moved from the 1500-line chat panel into a chat engine plus small UI components; zustand holds canvas, versions, settings and UI state.
158 lines
5.3 KiB
TypeScript
158 lines
5.3 KiB
TypeScript
import { SINGLE_BOX_XML } from "./fixtures/diagrams"
|
|
import {
|
|
expect,
|
|
getAttachmentInput,
|
|
getChatInput,
|
|
getIframe,
|
|
sendMessage,
|
|
test,
|
|
} from "./lib/fixtures"
|
|
import { createMockSSEResponse } from "./lib/helpers"
|
|
|
|
test.describe("File Upload", () => {
|
|
test("upload button opens file picker", async ({ page }) => {
|
|
await page.goto("/", { waitUntil: "networkidle" })
|
|
await getIframe(page).waitFor({ state: "visible", timeout: 30000 })
|
|
|
|
// The "Add" menu holds the upload entry
|
|
const addButton = page.locator('[data-testid="composer-add"]')
|
|
await expect(addButton).toBeVisible({ timeout: 10000 })
|
|
await addButton.click()
|
|
const [chooser] = await Promise.all([
|
|
page.waitForEvent("filechooser"),
|
|
page.getByRole("button", { name: "Upload file" }).click(),
|
|
])
|
|
expect(chooser.isMultiple()).toBe(true)
|
|
})
|
|
|
|
test("shows file preview after selecting image", async ({ page }) => {
|
|
await page.goto("/", { waitUntil: "networkidle" })
|
|
await getIframe(page).waitFor({ state: "visible", timeout: 30000 })
|
|
|
|
const fileInput = getAttachmentInput(page)
|
|
|
|
await fileInput.setInputFiles({
|
|
name: "test-image.png",
|
|
mimeType: "image/png",
|
|
buffer: Buffer.from(
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
|
|
"base64",
|
|
),
|
|
})
|
|
|
|
await expect(
|
|
page.locator('[role="alert"][data-type="error"]'),
|
|
).not.toBeVisible({ timeout: 2000 })
|
|
})
|
|
|
|
test("can remove uploaded file", async ({ page }) => {
|
|
await page.goto("/", { waitUntil: "networkidle" })
|
|
await getIframe(page).waitFor({ state: "visible", timeout: 30000 })
|
|
|
|
const fileInput = getAttachmentInput(page)
|
|
|
|
await fileInput.setInputFiles({
|
|
name: "test-image.png",
|
|
mimeType: "image/png",
|
|
buffer: Buffer.from(
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
|
|
"base64",
|
|
),
|
|
})
|
|
|
|
await expect(
|
|
page.locator('[role="alert"][data-type="error"]'),
|
|
).not.toBeVisible({ timeout: 2000 })
|
|
|
|
const removeButton = page.locator(
|
|
'[data-testid="remove-file-button"], button[aria-label*="Remove"], button:has(svg.lucide-x)',
|
|
)
|
|
|
|
const removeButtonCount = await removeButton.count()
|
|
if (removeButtonCount === 0) {
|
|
test.skip()
|
|
return
|
|
}
|
|
|
|
await removeButton.first().click()
|
|
await expect(removeButton.first()).not.toBeVisible({ timeout: 2000 })
|
|
})
|
|
|
|
test("sends file with message to API", async ({ page }) => {
|
|
let capturedRequest: any = null
|
|
|
|
await page.route("**/api/chat", async (route) => {
|
|
capturedRequest = route.request()
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "text/event-stream",
|
|
body: createMockSSEResponse(
|
|
SINGLE_BOX_XML,
|
|
"Based on your image, here is a diagram:",
|
|
),
|
|
})
|
|
})
|
|
|
|
await page.goto("/", { waitUntil: "networkidle" })
|
|
await getIframe(page).waitFor({ state: "visible", timeout: 30000 })
|
|
|
|
const fileInput = getAttachmentInput(page)
|
|
|
|
await fileInput.setInputFiles({
|
|
name: "architecture.png",
|
|
mimeType: "image/png",
|
|
buffer: Buffer.from(
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
|
|
"base64",
|
|
),
|
|
})
|
|
|
|
await sendMessage(page, "Convert this to a diagram")
|
|
|
|
await expect(
|
|
page.locator('text="Based on your image, here is a diagram:"'),
|
|
).toBeVisible({ timeout: 15000 })
|
|
|
|
expect(capturedRequest).not.toBeNull()
|
|
})
|
|
|
|
test("shows error for oversized file", async ({ page }) => {
|
|
await page.goto("/", { waitUntil: "networkidle" })
|
|
await getIframe(page).waitFor({ state: "visible", timeout: 30000 })
|
|
|
|
const fileInput = getAttachmentInput(page)
|
|
const largeBuffer = Buffer.alloc(3 * 1024 * 1024, "x")
|
|
|
|
await fileInput.setInputFiles({
|
|
name: "large-image.png",
|
|
mimeType: "image/png",
|
|
buffer: largeBuffer,
|
|
})
|
|
|
|
await expect(
|
|
page.locator('[role="alert"], [data-sonner-toast]').first(),
|
|
).toBeVisible({ timeout: 5000 })
|
|
})
|
|
|
|
test("drag and drop file upload works", async ({ page }) => {
|
|
await page.goto("/", { waitUntil: "networkidle" })
|
|
await getIframe(page).waitFor({ state: "visible", timeout: 30000 })
|
|
|
|
const chatForm = page.locator("form").first()
|
|
|
|
const dataTransfer = await page.evaluateHandle(() => {
|
|
const dt = new DataTransfer()
|
|
const file = new File(["test content"], "dropped-image.png", {
|
|
type: "image/png",
|
|
})
|
|
dt.items.add(file)
|
|
return dt
|
|
})
|
|
|
|
await chatForm.dispatchEvent("dragover", { dataTransfer })
|
|
await chatForm.dispatchEvent("drop", { dataTransfer })
|
|
|
|
await expect(getChatInput(page)).toBeVisible({ timeout: 3000 })
|
|
})
|
|
})
|