Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules/
node_modules
dist/
.DS_Store

Expand Down
1 change: 1 addition & 0 deletions scripts/run-browser-suite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const workflows = [
},
{ name: "first-run setup", script: "tests/browser/first_run.mjs" },
{ name: "selection toolbar hover", script: "tests/browser/selection_toolbar_hover.mjs" },
{ name: "node media upload", script: "tests/browser/node_media_upload.mjs" },
{ name: "model editor hosts", script: "tests/browser/model_editor_hosts.mjs" },
{ name: "motion studio", script: "tests/browser/motion_studio.mjs" },
];
Expand Down
15 changes: 14 additions & 1 deletion src/canvas/workspace/useWorkspaceTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,20 @@ export function useWorkspaceTransfer(rt: WorkspaceRuntime, readOnly: boolean) {
controller.signal,
);
if (target && failures.length) throw new Error(failures[0]!.message);
for (const { index: i, value: resource } of successes) {
for (const { index: i, value: imported } of successes) {
const resource =
target && i === 0 && !imported.assetId && imported.kind !== "text" && files[i]
? await workspaceProjection(importDocumentId).resource(
await creationClient(importDocumentId).importBlob(
files[i]!,
files[i]!.name,
imported.kind,
undefined,
undefined,
controller.signal,
),
)
: imported;
const kind = resource.kind;
const data = resourceNodeData(resource);
if (target && i === 0) {
Expand Down
5 changes: 5 additions & 0 deletions tests/browser/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@
"lanes": ["gate"],
"note": "Pointing at a labelled action opens its menu without a click, keeps it open across the pointer gap, and never steals focus."
},
{
"file": "node_media_upload.mjs",
"lanes": ["gate"],
"note": "Media uploaded into an existing node is saved as an asset instead of a page-local preview that disappears on refresh."
},
{
"file": "model_editor_hosts.mjs",
"previewSafe": true,
Expand Down
185 changes: 185 additions & 0 deletions tests/browser/node_media_upload.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import assert from "node:assert/strict";
import { launchBrowser } from "./engine.mjs";

const base = process.env.CREATION_WEB_URL ?? "http://127.0.0.1:5179";
const headers = { "X-OpenDirector-Request": "1" };
const health = await fetch(`${base}/api/creation-test-health`, { headers });
assert.ok(health.ok && (await health.json()).isolated, "Requires an isolated creation API");

const picture = {
name: "replace-probe.png",
mimeType: "image/png",
buffer: Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAQUlEQVR42u3OMQEAAAgDoJnc6BpjDyQgdWXm5wEECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAgW8LPAABAV/9nQ0AAAAASUVORK5CYII=",
"base64",
),
};

const replacement = {
name: "replace-probe-2.png",
mimeType: "image/png",
buffer: Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAACXBIWXMAAAABAAAAAQBPJcTWAAAAb0lEQVR4nO3PAQkAAAyEwO8feiyGCMIF0G0nxxc0IMcXNCDHFzQgxxc0IMcXNCDHFzQgxxc0IMcXNCDHFzQgxxc0IMcXNCDHFzQgxxc0IMcXNCDHFzQgxxc0IMcXNCDHFzQgxxc0IMcXNCDHFzSg9sFe4OIMD8UiAAAAAElFTkSuQmCC",
"base64",
),
};

const browser = await launchBrowser();
try {
const page = await browser.newPage({ locale: "en-US", viewport: { width: 1440, height: 900 } });
await page.addInitScript(() => {
try { localStorage.setItem("opendirector.modelSetup.v1", "dismissed"); } catch {}
});
page.setDefaultTimeout(20000);
const errors = [];
page.on("pageerror", (error) => errors.push(error.message));

const seed = await browser.newPage({ locale: "en-US" });
await seed.route((url) => url.pathname === "/", (route) =>
route.fulfill({ contentType: "text/html", body: "<html><body></body></html>" }));
await seed.goto(`${base}/`);
const project = await seed.evaluate(async () => {
const { createDocument, createNode } = await import("/src/canvas/model.ts");
const { importWorkspaceDocument } = await import("/src/backend/workspace.ts");
const { releaseBackendProject } = await import("/src/backend/projects.ts");
const document = createDocument(`Media replace ${crypto.randomUUID().slice(0, 8)}`);
const node = createNode("image", { x: 120, y: 120 }, { title: "Empty image" });
document.nodes = [node];
const saved = await importWorkspaceDocument(document);
await releaseBackendProject();
return { id: saved.id, node: node.id };
});
await seed.close();
const nodeId = project.node;

await page.goto(`${base}/ai-tool/ai-canvas/${project.id}`);
await page.waitForFunction(() => {
const workspace = document.querySelector(".cw-workspace");
return workspace?.getAttribute("aria-busy") === "false" && !workspace.inert;
});

const node = page.locator(`.react-flow__node[data-id="${nodeId}"]`);
await node.waitFor();
await node.hover();
const uploads = [];
page.on("request", (request) => {
if (request.method() === "PUT" && request.url().includes("/uploads/")) uploads.push(request.url());
});
const chooser = page.waitForEvent("filechooser");
await node.getByRole("button", { name: /Upload image/i }).click();
await (await chooser).setFiles(picture);
let item = null;
for (let attempt = 0; attempt < 120; attempt += 1) {
const response = await fetch(`${base}/api/creation/projects/${project.id}`, { headers });
const state = await response.json();
item = state.canvasItems.find((entry) => entry.id === nodeId);
if (item?.binding?.role === "material" && item?.binding?.objectRef?.assetId) break;
await page.waitForTimeout(100);
}
assert.equal(uploads.length, 1, "replacing node media must upload its bytes exactly once, not keep a page-local preview");

const snapshot = await (await fetch(`${base}/api/creation/projects/${project.id}`, { headers })).json();
item = snapshot.canvasItems.find((entry) => entry.id === nodeId);
assert.equal(item.binding.role, "material");
const assetId = item.binding.objectRef.assetId;
const asset = snapshot.assets.find((entry) => entry.id === assetId);
assert.ok(asset, "the replaced media must be registered as an asset");
assert.equal(asset.name, picture.name);

await page.reload();
await page.waitForFunction(() => {
const workspace = document.querySelector(".cw-workspace");
return workspace?.getAttribute("aria-busy") === "false" && !workspace.inert;
});
await page.waitForFunction((id) => {
const image = document.querySelector(`.react-flow__node[data-id="${id}"] img`);
return Boolean(image && image.complete && image.naturalWidth > 0);
}, nodeId, { timeout: 20000 });
assert.equal(await node.locator(".cw-placeholder-upload").count(), 0);
assert.deepEqual(errors, []);

await node.click({ button: "right" });
const secondChooser = page.waitForEvent("filechooser");
await page.getByRole("menuitem", { name: "Replace asset" }).click();
await (await secondChooser).setFiles(replacement);
let replaced = null;
for (let attempt = 0; attempt < 120; attempt += 1) {
const response = await fetch(`${base}/api/creation/projects/${project.id}`, { headers });
const state = await response.json();
const current = state.canvasItems.find((entry) => entry.id === nodeId);
if (
current?.binding?.role === "material" &&
current.binding.objectRef?.assetId &&
current.binding.objectRef.assetId !== assetId
) {
replaced = state;
break;
}
await page.waitForTimeout(100);
}
assert.ok(replaced, "replacing a registered asset must bind a new asset");
assert.equal(uploads.length, 2, "each replacement uploads its bytes exactly once");
const replacedItem = replaced.canvasItems.find((entry) => entry.id === nodeId);
const replacedAsset = replaced.assets.find((entry) => entry.id === replacedItem.binding.objectRef.assetId);
assert.equal(replacedAsset.name, replacement.name);

await page.reload();
await page.waitForFunction(() => {
const workspace = document.querySelector(".cw-workspace");
return workspace?.getAttribute("aria-busy") === "false" && !workspace.inert;
});
await page.waitForFunction((id) => {
const image = document.querySelector(`.react-flow__node[data-id="${id}"] img`);
return Boolean(image && image.complete && image.naturalWidth > 0);
}, nodeId, { timeout: 20000 });
const persisted = await (await fetch(`${base}/api/creation/projects/${project.id}`, { headers })).json();
assert.equal(
persisted.canvasItems.find((entry) => entry.id === nodeId).binding.objectRef.assetId,
replacedItem.binding.objectRef.assetId,
);
assert.deepEqual(errors, []);

const failing = await browser.newPage({ locale: "en-US", viewport: { width: 1440, height: 900 } });
await failing.addInitScript(() => {
try { localStorage.setItem("opendirector.modelSetup.v1", "dismissed"); } catch {}
});
failing.setDefaultTimeout(20000);
const seedFailure = await browser.newPage({ locale: "en-US" });
await seedFailure.route((url) => url.pathname === "/", (route) =>
route.fulfill({ contentType: "text/html", body: "<html><body></body></html>" }));
await seedFailure.goto(`${base}/`);
const failureProject = await seedFailure.evaluate(async () => {
const { createDocument, createNode } = await import("/src/canvas/model.ts");
const { importWorkspaceDocument } = await import("/src/backend/workspace.ts");
const { releaseBackendProject } = await import("/src/backend/projects.ts");
const document = createDocument(`Media failure ${crypto.randomUUID().slice(0, 8)}`);
const node = createNode("image", { x: 120, y: 120 }, { title: "Empty image" });
document.nodes = [node];
const saved = await importWorkspaceDocument(document);
await releaseBackendProject();
return { id: saved.id, node: node.id };
});
await seedFailure.close();
await failing.goto(`${base}/ai-tool/ai-canvas/${failureProject.id}`);
await failing.waitForFunction(() => {
const workspace = document.querySelector(".cw-workspace");
return workspace?.getAttribute("aria-busy") === "false" && !workspace.inert;
});
const failingNode = failing.locator(`.react-flow__node[data-id="${failureProject.node}"]`);
await failingNode.waitFor();
await failingNode.hover();
await failing.route("**/uploads/**", (route) =>
route.fulfill({ status: 500, contentType: "application/json", body: JSON.stringify({ detail: "fixture upload failure" }) }));
const failingChooser = failing.waitForEvent("filechooser");
await failingNode.getByRole("button", { name: /Upload image/i }).click();
await (await failingChooser).setFiles(picture);
await failing.locator(".cw-toast [role=status], .cw-toast [role=alert]")
.filter({ hasText: "fixture upload failure" }).waitFor();
const failedSnapshot = await (await fetch(`${base}/api/creation/projects/${failureProject.id}`, { headers })).json();
const failedItem = failedSnapshot.canvasItems.find((entry) => entry.id === failureProject.node);
assert.ok(!failedItem.binding?.objectRef?.assetId, "a failed upload must not bind an asset");
await failing.close();
console.log("Replacing node media uploads and keeps the saved asset");
} finally {
await browser.close();
}
Loading