11import fs from "node:fs/promises" ;
22import path from "node:path" ;
3- import { describe , expect , test } from "vitest" ;
3+ import { describe , expect , test , vi } from "vitest" ;
44import { readImageProbeFromHeader } from "../media/image-ops.js" ;
55import {
6+ cleanupManagedOutgoingMediaRecords ,
67 createManagedOutgoingMediaBlocks ,
78 MANAGED_OUTGOING_IMAGE_ARTIFACT_ID_PREFIX ,
89} from "./managed-image-attachments.js" ;
10+ import { readManagedImageRecord } from "./managed-image-record-store.js" ;
911import { connectGatewayClient , disconnectGatewayClient } from "./test-helpers.e2e.js" ;
1012import {
1113 installGatewayTestHooks ,
@@ -114,6 +116,23 @@ describe("managed image actions Gateway E2E", () => {
114116 expect ( rootFull . headers . get ( "content-type" ) ) . toBe ( "image/png" ) ;
115117 expect ( Buffer . from ( await rootFull . arrayBuffer ( ) ) ) . toEqual ( source ) ;
116118
119+ const partial = await fetch ( fullUrl , { headers : { Range : "bytes=3-10" } } ) ;
120+ expect ( partial . status ) . toBe ( 206 ) ;
121+ expect ( partial . headers . get ( "content-range" ) ) . toBe ( `bytes 3-10/${ source . byteLength } ` ) ;
122+ expect ( Buffer . from ( await partial . arrayBuffer ( ) ) ) . toEqual ( source . subarray ( 3 , 11 ) ) ;
123+
124+ const head = await fetch ( fullUrl , { method : "HEAD" } ) ;
125+ expect ( head . status ) . toBe ( 200 ) ;
126+ expect ( head . headers . get ( "content-length" ) ) . toBe ( String ( source . byteLength ) ) ;
127+ expect ( ( await head . arrayBuffer ( ) ) . byteLength ) . toBe ( 0 ) ;
128+
129+ const unsatisfiable = await fetch ( fullUrl , {
130+ headers : { Range : `bytes=${ source . byteLength } -` } ,
131+ } ) ;
132+ expect ( unsatisfiable . status ) . toBe ( 416 ) ;
133+ expect ( unsatisfiable . headers . get ( "content-range" ) ) . toBe ( `bytes */${ source . byteLength } ` ) ;
134+ expect ( ( await unsatisfiable . arrayBuffer ( ) ) . byteLength ) . toBe ( 0 ) ;
135+
117136 fullUrl . pathname = `/rosita${ fullUrl . pathname } ` ;
118137
119138 const full = await fetch ( fullUrl ) ;
@@ -137,8 +156,59 @@ describe("managed image actions Gateway E2E", () => {
137156 } ) ;
138157 expect ( authenticated . status ) . toBe ( 200 ) ;
139158 expect ( Buffer . from ( await authenticated . arrayBuffer ( ) ) ) . toEqual ( source ) ;
140- } finally {
159+
160+ const wrongIdentity = new URL ( fullUrl ) ;
161+ wrongIdentity . pathname = wrongIdentity . pathname . replace (
162+ / \/ [ 0 - 9 a - f - ] + \/ f u l l $ / u,
163+ "/22222222-2222-4222-8222-222222222222/full" ,
164+ ) ;
165+ const wrong = await fetch ( wrongIdentity , {
166+ headers : { Authorization : `Bearer ${ GATEWAY_TOKEN } ` } ,
167+ } ) ;
168+ expect ( wrong . status ) . toBe ( 404 ) ;
169+ expect ( await wrong . text ( ) ) . toBe ( "not found" ) ;
170+
171+ vi . useFakeTimers ( { toFake : [ "Date" ] } ) ;
172+ vi . setSystemTime ( Date . parse ( download . expiresAt ?? "" ) + 1 ) ;
173+ try {
174+ const expired = await fetch ( fullUrl ) ;
175+ expect ( expired . status ) . toBe ( 401 ) ;
176+ expect ( await expired . text ( ) ) . toContain ( "unauthorized" ) ;
177+ } finally {
178+ vi . useRealTimers ( ) ;
179+ }
180+
141181 await disconnectGatewayClient ( client ) ;
182+ const afterDisconnect = await fetch ( fullUrl ) ;
183+ expect ( afterDisconnect . status ) . toBe ( 200 ) ;
184+ expect ( Buffer . from ( await afterDisconnect . arrayBuffer ( ) ) ) . toEqual ( source ) ;
185+
186+ const record = readManagedImageRecord (
187+ artifactId . slice ( MANAGED_OUTGOING_IMAGE_ARTIFACT_ID_PREFIX . length ) ,
188+ stateDir ,
189+ ) ;
190+ if ( ! record ) {
191+ throw new Error ( "managed image record disappeared before cleanup" ) ;
192+ }
193+ const originalPath = path . join (
194+ record . original . mediaRoot ,
195+ record . original . mediaSubdir ,
196+ record . original . mediaId ,
197+ ) ;
198+ const cleanup = await cleanupManagedOutgoingMediaRecords ( {
199+ stateDir,
200+ sessionKey : SESSION_KEY ,
201+ forceDeleteSessionRecords : true ,
202+ } ) ;
203+ expect ( cleanup ) . toMatchObject ( { deletedRecordCount : 1 , deletedFileCount : 1 } ) ;
204+ expect ( readManagedImageRecord ( record . attachmentId , stateDir ) ) . toBeNull ( ) ;
205+ await expect ( fs . access ( originalPath ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
206+
207+ const afterCleanup = await fetch ( fullUrl ) ;
208+ expect ( afterCleanup . status ) . toBe ( 404 ) ;
209+ expect ( await afterCleanup . text ( ) ) . toBe ( "not found" ) ;
210+ } finally {
211+ await disconnectGatewayClient ( client ) . catch ( ( ) => { } ) ;
142212 }
143213 } ,
144214 { serverOptions : { auth : { mode : "token" , token : GATEWAY_TOKEN } } } ,
0 commit comments