|
| 1 | +import * as sinon from 'sinon' |
| 2 | +import * as fs from 'fs' |
| 3 | +import { expect } from '@oclif/test' |
| 4 | +import rewriteAssets from './rewriteAssets' |
| 5 | + |
| 6 | +describe('rewriteAssets', () => { |
| 7 | + beforeEach(() => { |
| 8 | + sinon.restore() |
| 9 | + }) |
| 10 | + |
| 11 | + it('decodes base64 content and writes assets to the correct file paths', () => { |
| 12 | + const mkdirSyncStub = sinon.stub(fs, 'mkdirSync') |
| 13 | + const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') |
| 14 | + |
| 15 | + const jsContent = Buffer.from('console.log("hello")').toString('base64') |
| 16 | + |
| 17 | + rewriteAssets('theme/path', { |
| 18 | + 'script.js': jsContent |
| 19 | + }) |
| 20 | + |
| 21 | + expect(mkdirSyncStub.calledOnce).to.equal(true) |
| 22 | + expect(mkdirSyncStub.firstCall.args[1]).to.deep.equal({ recursive: true }) |
| 23 | + |
| 24 | + expect(writeFileSyncStub.callCount).to.equal(1) |
| 25 | + expect(writeFileSyncStub.firstCall.args[0]).to.match(/theme\/path\/assets\/script\.js$/) |
| 26 | + expect(Buffer.compare( |
| 27 | + writeFileSyncStub.firstCall.args[1] as Buffer, |
| 28 | + Buffer.from('console.log("hello")') |
| 29 | + )).to.equal(0) |
| 30 | + }) |
| 31 | + |
| 32 | + it('handles multiple file types', () => { |
| 33 | + sinon.stub(fs, 'mkdirSync') |
| 34 | + const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') |
| 35 | + |
| 36 | + const jsContent = Buffer.from('var a = 1;').toString('base64') |
| 37 | + const pngContent = Buffer.from([0x89, 0x50, 0x4e, 0x47]).toString('base64') |
| 38 | + |
| 39 | + rewriteAssets('theme/path', { |
| 40 | + 'app.js': jsContent, |
| 41 | + 'logo.png': pngContent |
| 42 | + }) |
| 43 | + |
| 44 | + expect(writeFileSyncStub.callCount).to.equal(2) |
| 45 | + expect(writeFileSyncStub.firstCall.args[0]).to.match(/theme\/path\/assets\/app\.js$/) |
| 46 | + expect(writeFileSyncStub.secondCall.args[0]).to.match(/theme\/path\/assets\/logo\.png$/) |
| 47 | + |
| 48 | + expect(Buffer.compare( |
| 49 | + writeFileSyncStub.secondCall.args[1] as Buffer, |
| 50 | + Buffer.from([0x89, 0x50, 0x4e, 0x47]) |
| 51 | + )).to.equal(0) |
| 52 | + }) |
| 53 | + |
| 54 | + it('handles empty assets object', () => { |
| 55 | + sinon.stub(fs, 'mkdirSync') |
| 56 | + const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') |
| 57 | + |
| 58 | + rewriteAssets('theme/path', {}) |
| 59 | + |
| 60 | + expect(writeFileSyncStub.callCount).to.equal(0) |
| 61 | + }) |
| 62 | + |
| 63 | + it('ignores write errors', () => { |
| 64 | + sinon.stub(fs, 'mkdirSync') |
| 65 | + const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') |
| 66 | + |
| 67 | + writeFileSyncStub.onFirstCall().throws(new Error('Permission denied')) |
| 68 | + writeFileSyncStub.onSecondCall().returns(undefined) |
| 69 | + |
| 70 | + const jsContent = Buffer.from('first').toString('base64') |
| 71 | + const jsContent2 = Buffer.from('second').toString('base64') |
| 72 | + |
| 73 | + expect(() => { |
| 74 | + rewriteAssets('theme/path', { |
| 75 | + 'a.js': jsContent, |
| 76 | + 'b.js': jsContent2 |
| 77 | + }) |
| 78 | + }).to.not.throw() |
| 79 | + |
| 80 | + expect(writeFileSyncStub.callCount).to.equal(2) |
| 81 | + }) |
| 82 | + |
| 83 | + it('ignores mkdir errors', () => { |
| 84 | + const mkdirSyncStub = sinon.stub(fs, 'mkdirSync') |
| 85 | + const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') |
| 86 | + |
| 87 | + mkdirSyncStub.throws(new Error('Permission denied')) |
| 88 | + |
| 89 | + const jsContent = Buffer.from('hello').toString('base64') |
| 90 | + |
| 91 | + expect(() => { |
| 92 | + rewriteAssets('theme/path', { 'a.js': jsContent }) |
| 93 | + }).to.not.throw() |
| 94 | + |
| 95 | + expect(writeFileSyncStub.callCount).to.equal(1) |
| 96 | + }) |
| 97 | +}) |
0 commit comments