|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | 2 | import { deflateSync } from "node:zlib"; |
3 | 3 | import { unlink } from "node:fs/promises"; |
4 | | -import { tmpdir } from "node:os"; |
5 | | -import { join } from "node:path"; |
| 4 | +import { homedir, tmpdir } from "node:os"; |
| 5 | +import { join, resolve } from "node:path"; |
6 | 6 | import { defined } from "../../tests/helpers/defined.js"; |
7 | 7 | import { |
8 | 8 | findDuplicateAttachment, |
@@ -72,29 +72,117 @@ function buildTestPng(width: number, height: number): Buffer { |
72 | 72 | ]); |
73 | 73 | } |
74 | 74 |
|
75 | | -describe("image attachment helpers", () => { |
76 | | - test("detects supported image MIME types from paths", () => { |
77 | | - expect(imageMimeTypeForPath("shot.png")).toBe("image/png"); |
78 | | - expect(imageMimeTypeForPath("photo.JPEG")).toBe("image/jpeg"); |
79 | | - expect(imageMimeTypeForPath("animation.gif")).toBe("image/gif"); |
80 | | - expect(imageMimeTypeForPath("notes.txt")).toBeUndefined(); |
| 75 | +describe("findImagePathMentions", () => { |
| 76 | + test("preserves balanced wrappers and exact inner whitespace", () => { |
| 77 | + const observed = |
| 78 | + "/Users/operator/Desktop/Screenshot 2026-09-10 at 11.42.07\u202fAM.png"; |
| 79 | + |
| 80 | + expect(findImagePathMentions(`inspect '${observed}'`, "/repo")).toEqual([ |
| 81 | + { raw: `'${observed}'`, path: observed }, |
| 82 | + ]); |
| 83 | + expect( |
| 84 | + findImagePathMentions( |
| 85 | + '"./screen (final), version; 2.png", `~/screen: final!.webp`!', |
| 86 | + "/repo", |
| 87 | + ), |
| 88 | + ).toEqual([ |
| 89 | + { |
| 90 | + raw: '"./screen (final), version; 2.png"', |
| 91 | + path: "/repo/screen (final), version; 2.png", |
| 92 | + }, |
| 93 | + { |
| 94 | + raw: "`~/screen: final!.webp`", |
| 95 | + path: join(homedir(), "screen: final!.webp"), |
| 96 | + }, |
| 97 | + ]); |
81 | 98 | }); |
82 | 99 |
|
83 | | - test("finds image paths embedded in instructions", () => { |
| 100 | + test("normalizes wrapped file URLs and keeps quoted backslashes literal", () => { |
84 | 101 | expect( |
85 | 102 | findImagePathMentions( |
86 | | - "what is in /tmp/Screenshot 2026-01-01.png please", |
| 103 | + '`file:///tmp/my%20shot.jpeg` and "/tmp/my\\ shot.png"', |
| 104 | + "/repo", |
| 105 | + ), |
| 106 | + ).toEqual([ |
| 107 | + { raw: "`file:///tmp/my%20shot.jpeg`", path: "/tmp/my shot.jpeg" }, |
| 108 | + { raw: '"/tmp/my\\ shot.png"', path: "/tmp/my\\ shot.png" }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | + |
| 112 | + test("preserves every unquoted terminator", () => { |
| 113 | + expect(findImagePathMentions("/tmp/shot.png", "/repo")).toEqual([ |
| 114 | + { raw: "/tmp/shot.png", path: "/tmp/shot.png" }, |
| 115 | + ]); |
| 116 | + for (const terminator of [ |
| 117 | + " ", |
| 118 | + "\t", |
| 119 | + "\n", |
| 120 | + ")", |
| 121 | + ",", |
| 122 | + ".", |
| 123 | + ";", |
| 124 | + ":", |
| 125 | + "!", |
| 126 | + "?", |
| 127 | + ]) { |
| 128 | + expect( |
| 129 | + findImagePathMentions(`/tmp/shot.png${terminator}after`, "/repo"), |
| 130 | + ).toEqual([{ raw: "/tmp/shot.png", path: "/tmp/shot.png" }]); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + test("preserves unquoted parsing and normalization", () => { |
| 135 | + expect( |
| 136 | + findImagePathMentions( |
| 137 | + "what is in /tmp/Screenshot 2026-01-01.png please file:///tmp/my%20shot.jpg ./other.webp", |
87 | 138 | "/repo", |
88 | 139 | ), |
89 | 140 | ).toEqual([ |
90 | 141 | { |
91 | 142 | raw: "/tmp/Screenshot 2026-01-01.png", |
92 | 143 | path: "/tmp/Screenshot 2026-01-01.png", |
93 | 144 | }, |
| 145 | + { raw: "file:///tmp/my%20shot.jpg", path: "/tmp/my shot.jpg" }, |
| 146 | + { raw: "./other.webp", path: "/repo/other.webp" }, |
| 147 | + ]); |
| 148 | + expect(findImagePathMentions("./relative path.png", "/repo")).toEqual([]); |
| 149 | + }); |
| 150 | + |
| 151 | + test("bounds unmatched wrappers to their line and old unquoted boundaries", () => { |
| 152 | + expect( |
| 153 | + findImagePathMentions( |
| 154 | + "look at '/tmp/first.png next\nthen' /tmp/second.jpg", |
| 155 | + "/repo", |
| 156 | + ), |
| 157 | + ).toEqual([ |
| 158 | + { raw: "/tmp/first.png", path: "/tmp/first.png" }, |
| 159 | + { raw: "/tmp/second.jpg", path: "/tmp/second.jpg" }, |
| 160 | + ]); |
| 161 | + expect(findImagePathMentions('/tmp/shot.png"', "/repo")).toEqual([]); |
| 162 | + expect(findImagePathMentions('"./first.png\ncontinued"', "/repo")).toEqual([ |
| 163 | + { raw: "./first.png", path: resolve("/repo", "first.png") }, |
94 | 164 | ]); |
| 165 | + }); |
| 166 | + |
| 167 | + test("keeps source order, deduplicates normalized paths, and rejects unsupported candidates", () => { |
95 | 168 | expect( |
96 | | - findImagePathMentions("look at file:///tmp/my%20shot.png", "/repo"), |
97 | | - ).toEqual([{ raw: "file:///tmp/my%20shot.png", path: "/tmp/my shot.png" }]); |
| 169 | + findImagePathMentions( |
| 170 | + "`./first.gif` /repo/second.JPG './first.gif' image.png './bad.bmp' \"./valid.png.txt\"", |
| 171 | + "/repo", |
| 172 | + ), |
| 173 | + ).toEqual([ |
| 174 | + { raw: "`./first.gif`", path: "/repo/first.gif" }, |
| 175 | + { raw: "/repo/second.JPG", path: "/repo/second.JPG" }, |
| 176 | + ]); |
| 177 | + }); |
| 178 | +}); |
| 179 | + |
| 180 | +describe("image attachment helpers", () => { |
| 181 | + test("detects supported image MIME types from paths", () => { |
| 182 | + expect(imageMimeTypeForPath("shot.png")).toBe("image/png"); |
| 183 | + expect(imageMimeTypeForPath("photo.JPEG")).toBe("image/jpeg"); |
| 184 | + expect(imageMimeTypeForPath("animation.gif")).toBe("image/gif"); |
| 185 | + expect(imageMimeTypeForPath("notes.txt")).toBeUndefined(); |
98 | 186 | }); |
99 | 187 |
|
100 | 188 | test("leaves small images untouched", async () => { |
|
0 commit comments