@@ -5,12 +5,16 @@ import { pathToFileURL } from "node:url";
55import { describe , expect , it } from "vitest" ;
66import { normalizeMediaFacts , resolveMediaFacts } from "../../../media/media-facts.js" ;
77import { captureEnv , setTestEnvValue } from "../../../test-utils/env.js" ;
8+ import type { AgentMessage } from "../../runtime/index.js" ;
89import { createHostSandboxFsBridge } from "../../test-helpers/host-sandbox-fs-bridge.js" ;
9- import { detectAndLoadPromptImages , hasHydratableMediaImages } from "./images.js" ;
10+ import {
11+ detectAndLoadPromptImages ,
12+ hasHydratableMediaImages ,
13+ hydratePromptMediaMessages ,
14+ } from "./images.js" ;
1015
1116const TINY_PNG_BASE64 =
1217 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAADUlEQVR4nGP4////KwAJ5gPoxLp9owAAAABJRU5ErkJggg==" ;
13-
1418describe ( "fact-carried image references" , ( ) => {
1519 it ( "counts only facts that will hydrate an image attachment" , ( ) => {
1620 expect ( hasHydratableMediaImages ( [ { path : "/tmp/photo.png" , kind : "image" } ] ) ) . toBe ( true ) ;
@@ -33,6 +37,98 @@ describe("fact-carried image references", () => {
3337 }
3438 } ) ;
3539
40+ it ( "shares canonical SVG, TIFF, and document classification with embedded image refs" , ( ) => {
41+ expect ( hasHydratableMediaImages ( [ { path : "/tmp/diagram.svg" } ] ) ) . toBe ( false ) ;
42+ expect ( hasHydratableMediaImages ( [ { path : "/tmp/diagram.svg" , kind : "unknown" } ] ) ) . toBe ( false ) ;
43+ expect ( hasHydratableMediaImages ( [ { path : "/tmp/diagram.svg" , kind : "image" } ] ) ) . toBe ( true ) ;
44+ expect ( hasHydratableMediaImages ( [ { path : "/tmp/scan.tiff" } ] ) ) . toBe ( true ) ;
45+ expect (
46+ hasHydratableMediaImages ( [
47+ { path : "/tmp/scan.tif" , contentType : "application/octet-stream" } ,
48+ ] ) ,
49+ ) . toBe ( true ) ;
50+ expect (
51+ hasHydratableMediaImages ( [
52+ { path : "/tmp/report.png" , kind : "unknown" , contentType : "application/pdf" } ,
53+ ] ) ,
54+ ) . toBe ( false ) ;
55+ } ) ;
56+
57+ it . each ( [
58+ {
59+ name : "filename-only SVG" ,
60+ fileName : "diagram.svg" ,
61+ contentType : undefined ,
62+ kind : undefined ,
63+ bytes : Buffer . from ( '<svg xmlns="http://www.w3.org/2000/svg"/>' ) ,
64+ expectedImages : 0 ,
65+ } ,
66+ {
67+ name : "unknown-kind PDF metadata over valid PNG bytes" ,
68+ fileName : "report.png" ,
69+ contentType : "application/pdf" ,
70+ kind : "unknown" as const ,
71+ bytes : Buffer . from ( TINY_PNG_BASE64 , "base64" ) ,
72+ expectedImages : 0 ,
73+ } ,
74+ {
75+ name : "filename-only authentic PNG" ,
76+ fileName : "scan.png" ,
77+ contentType : undefined ,
78+ kind : undefined ,
79+ bytes : Buffer . from ( TINY_PNG_BASE64 , "base64" ) ,
80+ expectedImages : 1 ,
81+ } ,
82+ {
83+ name : "generic-binary authentic PNG" ,
84+ fileName : "scan.png" ,
85+ contentType : "application/octet-stream" ,
86+ kind : undefined ,
87+ bytes : Buffer . from ( TINY_PNG_BASE64 , "base64" ) ,
88+ expectedImages : 1 ,
89+ } ,
90+ ] ) ( "applies canonical $name rules to actual persisted embedded replay" , async ( testCase ) => {
91+ const workspaceDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-canonical-media-ref-" ) ) ;
92+ const imagePath = path . join ( workspaceDir , testCase . fileName ) ;
93+ await fs . writeFile ( imagePath , testCase . bytes ) ;
94+ const media = [ { path : imagePath , contentType : testCase . contentType , kind : testCase . kind } ] ;
95+
96+ try {
97+ const loaded = await detectAndLoadPromptImages ( {
98+ prompt : "" ,
99+ media,
100+ workspaceDir,
101+ model : { input : [ "text" , "image" ] } ,
102+ workspaceOnly : true ,
103+ } ) ;
104+ expect ( loaded . images ) . toHaveLength ( testCase . expectedImages ) ;
105+ expect ( loaded . failedMediaCount ) . toBe ( 0 ) ;
106+ if ( testCase . expectedImages > 0 ) {
107+ expect ( loaded . images [ 0 ] ?. mimeType ) . toBe ( "image/png" ) ;
108+ }
109+
110+ const persisted = {
111+ role : "user" ,
112+ content : "inspect" ,
113+ __openclaw : { media } ,
114+ } as unknown as AgentMessage ;
115+ const replayed = await hydratePromptMediaMessages ( [ persisted ] , {
116+ workspaceDir,
117+ model : { input : [ "text" , "image" ] } ,
118+ workspaceOnly : true ,
119+ } ) ;
120+ const replayedMessage = replayed [ 0 ] ;
121+ const content = replayedMessage ?. role === "user" ? replayedMessage . content : undefined ;
122+ const images = Array . isArray ( content ) ? content . filter ( ( item ) => item . type === "image" ) : [ ] ;
123+ expect ( images ) . toHaveLength ( testCase . expectedImages ) ;
124+ if ( testCase . expectedImages > 0 ) {
125+ expect ( images [ 0 ] ?. mimeType ) . toBe ( "image/png" ) ;
126+ }
127+ } finally {
128+ await fs . rm ( workspaceDir , { recursive : true , force : true } ) ;
129+ }
130+ } ) ;
131+
36132 it ( "retains described-image suppression across fact copy boundaries" , async ( ) => {
37133 const normalized = normalizeMediaFacts ( [
38134 {
0 commit comments