import * as fs from "fs";
import * as os from "os";
import Repo from "../lib/repo";
import {ensureDirSync, removeSync} from "fs-extra";

describe('add assets', () => {

    let projectDir: string;

    beforeEach(() => {
        projectDir = `${os.tmpdir()}/fun-project`;
        console.log(projectDir);
        removeSync(projectDir);
        ensureDirSync(projectDir);
    });

    test('add nest model', async () => {
        const repo = new Repo(projectDir);
        repo.init();
        await repo.add({name: 'jesse2/test/green-cube', version: '1.0.0'});
        const packages = JSON.parse(
            fs.readFileSync(`${projectDir}/modelnest.json`, 'utf8')
        );

        expect(packages.assets).toEqual({
            "jesse2/test/green-cube": "1.0.0"
        });
        expect(packages.nest).toBe('nest_models');
        const exists = fs.existsSync(`${projectDir}/nest_models`);
        expect(exists).toBeTruthy();

        const assetFiles = fs.readdirSync(`${repo.modelNestDirectory}/jesse2/test/green-cube`);
        expect(assetFiles).toContain('version.json');

        expect(assetFiles.sort()).toEqual([
            'version.json',
            'green_cube.mtl',
            'green_cube.obj',
            'torso_ZBrushTexture.001.jpg',
        ].sort());
    });

    test('update nest model', async () => {
        const repo = new Repo(projectDir);
        repo.init();
        await repo.add({name: 'jesse2/test/green-cube', version: '1.0.0'});
        const newVersions = await repo.update('jesse2/test/green-cube');

        const packages = JSON.parse(
            fs.readFileSync(`${projectDir}/modelnest.json`, 'utf8')
        );

        expect(packages.assets).toEqual({
            "jesse2/test/green-cube": "2.0.0"
        });
        expect(packages.nest).toBe('nest_models');
        const exists = fs.existsSync(`${projectDir}/nest_models`);
        expect(exists).toBeTruthy();

        const assetFiles = fs.readdirSync(`${repo.modelNestDirectory}/jesse2/test/green-cube`);
        expect(assetFiles).toContain('version.json');

        expect(assetFiles.sort()).toEqual([
            'version.json',
            'green_cube.mtl',
            'green_cube.obj',
            'torso_ZBrushTexture.001.jpg',
            'torso_ZBrushTexture.001 copy.jpg',
        ].sort());
    });

});