11import assert from 'node:assert/strict' ;
22import fs from 'node:fs' ;
3- import http from 'node:http' ;
43import path from 'node:path' ;
5- import { gzipSync } from 'node:zlib' ;
64import { test } from 'vitest' ;
7- import * as tar from 'tar-stream' ;
85import { AppError } from '@agent-device/kernel/errors' ;
96import { downloadRemoteArtifact } from '../daemon-artifacts.ts' ;
7+ import {
8+ createArtifactDownloadServer ,
9+ createDirectoryArtifactArchive ,
10+ } from './support/artifact-download-fixtures.ts' ;
1011import {
1112 closeLoopbackServer ,
1213 listenOnLoopback ,
@@ -16,49 +17,6 @@ import {
1617
1718const SUITE_INVOCATION_ID = 'cd5f9c01feec8d70' ;
1819
19- async function createDirectoryArtifactArchive ( directoryPath : string ) : Promise < Buffer > {
20- const pack = tar . pack ( ) ;
21- const rootName = path . basename ( directoryPath ) ;
22- const visit = async ( currentPath : string , archivePath : string ) : Promise < void > => {
23- for ( const entry of await fs . promises . readdir ( currentPath , { withFileTypes : true } ) ) {
24- const sourcePath = path . join ( currentPath , entry . name ) ;
25- const name = `${ archivePath } /${ entry . name } ` ;
26- if ( entry . isDirectory ( ) ) {
27- await visit ( sourcePath , name ) ;
28- } else if ( entry . isSymbolicLink ( ) ) {
29- pack . entry (
30- { name, type : 'symlink' , linkname : await fs . promises . readlink ( sourcePath ) } ,
31- Buffer . alloc ( 0 ) ,
32- ) ;
33- } else if ( entry . isFile ( ) ) {
34- pack . entry ( { name, type : 'file' } , await fs . promises . readFile ( sourcePath ) ) ;
35- }
36- }
37- } ;
38- await visit ( directoryPath , rootName ) ;
39- pack . finalize ( ) ;
40- const chunks : Buffer [ ] = [ ] ;
41- for await ( const chunk of pack ) chunks . push ( Buffer . from ( chunk ) ) ;
42- return gzipSync ( Buffer . concat ( chunks ) ) ;
43- }
44-
45- function createArtifactDownloadServer ( archives : ReadonlyMap < string , Buffer > ) : http . Server {
46- return http . createServer ( ( request , response ) => {
47- const prefix = '/artifacts/' ;
48- const artifactId = request . url ?. startsWith ( prefix )
49- ? decodeURIComponent ( request . url . slice ( prefix . length ) )
50- : undefined ;
51- const archive = request . method === 'GET' && artifactId ? archives . get ( artifactId ) : undefined ;
52- if ( ! archive ) {
53- response . statusCode = 404 ;
54- response . end ( 'not found' ) ;
55- return ;
56- }
57- response . setHeader ( 'content-type' , 'application/gzip' ) ;
58- response . end ( archive ) ;
59- } ) ;
60- }
61-
6220test ( 'downloadRemoteArtifact extracts a directory artifact into the caller-local artifacts root' , async ( t ) => {
6321 if ( await skipWhenLoopbackUnavailable ( t , 'remote test-artifacts directory download coverage' ) )
6422 return ;
0 commit comments