




























@@ -10,33 +10,77 @@ import {
1010resolveMediaReferenceLocalPath,
1111} from "./media-reference.js";
121213+async function expectMediaReferenceError(
14+run: () => Promise<unknown>,
15+expectedCode: MediaReferenceError["code"],
16+) {
17+let mediaError: unknown;
18+try {
19+await run();
20+} catch (error) {
21+mediaError = error;
22+}
23+expect(mediaError).toBeInstanceOf(MediaReferenceError);
24+if (!(mediaError instanceof MediaReferenceError)) {
25+throw new Error("expected MediaReferenceError");
26+}
27+expect(mediaError.name).toBe("MediaReferenceError");
28+expect(mediaError.code).toBe(expectedCode);
29+}
30+1331describe("media reference helpers", () => {
1432it("normalizes outbound MEDIA tags without changing canonical media URIs", () => {
1533expect(normalizeMediaReferenceSource(" MEDIA: ./out.png")).toBe("./out.png");
1634expect(normalizeMediaReferenceSource("media://inbound/a.png")).toBe("media://inbound/a.png");
1735});
18361937it("classifies supported and unsupported media reference schemes", () => {
20-expect(classifyMediaReferenceSource("media://inbound/a.png")).toMatchObject({
21-isMediaStoreUrl: true,
38+expect(classifyMediaReferenceSource("media://inbound/a.png")).toStrictEqual({
39+hasScheme: true,
2240hasUnsupportedScheme: false,
41+isDataUrl: false,
42+isFileUrl: false,
43+isHttpUrl: false,
44+isMediaStoreUrl: true,
45+looksLikeWindowsDrivePath: false,
2346});
24-expect(classifyMediaReferenceSource("data:image/png;base64,cG5n")).toMatchObject({
25-isDataUrl: true,
47+expect(classifyMediaReferenceSource("data:image/png;base64,cG5n")).toStrictEqual({
48+hasScheme: true,
2649hasUnsupportedScheme: false,
50+isDataUrl: true,
51+isFileUrl: false,
52+isHttpUrl: false,
53+isMediaStoreUrl: false,
54+looksLikeWindowsDrivePath: false,
2755});
2856expect(
2957classifyMediaReferenceSource("data:image/png;base64,cG5n", { allowDataUrl: false }),
30-).toMatchObject({
31-isDataUrl: true,
58+).toStrictEqual({
59+hasScheme: true,
3260hasUnsupportedScheme: true,
61+isDataUrl: true,
62+isFileUrl: false,
63+isHttpUrl: false,
64+isMediaStoreUrl: false,
65+looksLikeWindowsDrivePath: false,
3366});
34-expect(classifyMediaReferenceSource("ftp://example.test/a.png")).toMatchObject({
67+expect(classifyMediaReferenceSource("ftp://example.test/a.png")).toStrictEqual({
68+hasScheme: true,
3569hasUnsupportedScheme: true,
70+isDataUrl: false,
71+isFileUrl: false,
72+isHttpUrl: false,
73+isMediaStoreUrl: false,
74+looksLikeWindowsDrivePath: false,
3675});
37-expect(classifyMediaReferenceSource("C:\\Users\\pete\\image.png")).toMatchObject({
38-looksLikeWindowsDrivePath: true,
76+expect(classifyMediaReferenceSource("C:\\Users\\pete\\image.png")).toStrictEqual({
77+hasScheme: true,
3978hasUnsupportedScheme: false,
79+isDataUrl: false,
80+isFileUrl: false,
81+isHttpUrl: false,
82+isMediaStoreUrl: false,
83+looksLikeWindowsDrivePath: true,
4084});
4185});
4286@@ -49,7 +93,7 @@ describe("media reference helpers", () => {
4993const realFilePath = await fs.realpath(filePath);
50945195try {
52-await expect(resolveInboundMediaReference(`media://inbound/${id}`)).resolves.toMatchObject({
96+await expect(resolveInboundMediaReference(`media://inbound/${id}`)).resolves.toStrictEqual({
5397 id,
5498normalizedSource: `media://inbound/${id}`,
5599physicalPath: realFilePath,
@@ -87,8 +131,9 @@ describe("media reference helpers", () => {
87131const realFilePath = await fs.realpath(filePath);
8813289133try {
90-await expect(resolveInboundMediaReference(filePath)).resolves.toMatchObject({
134+await expect(resolveInboundMediaReference(filePath)).resolves.toStrictEqual({
91135 id,
136+normalizedSource: filePath,
92137physicalPath: realFilePath,
93138sourceType: "path",
94139});
@@ -104,21 +149,22 @@ describe("media reference helpers", () => {
104149});
105150106151it("rejects inbound media URIs with unsupported locations or unsafe ids", async () => {
107-await expect(resolveInboundMediaReference("media://outbound/a.png")).rejects.toMatchObject({
108-code: "path-not-allowed",
109-});
110-await expect(
111-resolveInboundMediaReference("media://inbound/nested%2Fa.png"),
112-).rejects.toBeInstanceOf(MediaReferenceError);
113-await expect(
114-resolveInboundMediaReference("media://inbound/nested%2Fa.png"),
115-).rejects.toMatchObject({ code: "invalid-path" });
116-await expect(resolveInboundMediaReference("media://inbound/")).rejects.toMatchObject({
117-code: "invalid-path",
118-});
119-await expect(resolveInboundMediaReference("media://inbound/%00.png")).rejects.toMatchObject({
120-code: "invalid-path",
121-});
152+await expectMediaReferenceError(
153+() => resolveInboundMediaReference("media://outbound/a.png"),
154+"path-not-allowed",
155+);
156+await expectMediaReferenceError(
157+() => resolveInboundMediaReference("media://inbound/nested%2Fa.png"),
158+"invalid-path",
159+);
160+await expectMediaReferenceError(
161+() => resolveInboundMediaReference("media://inbound/"),
162+"invalid-path",
163+);
164+await expectMediaReferenceError(
165+() => resolveInboundMediaReference("media://inbound/%00.png"),
166+"invalid-path",
167+);
122168});
123169124170it("rejects symlinked inbound media files", async () => {
@@ -133,12 +179,11 @@ describe("media reference helpers", () => {
133179await fs.symlink(targetPath, linkPath);
134180135181try {
136-await expect(resolveInboundMediaReference(`media://inbound/${id}`)).rejects.toMatchObject({
137-code: "invalid-path",
138-});
139-await expect(resolveInboundMediaReference(linkPath)).rejects.toMatchObject({
140-code: "invalid-path",
141-});
182+await expectMediaReferenceError(
183+() => resolveInboundMediaReference(`media://inbound/${id}`),
184+"invalid-path",
185+);
186+await expectMediaReferenceError(() => resolveInboundMediaReference(linkPath), "invalid-path");
142187} finally {
143188await fs.rm(linkPath, { force: true });
144189await fs.rm(targetDir, { recursive: true, force: true });
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。