1+ import fs from "node:fs/promises" ;
2+ import os from "node:os" ;
3+ import path from "node:path" ;
14import { expectDefined } from "@openclaw/normalization-core" ;
25// Slack tests cover media plugin behavior.
36import type { WebClient } from "@slack/web-api" ;
@@ -127,10 +130,12 @@ const saveRemoteMediaMock = vi.hoisted(() =>
127130) ;
128131const fetchWithRuntimeDispatcherMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
129132const logVerboseMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
133+ const mediaWarnMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
130134
131135vi . mock ( "./media.runtime.js" , ( ) => ( {
132136 fetchWithRuntimeDispatcher : fetchWithRuntimeDispatcherMock ,
133137 saveRemoteMedia : saveRemoteMediaMock ,
138+ slackMediaLog : { warn : mediaWarnMock } ,
134139} ) ) ;
135140
136141vi . mock ( "./thread.runtime.js" , ( ) => ( {
@@ -161,6 +166,7 @@ beforeEach(() => {
161166 readRemoteMediaBufferMock . mockClear ( ) ;
162167 fetchWithRuntimeDispatcherMock . mockClear ( ) ;
163168 logVerboseMock . mockClear ( ) ;
169+ mediaWarnMock . mockClear ( ) ;
164170 saveMediaBufferMock . mockReset ( ) ;
165171 saveMediaBufferMock . mockImplementation (
166172 async (
@@ -701,6 +707,7 @@ describe("resolveSlackMedia", () => {
701707 "https://files.slack.com/stale.jpg" ,
702708 "https://files.slack.com/fresh.jpg" ,
703709 ] ) ;
710+ expect ( mediaWarnMock ) . not . toHaveBeenCalled ( ) ;
704711 } ) ;
705712
706713 it ( "rejects a refreshed URL when its file metadata fails caller admission" , async ( ) => {
@@ -742,23 +749,36 @@ describe("resolveSlackMedia", () => {
742749 ] ) ;
743750 } ) ;
744751
745- it ( "rejects HTML auth pages for non-HTML files" , async ( ) => {
746- mockFetch . mockResolvedValueOnce (
747- new Response ( "<!DOCTYPE html><html><body>login</body></html>" , {
748- status : 200 ,
749- headers : { "content-type" : "text/html; charset=utf-8" } ,
750- } ) ,
751- ) ;
752-
753- const result = await resolveSlackMedia ( {
754- files : [ { url_private : "https://files.slack.com/test.jpg" , name : "test.jpg" } ] ,
755- token : "xoxb-test-token" ,
756- maxBytes : 1024 * 1024 ,
757- } ) ;
752+ it . each ( [ "text/html; charset=utf-8" , "image/jpeg" ] ) (
753+ "records blocked HTML auth pages for non-HTML files served as %s without retaining bytes" ,
754+ async ( contentType ) => {
755+ const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "slack-blocked-media-" ) ) ;
756+ const savedPath = path . join ( dir , "test.jpg" ) ;
757+ await fs . writeFile ( savedPath , "<!DOCTYPE html><html><body>login</body></html>" ) ;
758+ saveRemoteMediaMock . mockResolvedValueOnce ( {
759+ ...createSavedMedia ( savedPath , contentType ) ,
760+ fileName : "test.jpg" ,
761+ } ) ;
762+ const file = { url_private : "https://files.slack.com/test.jpg" , name : "test.jpg" } ;
763+ try {
764+ const result = await resolveSlackAttachmentContent ( {
765+ files : [ file ] ,
766+ token : "xoxb-test-token" ,
767+ maxBytes : 1024 * 1024 ,
768+ } ) ;
758769
759- expect ( result ) . toBeNull ( ) ;
760- expect ( saveRemoteMediaMock ) . toHaveBeenCalledTimes ( 1 ) ;
761- } ) ;
770+ expect ( result ?. media ) . toEqual ( [ ] ) ;
771+ await expect ( fs . stat ( savedPath ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
772+ expect ( result ?. files ) . toEqual ( [ { ...file , reason : "blocked: unexpected HTML content" } ] ) ;
773+ expect ( result ) . toMatchObject ( { unavailableMediaCount : 1 } ) ;
774+ expect ( mediaWarnMock ) . toHaveBeenCalledExactlyOnceWith (
775+ expect . stringContaining ( "blocked: unexpected HTML content" ) ,
776+ ) ;
777+ } finally {
778+ await fs . rm ( dir , { recursive : true , force : true } ) ;
779+ }
780+ } ,
781+ ) ;
762782
763783 it ( "allows expected HTML uploads" , async ( ) => {
764784 saveMediaBufferMock . mockResolvedValue ( createSavedMedia ( "/tmp/page.html" , "text/html" ) ) ;
@@ -961,8 +981,10 @@ describe("resolveSlackMedia", () => {
961981 mimetype : "image/jpeg" ,
962982 } ) ) ;
963983
984+ const unavailableFiles = new Map < SlackFile , string > ( ) ;
964985 const result = await resolveSlackMedia ( {
965986 files,
987+ unavailableFiles,
966988 token : "xoxb-test-token" ,
967989 maxBytes : 1024 * 1024 ,
968990 } ) ;
@@ -971,6 +993,7 @@ describe("resolveSlackMedia", () => {
971993 expect ( media ) . toHaveLength ( 8 ) ;
972994 expect ( saveMediaBufferMock ) . toHaveBeenCalledTimes ( 8 ) ;
973995 expect ( mockFetch ) . toHaveBeenCalledTimes ( 8 ) ;
996+ expect ( [ ...unavailableFiles ] ) . toEqual ( [ [ files [ 8 ] , "omitted: 8-file limit" ] ] ) ;
974997 } ) ;
975998
976999 it ( "routes dispatcher-backed Slack media requests through runtime fetch" , async ( ) => {
@@ -1343,17 +1366,25 @@ describe("Slack message file intake", () => {
13431366 expect ( result ?. rawBody ) . toContain ( "missing.png (image/png)" ) ;
13441367 } ) ;
13451368
1346- it ( "applies Slack's eight-file budget once across direct and forwarded sources" , async ( ) => {
1347- const result = await resolveMessageFiles ( {
1348- direct : Array . from ( { length : 8 } , ( _ , index ) => file ( `FDIRECT${ index } ` ) ) ,
1349- forwarded : [ Array . from ( { length : 8 } , ( _ , index ) => file ( `FFORWARDED${ index } ` ) ) ] ,
1350- } ) ;
1369+ it . each ( [ 1 , 8 ] ) (
1370+ "announces %i omitted files beyond the shared eight-file budget" ,
1371+ async ( omitted ) => {
1372+ const result = await resolveMessageFiles ( {
1373+ direct : Array . from ( { length : 8 } , ( _ , index ) => file ( `FDIRECT${ index } ` ) ) ,
1374+ forwarded : [ Array . from ( { length : omitted } , ( _ , index ) => file ( `FFORWARDED${ index } ` ) ) ] ,
1375+ } ) ;
13511376
1352- expect ( mockFetch ) . toHaveBeenCalledTimes ( 8 ) ;
1353- expect ( result ?. effectiveDirectMedia ) . toHaveLength ( 8 ) ;
1354- expect ( result ?. rawBody ) . toContain ( "FDIRECT0.png" ) ;
1355- expect ( result ?. rawBody ) . not . toContain ( "FFORWARDED0.png" ) ;
1356- } ) ;
1377+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 8 ) ;
1378+ expect ( result ?. effectiveDirectMedia ) . toHaveLength ( 8 ) ;
1379+ expect ( result ?. rawBody ) . toContain ( "FDIRECT0.png" ) ;
1380+ for ( let index = 0 ; index < omitted ; index ++ ) {
1381+ expect ( result ?. rawBody ) . toContain (
1382+ `FFORWARDED${ index } .png (image/png, fileId: FFORWARDED${ index } ) unavailable (omitted: 8-file limit)` ,
1383+ ) ;
1384+ }
1385+ expect ( mediaWarnMock ) . not . toHaveBeenCalled ( ) ;
1386+ } ,
1387+ ) ;
13571388
13581389 it ( "keeps forwarded images before their files without letting failures shift later attachments" , async ( ) => {
13591390 mockFetch . mockImplementation ( async ( input ) => {
@@ -1395,6 +1426,21 @@ describe("Slack message file intake", () => {
13951426 expect ( result ?. rawBody ) . toContain ( "FFAILED.png (image/png, fileId: FFAILED)" ) ;
13961427 } ) ;
13971428
1429+ it ( "bounds omission text while retaining the total unavailable count" , async ( ) => {
1430+ const result = await resolveMessageFiles ( {
1431+ direct : Array . from ( { length : 48 } , ( _ , index ) => file ( `FFILE${ index } ` ) ) ,
1432+ } ) ;
1433+
1434+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 8 ) ;
1435+ expect ( result ?. effectiveDirectMedia ) . toHaveLength ( 8 ) ;
1436+ expect ( result ?. rawBody ) . toContain (
1437+ "FFILE8.png (image/png, fileId: FFILE8) unavailable (omitted: 8-file limit)" ,
1438+ ) ;
1439+ expect ( result ?. rawBody ) . toContain ( "… (file references truncated)" ) ;
1440+ expect ( result ?. rawBody ) . toContain ( "[slack 40 attachments unavailable]" ) ;
1441+ expect ( expectDefined ( result , "Slack message content" ) . rawBody . length ) . toBeLessThan ( 2600 ) ;
1442+ } ) ;
1443+
13981444 it ( "preserves distinct files without Slack file identifiers" , async ( ) => {
13991445 const first = { ...file ( "FIRST" ) , id : undefined } ;
14001446 const second = { ...file ( "SECOND" ) , id : undefined } ;
@@ -1415,7 +1461,9 @@ describe("Slack message file intake", () => {
14151461
14161462 expect ( mockFetch ) . not . toHaveBeenCalled ( ) ;
14171463 expect ( result ?. effectiveDirectMedia ) . toBeNull ( ) ;
1418- expect ( result ?. rawBody ) . toBe ( "Attached files\n[Slack file: file (fileId: FUNTRUSTED)]" ) ;
1464+ expect ( result ?. rawBody ) . toBe (
1465+ "Attached files\n[Slack file: file (fileId: FUNTRUSTED) unavailable (no private download URL)]\n\n[slack attachment unavailable]" ,
1466+ ) ;
14191467 } ) ;
14201468
14211469 it ( "ignores richer metadata beyond the existing forwarded-attachment trust limit" , async ( ) => {
@@ -1429,7 +1477,9 @@ describe("Slack message file intake", () => {
14291477
14301478 expect ( mockFetch ) . not . toHaveBeenCalled ( ) ;
14311479 expect ( result ?. effectiveDirectMedia ) . toBeNull ( ) ;
1432- expect ( result ?. rawBody ) . toBe ( "Attached files\n[Slack file: file (fileId: FLATE)]" ) ;
1480+ expect ( result ?. rawBody ) . toBe (
1481+ "Attached files\n[Slack file: file (fileId: FLATE) unavailable (no private download URL)]\n\n[slack attachment unavailable]" ,
1482+ ) ;
14331483 } ) ;
14341484} ) ;
14351485
@@ -1444,6 +1494,41 @@ describe("resolveSlackAttachmentContent", () => {
14441494 vi . restoreAllMocks ( ) ;
14451495 } ) ;
14461496
1497+ it . each ( [ "direct" , "forwarded" ] ) (
1498+ "records one bounded reason and warning for a failed %s file after URL refresh" ,
1499+ async ( source ) => {
1500+ const file = {
1501+ id : "FFAILED" ,
1502+ name : "missing.png" ,
1503+ url_private : "https://files.slack.com/stale.png" ,
1504+ } ;
1505+ const client = {
1506+ files : {
1507+ info : vi . fn ( async ( ) => ( { file : { url_private : "https://files.slack.com/fresh.png" } } ) ) ,
1508+ } ,
1509+ } as unknown as WebClient ;
1510+ mockFetch . mockRejectedValueOnce ( new Error ( "stale URL" ) ) ;
1511+ mockFetch . mockRejectedValueOnce ( new Error ( `Download denied\n${ "detail " . repeat ( 100 ) } ` ) ) ;
1512+ const result = await resolveSlackAttachmentContent ( {
1513+ ...( source === "direct"
1514+ ? { files : [ file ] }
1515+ : { attachments : [ { is_share : true , files : [ file ] } ] } ) ,
1516+ client,
1517+ token : "xoxb-test-token" ,
1518+ maxBytes : 1024 ,
1519+ } ) ;
1520+
1521+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
1522+ expect ( result ?. media ) . toEqual ( [ ] ) ;
1523+ const reason = `Download denied ${ "detail " . repeat ( 100 ) } ` . slice ( 0 , 200 ) ;
1524+ expect . soft ( result ?. files ) . toEqual ( [ { ...file , reason } ] ) ;
1525+ expect . soft ( result ) . toMatchObject ( { unavailableMediaCount : 1 } ) ;
1526+ expect ( mediaWarnMock ) . toHaveBeenCalledExactlyOnceWith (
1527+ `slack: file missing.png (fileId: FFAILED) unavailable (${ reason } )` ,
1528+ ) ;
1529+ } ,
1530+ ) ;
1531+
14471532 it ( "ignores non-forwarded attachments" , async ( ) => {
14481533 const result = await resolveSlackAttachmentContent ( {
14491534 attachments : [
@@ -1477,7 +1562,7 @@ describe("resolveSlackAttachmentContent", () => {
14771562 expect ( result ) . toEqual ( {
14781563 text : "[Forwarded message from Bob]\nPlease review this" ,
14791564 media : [ ] ,
1480- unavailableImageCount : 0 ,
1565+ unavailableMediaCount : 0 ,
14811566 } ) ;
14821567 expect ( mockFetch ) . not . toHaveBeenCalled ( ) ;
14831568 } ) ;
@@ -1490,10 +1575,35 @@ describe("resolveSlackAttachmentContent", () => {
14901575 maxBytes : 1024 * 1024 ,
14911576 } ) ;
14921577
1493- expect ( result ) . toEqual ( { text : "" , media : [ ] , unavailableImageCount : 0 , files : [ file ] } ) ;
1578+ expect ( result ) . toEqual ( {
1579+ text : "" ,
1580+ media : [ ] ,
1581+ unavailableMediaCount : 1 ,
1582+ files : [ { ...file , reason : "no private download URL" } ] ,
1583+ } ) ;
14941584 expect ( mockFetch ) . not . toHaveBeenCalled ( ) ;
14951585 } ) ;
14961586
1587+ it ( "redacts download credentials before exposing a failure reason or warning" , async ( ) => {
1588+ mockFetch . mockRejectedValueOnce (
1589+ new Error (
1590+ "Download failed at https://files.slack.com/file.png?token=synthetic-private-query-value" ,
1591+ ) ,
1592+ ) ;
1593+ const result = await resolveSlackAttachmentContent ( {
1594+ files : [ { name : "file.png" , url_private : "https://files.slack.com/file.png" } ] ,
1595+ token : "xoxb-test-token" ,
1596+ maxBytes : 1024 ,
1597+ } ) ;
1598+
1599+ expect ( result ?. files ?. [ 0 ] ) . toMatchObject ( {
1600+ reason : expect . stringContaining ( "Download failed" ) ,
1601+ } ) ;
1602+ expect ( JSON . stringify ( result ) ) . not . toContain ( "synthetic-private-query-value" ) ;
1603+ expect ( mediaWarnMock ) . toHaveBeenCalledOnce ( ) ;
1604+ expect ( mediaWarnMock . mock . calls [ 0 ] ?. [ 0 ] ) . not . toContain ( "synthetic-private-query-value" ) ;
1605+ } ) ;
1606+
14971607 it ( "skips forwarded image URLs on non-Slack hosts" , async ( ) => {
14981608 const result = await resolveSlackAttachmentContent ( {
14991609 attachments : [ { is_share : true , image_url : "https://example.com/forwarded.jpg" } ] ,
@@ -1532,7 +1642,7 @@ describe("resolveSlackAttachmentContent", () => {
15321642 placeholder : "[Forwarded image: forwarded.jpg]" ,
15331643 } ,
15341644 ] ,
1535- unavailableImageCount : 0 ,
1645+ unavailableMediaCount : 0 ,
15361646 } ) ;
15371647 const firstCall = requireMockCall ( mockFetch , 0 , "fetch" ) ;
15381648 expect ( firstCall [ 0 ] ) . toBe ( "https://files.slack.com/forwarded.jpg" ) ;
@@ -1553,7 +1663,7 @@ describe("resolveSlackAttachmentContent", () => {
15531663 expect ( result ) . toEqual ( {
15541664 text : "" ,
15551665 media : [ ] ,
1556- unavailableImageCount : 1 ,
1666+ unavailableMediaCount : 1 ,
15571667 } ) ;
15581668 expect ( saveMediaBufferMock ) . not . toHaveBeenCalled ( ) ;
15591669 expect ( mockFetch ) . toHaveBeenCalledOnce ( ) ;
0 commit comments