|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as zlib from "zlib"; |
| 5 | +import { Uploader } from "./uploader"; |
| 6 | +import { Client } from "../../apiv2"; |
| 7 | +import * as hashcache from "./hashcache"; |
| 8 | + |
| 9 | +describe("deploy/hosting/uploader", () => { |
| 10 | + let clientPostStub: sinon.SinonStub; |
| 11 | + let clientRequestStub: sinon.SinonStub; |
| 12 | + |
| 13 | + class MockQueue { |
| 14 | + public handler: any; |
| 15 | + private promises: Promise<void>[] = []; |
| 16 | + constructor(options: any) { |
| 17 | + this.handler = options.handler; |
| 18 | + } |
| 19 | + add(item: any) { |
| 20 | + const p = Promise.resolve(this.handler(item)); |
| 21 | + this.promises.push(p); |
| 22 | + } |
| 23 | + process() { } |
| 24 | + async wait() { |
| 25 | + await Promise.all(this.promises); |
| 26 | + return Promise.resolve(); |
| 27 | + } |
| 28 | + close() { } |
| 29 | + stats() { |
| 30 | + return { total: 0, complete: 0, cursor: 0 }; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + sinon.stub(fs, "statSync"); |
| 36 | + sinon.stub(fs, "createReadStream"); |
| 37 | + sinon.stub(zlib, "createGzip"); |
| 38 | + clientPostStub = sinon.stub(Client.prototype, "post"); |
| 39 | + clientRequestStub = sinon.stub(Client.prototype, "request"); |
| 40 | + sinon.stub(hashcache, "load").returns(new Map()); |
| 41 | + sinon.stub(hashcache, "dump"); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + sinon.restore(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should initialize correctly", () => { |
| 49 | + const uploader = new Uploader({ |
| 50 | + version: "v1", |
| 51 | + projectRoot: "root", |
| 52 | + files: ["file1.txt"], |
| 53 | + public: "public", |
| 54 | + }); |
| 55 | + expect(uploader).to.be.instanceOf(Uploader); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should hash files and populate version", async () => { |
| 59 | + const uploader = new Uploader({ |
| 60 | + version: "v1", |
| 61 | + projectRoot: "root", |
| 62 | + files: ["file1.txt", "file2.txt"], |
| 63 | + public: "public", |
| 64 | + }); |
| 65 | + (uploader as any).hashQueue = new MockQueue({ handler: (uploader as any).hashHandler.bind(uploader) }); |
| 66 | + (uploader as any).populateQueue = new MockQueue({ handler: (uploader as any).populateHandler.bind(uploader) }); |
| 67 | + (uploader as any).uploadQueue = new MockQueue({ handler: (uploader as any).uploadHandler.bind(uploader) }); |
| 68 | + |
| 69 | + (fs.statSync as sinon.SinonStub).returns({ mtime: new Date(), size: 100 }); |
| 70 | + const { PassThrough, Readable } = require("stream"); |
| 71 | + |
| 72 | + // Mock stream for file1.txt |
| 73 | + const mockStream1 = new Readable({ |
| 74 | + read() { |
| 75 | + this.push(Buffer.from("hash1")); |
| 76 | + this.push(null); |
| 77 | + }, |
| 78 | + }); |
| 79 | + // Mock stream for file2.txt |
| 80 | + const mockStream2 = new Readable({ |
| 81 | + read() { |
| 82 | + this.push(Buffer.from("hash2")); |
| 83 | + this.push(null); |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + (zlib.createGzip as sinon.SinonStub).callsFake(() => new PassThrough()); |
| 88 | + (fs.createReadStream as sinon.SinonStub).callsFake((filePath: string) => { |
| 89 | + if (filePath.includes("file1.txt")) { |
| 90 | + return new Readable({ |
| 91 | + read() { |
| 92 | + this.push(Buffer.from("hash1")); |
| 93 | + this.push(null); |
| 94 | + }, |
| 95 | + }); |
| 96 | + } |
| 97 | + if (filePath.includes("file2.txt")) { |
| 98 | + return new Readable({ |
| 99 | + read() { |
| 100 | + this.push(Buffer.from("hash2")); |
| 101 | + this.push(null); |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + return new PassThrough(); |
| 106 | + }); |
| 107 | + |
| 108 | + clientPostStub.resolves({ |
| 109 | + body: { |
| 110 | + uploadUrl: "https://upload.url", |
| 111 | + uploadRequiredHashes: [ |
| 112 | + "af316ecb91a8ee7ae99210702b2d4758f30cdde3bf61e3d8e787d74681f90a6e", // hash for "hash1" |
| 113 | + "e7bf382f6e5915b3f88619b866223ebf1d51c4c5321cccde2e9ff700a3259086" // hash for "hash2" |
| 114 | + ] |
| 115 | + }, |
| 116 | + }); |
| 117 | + clientRequestStub.resolves({ status: 200, response: { text: sinon.stub().resolves("") } }); |
| 118 | + |
| 119 | + await uploader.start(); |
| 120 | + |
| 121 | + expect(clientPostStub.calledWithMatch(/\/v1:populateFiles/)).to.be.true; |
| 122 | + expect(clientPostStub.firstCall.args[1].files).to.have.property("/file1.txt"); |
| 123 | + expect(clientPostStub.firstCall.args[1].files).to.have.property("/file2.txt"); |
| 124 | + expect(clientRequestStub.calledTwice).to.be.true; |
| 125 | + }); |
| 126 | +}); |
0 commit comments