Files
multica/packages/core/workspace/avatar-url.test.ts
Alex 746c0c4456 MUL-2746 fix(avatar): normalize relative avatar urls in desktop/web (#3100)
* fix(avatar): normalize relative avatar urls in desktop/web

Co-authored-by: multica-agent <github@multica.ai>

* fix: test

Co-authored-by: multica-agent <github@multica.ai>

* fix(avatar): normalize avatar url in AvatarPicker preview

MUL-2746. The picker is used by create-agent and create-squad, and also
prefills from a template's `avatar_url` when duplicating an agent. The
upload result / template URL is root-relative in local-storage setups,
so on Desktop (file:// runtime) the preview <img> resolves against the
local filesystem and the avatar fails to render. Route the value through
`resolvePublicFileUrl` for rendering only; the stored URL stays raw so
the parent's create call still posts what the backend expects.

Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: multica-agent <github@multica.ai>
Co-authored-by: J (Multica agent) <agents@multica.ai>
2026-05-27 16:02:08 +08:00

40 lines
1.3 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { resolvePublicFileUrl, resolvePublicFileUrlWithBase } from "./avatar-url";
vi.mock("../api", () => ({
api: {
getBaseUrl: () => "http://127.0.0.1:8080",
},
}));
describe("resolvePublicFileUrlWithBase", () => {
it("resolves root-relative URLs against base URL", () => {
expect(resolvePublicFileUrlWithBase("/uploads/a.png", "http://127.0.0.1:8080")).toBe(
"http://127.0.0.1:8080/uploads/a.png",
);
});
it("trims trailing slash in base URL", () => {
expect(resolvePublicFileUrlWithBase("/upload/a.png", "http://127.0.0.1:8080/")).toBe(
"http://127.0.0.1:8080/upload/a.png",
);
});
it("keeps absolute URLs unchanged", () => {
expect(resolvePublicFileUrlWithBase("https://cdn.example.com/a.png", "http://127.0.0.1:8080")).toBe(
"https://cdn.example.com/a.png",
);
});
it("returns null for empty values", () => {
expect(resolvePublicFileUrlWithBase(null, "http://127.0.0.1:8080")).toBeNull();
expect(resolvePublicFileUrlWithBase(undefined, "http://127.0.0.1:8080")).toBeNull();
});
});
describe("resolvePublicFileUrl", () => {
it("uses API base URL implicitly", () => {
expect(resolvePublicFileUrl("/uploads/a.png")).toBe("http://127.0.0.1:8080/uploads/a.png");
});
});