11import { describe , expect , it } from "vitest" ;
2- import { isEmptyUserTextOnlyMessage } from "../../lib/chat/message-extract.ts" ;
2+ import {
3+ isEmptyUserTextOnlyMessage ,
4+ readTranscriptMediaEntries ,
5+ } from "../../lib/chat/message-extract.ts" ;
6+ import { extractTranscriptAttachments } from "./components/chat-message-media.ts" ;
7+
8+ const MANAGED_UUID = "43007e90-2ade-43f2-a781-42b843e9eca3" ;
9+
10+ function userMessageWithMedia ( media : unknown [ ] ) : {
11+ role : string ;
12+ content : string ;
13+ __openclaw : { media : unknown [ ] } ;
14+ } {
15+ return { role : "user" , content : "" , __openclaw : { media } } ;
16+ }
317
418describe ( "chat history canonical media filtering" , ( ) => {
519 it . each ( [
@@ -21,3 +35,99 @@ describe("chat history canonical media filtering", () => {
2135 expect ( isEmptyUserTextOnlyMessage ( { role : "user" , content : "" } ) ) . toBe ( true ) ;
2236 } ) ;
2337} ) ;
38+
39+ describe ( "chat history attachment card labels" , ( ) => {
40+ it ( "carries the persisted canonical fileName through the media projection" , ( ) => {
41+ const entries = readTranscriptMediaEntries (
42+ userMessageWithMedia ( [
43+ {
44+ path : `media://inbound/report---${ MANAGED_UUID } .pdf` ,
45+ fileName : "report.pdf" ,
46+ contentType : "application/pdf" ,
47+ } ,
48+ ] ) ,
49+ ) ;
50+ expect ( entries [ 0 ] ?. fileName ) . toBe ( "report.pdf" ) ;
51+ } ) ;
52+
53+ it ( "labels a managed inbound attachment with the persisted original fileName" , ( ) => {
54+ const attachments = extractTranscriptAttachments (
55+ userMessageWithMedia ( [
56+ {
57+ path : `media://inbound/report---${ MANAGED_UUID } .pdf` ,
58+ fileName : "report.pdf" ,
59+ contentType : "application/pdf" ,
60+ } ,
61+ ] ) ,
62+ ) ;
63+ expect ( attachments [ 0 ] ?. attachment . label ) . toBe ( "report.pdf" ) ;
64+ } ) ;
65+
66+ it ( "restores the original name from a legacy managed inbound UUID suffix when fileName is absent" , ( ) => {
67+ const attachments = extractTranscriptAttachments (
68+ userMessageWithMedia ( [
69+ {
70+ path : `media://inbound/openclaw-attachment-test---${ MANAGED_UUID } .docx` ,
71+ contentType : "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
72+ } ,
73+ ] ) ,
74+ ) ;
75+ expect ( attachments [ 0 ] ?. attachment . label ) . toBe ( "openclaw-attachment-test.docx" ) ;
76+ } ) ;
77+
78+ it ( "leaves non-managed https attachment paths unchanged" , ( ) => {
79+ // `---old` is not a canonical managed UUID suffix, so it must not be stripped
80+ // from https URLs (which never carry the collision-safe managed suffix).
81+ const attachments = extractTranscriptAttachments (
82+ userMessageWithMedia ( [
83+ {
84+ path : "https://example.com/files/openclaw-attachment-test---old.docx" ,
85+ contentType : "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
86+ } ,
87+ ] ) ,
88+ ) ;
89+ expect ( attachments [ 0 ] ?. attachment . label ) . toBe ( "openclaw-attachment-test---old.docx" ) ;
90+ } ) ;
91+
92+ it ( "does not strip a managed inbound basename that lacks a UUID suffix" , ( ) => {
93+ const attachments = extractTranscriptAttachments (
94+ userMessageWithMedia ( [
95+ {
96+ path : "media://inbound/plain-name.docx" ,
97+ contentType : "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
98+ } ,
99+ ] ) ,
100+ ) ;
101+ expect ( attachments [ 0 ] ?. attachment . label ) . toBe ( "plain-name.docx" ) ;
102+ } ) ;
103+
104+ it ( "strips only the terminal managed UUID suffix, preserving a UUID-shaped segment in the original name" , ( ) => {
105+ // Legacy record (no persisted fileName) whose original filename itself
106+ // contains a "---<uuid>"-shaped segment before the terminal managed suffix.
107+ const attachments = extractTranscriptAttachments (
108+ userMessageWithMedia ( [
109+ {
110+ path : `media://inbound/report---a1b2c3d4-e5f6-7890-abcd-ef1234567890-final---${ MANAGED_UUID } .docx` ,
111+ contentType : "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
112+ } ,
113+ ] ) ,
114+ ) ;
115+ expect ( attachments [ 0 ] ?. attachment . label ) . toBe (
116+ "report---a1b2c3d4-e5f6-7890-abcd-ef1234567890-final.docx" ,
117+ ) ;
118+ } ) ;
119+
120+ it ( "preserves a dotted UUID-shaped segment in a legacy attachment name" , ( ) => {
121+ const path = `media://inbound/report---a1b2c3d4-e5f6-7890-abcd-ef1234567890.backup---${ MANAGED_UUID } .pdf` ;
122+ const attachments = extractTranscriptAttachments (
123+ userMessageWithMedia ( [ { path, contentType : "application/pdf" } ] ) ,
124+ ) ;
125+
126+ expect ( attachments [ 0 ] ?. attachment ) . toMatchObject ( {
127+ url : path ,
128+ label : "report---a1b2c3d4-e5f6-7890-abcd-ef1234567890.backup.pdf" ,
129+ mimeType : "application/pdf" ,
130+ } ) ;
131+ expect ( attachments [ 0 ] ?. attachment . label ) . not . toContain ( MANAGED_UUID ) ;
132+ } ) ;
133+ } ) ;
0 commit comments