From 9db311c67b8fb8577ee9b50bad291b50522043f5 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Sat, 14 Oct 2023 13:23:26 +0200 Subject: [PATCH] Fix zip upload and add tests for it --- node-src/lib/compress.test.ts | 2 +- node-src/lib/compress.ts | 6 ++-- node-src/tasks/upload.test.ts | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/node-src/lib/compress.test.ts b/node-src/lib/compress.test.ts index 6ea486e0b..816da9e09 100644 --- a/node-src/lib/compress.test.ts +++ b/node-src/lib/compress.test.ts @@ -14,7 +14,7 @@ afterEach(() => { }); const testContext = { sourceDir: '/chromatic-tmp', log: new TestLogger() } as any; -const fileInfo = { paths: ['file1'] }; +const fileInfo = { paths: ['/chromatic-tmp/file1'] }; describe('makeZipFile', () => { it('adds files to an archive', async () => { diff --git a/node-src/lib/compress.ts b/node-src/lib/compress.ts index 8a1144d39..cb03485fe 100644 --- a/node-src/lib/compress.ts +++ b/node-src/lib/compress.ts @@ -1,6 +1,5 @@ import archiver from 'archiver'; import { createReadStream, createWriteStream } from 'fs'; -import { join } from 'path'; import { file as tempFile } from 'tmp-promise'; import { Context } from '../types'; @@ -25,9 +24,8 @@ export default async function makeZipFile(ctx: Context, fileInfo: { paths: strin archive.pipe(sink); paths.forEach((path) => { - const fullPath = join(ctx.sourceDir, path); - ctx.log.debug({ fullPath }, 'Adding file to zip archive'); - archive.append(createReadStream(fullPath), { name: path }); + ctx.log.debug({ path }, 'Adding file to zip archive'); + archive.append(createReadStream(path), { name: path }); }); ctx.log.debug('Finalizing zip archive'); diff --git a/node-src/tasks/upload.test.ts b/node-src/tasks/upload.test.ts index 0a6af577c..77df36978 100644 --- a/node-src/tasks/upload.test.ts +++ b/node-src/tasks/upload.test.ts @@ -2,6 +2,7 @@ import { createReadStream, readdirSync, readFileSync, statSync } from 'fs'; import progressStream from 'progress-stream'; import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { default as compress } from '../lib/compress'; import { getDependentStoryFiles as getDepStoryFiles } from '../lib/getDependentStoryFiles'; import { findChangedDependencies as findChangedDep } from '../lib/findChangedDependencies'; import { findChangedPackageFiles as findChangedPkg } from '../lib/findChangedPackageFiles'; @@ -9,11 +10,13 @@ import { validateFiles, traceChangedFiles, uploadStorybook } from './upload'; vi.mock('fs'); vi.mock('progress-stream'); +vi.mock('../lib/compress'); vi.mock('../lib/getDependentStoryFiles'); vi.mock('../lib/findChangedDependencies'); vi.mock('../lib/findChangedPackageFiles'); vi.mock('./read-stats-file'); +const makeZipFile = vi.mocked(compress); const findChangedDependencies = vi.mocked(findChangedDep); const findChangedPackageFiles = vi.mocked(findChangedPkg); const getDependentStoryFiles = vi.mocked(getDepStoryFiles); @@ -372,4 +375,59 @@ describe('uploadStorybook', () => { unit: 'bytes', }); }); + + describe('with zip', () => { + it.only('retrieves the upload location, adds the files to an archive and uploads it', async () => { + const client = { runQuery: vi.fn() }; + client.runQuery.mockReturnValue({ + getZipUploadUrl: { + domain: 'https://asdqwe.chromatic.com', + url: 'https://asdqwe.chromatic.com/storybook.zip', + sentinelUrl: 'https://asdqwe.chromatic.com/upload.txt', + }, + }); + + makeZipFile.mockReturnValue(Promise.resolve({ path: 'storybook.zip', size: 80 })); + createReadStreamMock.mockReturnValue({ pipe: vi.fn() } as any); + http.fetch.mockReturnValue({ ok: true, text: () => Promise.resolve('OK') }); + progress.mockReturnValue({ on: vi.fn() } as any); + + const fileInfo = { + lengths: [ + { knownAs: 'iframe.html', contentLength: 42 }, + { knownAs: 'index.html', contentLength: 42 }, + ], + paths: ['iframe.html', 'index.html'], + total: 84, + }; + const ctx = { + client, + env, + log, + http, + sourceDir: '/static/', + options: { zip: true }, + fileInfo, + announcedBuild: { id: '1' }, + } as any; + await uploadStorybook(ctx, {} as any); + + expect(client.runQuery).toHaveBeenCalledWith( + expect.stringMatching(/GetZipUploadUrlMutation/), + { buildId: '1' } + ); + expect(http.fetch).toHaveBeenCalledWith( + 'https://asdqwe.chromatic.com/storybook.zip', + expect.objectContaining({ + method: 'PUT', + headers: { + 'content-type': 'application/zip', + 'content-length': '80', + }, + }), + expect.objectContaining({ retries: 0 }) + ); + expect(ctx.uploadedBytes).toBe(80); + }); + }); });