import { IllusionEngine } from '../illusion-engine'; import { IllusionCore } from '../core/illusion-core'; import { installIllusionEngine } from '../index'; import { FileSystem } from './file-system'; import { MessageType } from '../core/message-type'; const fs = require('fs'); const path = require('path'); jest.mock('../wasm/illusion.js') jest.mock('../core/illusion-core'); jest.mock('../index'); describe('model/fileSystem', () => { let engine: IllusionEngine; let fileSystem: FileSystem; let illusionCore: any; beforeAll(async() => { engine = await installIllusionEngine(); // @ts-ignore illusionCore = engine.servicer.illusionCore; }); beforeEach(async() => { fileSystem = engine.fileSystem; jest.clearAllMocks(); }); it('读取文件', async() => { await fileSystem.readFile('/testpath/file.jpg', { encoding: 'utf8' }); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.readFile, ['/testpath/file.jpg', { encoding: 'utf8' }]]); }); it('写文件', async() => { await fileSystem.writeFile('/testpath/file.test', 'test content', { flags: 'w+' }); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.writeFile, ['/testpath/file.test', 'test content', { flags: 'w+' }]]); }); it('创建目录', async() => { await fileSystem.mkdir('/testpath'); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.mkdir, ['/testpath']]); }); it('移除文件', async() => { await fileSystem.removeFile('/testpath/file.jpg'); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.remove, ['/testpath/file.jpg']]); }); it('加密文件', async() => { const src = 'a'; const dst = 'b'; await fileSystem.encryptFile(src, dst); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.encryptFile, [src, dst]]); }); it('加密文本', async() => { const content = 'encrypt'; const result = await fileSystem.encryptContent(content); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.encryptContent, [content]]); // @ts-ignore const actualResult = await illusionCore.do.mock.results[0].value; expect(result).toEqual(actualResult); }); it('加密内容', async() => { const targetContent = ['a', 'b', 'c']; const buffer = targetContent.map(char => char.charCodeAt(0)); const result = await fileSystem.encryptBuffer(buffer); expect(illusionCore.do).toHaveBeenCalledTimes(1); expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.encryptContent, [targetContent.join('')]]); const actualResult = await illusionCore.do.mock.results[0].value; expect(result).toEqual(actualResult); }); it('解密数据', async() => { const content = [100, 100]; const result = await fileSystem.decryptBuffer(content); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.decryptBuffer, [content]]); // @ts-ignore const actualResult = await illusionCore.do.mock.results[0].value; expect(result).toEqual(actualResult); }); it('解密文本', async() => { const codes = [45, 46, 47]; const string = codes.reduce((s, value) => { return s + String.fromCharCode(value); }, ''); const result = await fileSystem.decryptContent(string); expect(illusionCore.do).toHaveBeenCalledTimes(1); // @ts-ignore expect(illusionCore.do.mock.calls[0]).toEqual([MessageType.file.decryptBuffer, [codes]]); // @ts-ignore const actualResult = await illusionCore.do.mock.results[0].value; expect(result).toEqual(actualResult); }); it('加载 zip 配置包', async() => { const zipFile = fs.readFileSync(path.resolve(__dirname, '../../tests/resources/filters.zip')); const { config, files } = await fileSystem.loadZip(zipFile, undefined, false); expect(config).toBeDefined(); expect(JSON.parse(config)).toBeDefined(); expect(files.length).toBe(1); files.forEach(file => { expect(file.name).not.toBe('config.json'); }); }); it('加载 zip 配置包并解密', async() => { const zipFile = fs.readFileSync(path.resolve(__dirname, '../../tests/resources/filters.zip')); const { config } = await fileSystem.loadZip(zipFile, undefined, true); expect(illusionCore.do).toBeCalledTimes(1); expect(illusionCore.do.mock.calls[0][0]).toEqual(MessageType.file.decryptBuffer); const actualResult = await illusionCore.do.mock.results[0].value; expect(config).toBe(actualResult) }); it('zip 包中没有 config.json 时抛出异常',async() => { const zipFile = fs.readFileSync(path.resolve(__dirname, '../../tests/resources/invalid_filters.zip')); try { await fileSystem.loadZip(zipFile); throw new Error('不能执行到这里'); } catch(error) { expect(error.message).toBe('config file is not exist'); } }); it('连接路径', () => { const expectResult = '/path1/path2'; const result1 = fileSystem.joinPath(['path1', 'path2']); expect(result1).toEqual(expectResult); const result2 = fileSystem.joinPath(['path1/', 'path2']); expect(result2).toEqual(expectResult); const result3 = fileSystem.joinPath(['path1', '/path2']); expect(result3).toEqual(expectResult); const result4 = fileSystem.joinPath(['path1/', '/path2']); expect(result4).toEqual(expectResult); const paths = ['path1', 'path2', 'path3', 'path4', 'path5']; const result5 = fileSystem.joinPath(paths); expect(result5).toEqual('/' + paths.join('/')); }); it('写入 shader', async() => { const shader = 'shader content'; const name = 'shader.fs'; const version = '0.1.1'; // @ts-ignore const { workResource } = fileSystem.options; await fileSystem.writeShader(shader, name); expect(illusionCore.do).toHaveBeenCalledTimes(1); expect(illusionCore.do.mock.calls[0][0]).toEqual(MessageType.file.writeFile); expect(illusionCore.do.mock.calls[0][1][0]).toEqual(`${workResource}/${name}`); expect(illusionCore.do.mock.calls[0][1][1]).toEqual(shader); await fileSystem.writeShader(shader, name, version); expect(illusionCore.do).toHaveBeenCalledTimes(2); expect(illusionCore.do.mock.calls[1][0]).toEqual(MessageType.file.writeFile); expect(illusionCore.do.mock.calls[1][1][0]).toEqual(`${workResource}/${version}_${name}`); expect(illusionCore.do.mock.calls[1][1][1]).toEqual(shader); }); });