Skip to content

Commit

Permalink
Add .aiignore file and update Config and Scopes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timschwartz committed Apr 9, 2024
1 parent 9654b2c commit d5422fd
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .aiignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist
package-lock.json
node_modules
.git
autom4te.cache
4 changes: 4 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Config {
}

loadConfig = () => {
// if file does not exist, create it
if (!fs.existsSync(this.configDir + this.configFile)) {
this.saveConfig();
}
this.config = JSON.parse(fs.readFileSync(this.configDir + this.configFile).toString());
};

Expand Down
14 changes: 10 additions & 4 deletions test/Config.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import fs from "fs";
import os from "os";
import path from "path";
import Config from "../src/Config";

const home = os.homedir();
const prefix = home + "/the-seed";
const configDir = "./ConfigTestDir";

function createTempDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "config-test-"));
}

describe("test Config", () => {
let configDir: string;
let config: Config;

beforeAll(() => {
fs.mkdirSync(configDir);
configDir = createTempDir();
config = new Config(configDir);
});

const config = new Config(configDir);

it("write empty config file", () => {
config.saveConfig();
});
Expand Down
69 changes: 69 additions & 0 deletions test/ResourcePak.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fs from "fs";
import os from "os";
import path from "path";
import ResourcePak from "../src/ResourcePak";
import Config from "../src/Config";

// Helper to create a temporary directory for testing
function createTempDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "resource-pak-"));
}

describe("ResourcePak", () => {
let tempDir: string;
let config: Config;
let rp: ResourcePak;

beforeEach(() => {
tempDir = createTempDir();
const defaultConfig = {
prefix: tempDir, // or any other default values your Config expects
scopes: {}
};
fs.writeFileSync(path.join(tempDir, 'config.json'), JSON.stringify(defaultConfig));
config = new Config(tempDir);
rp = new ResourcePak(config, tempDir);
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true });
});

it("should create a new package", () => {
const packageName = "@test/package";
rp.create(packageName);
expect(rp.package).toBeDefined();
expect(rp.package?.name).toBe(packageName);
// Add more assertions as necessary
});

it("should add a resource", () => {
const packageName = "@test/package";
rp.create(packageName);

const resourceName = "testResource";
const fileName = "testFile.txt";
// Creating a dummy file to add
const filePath = path.join(tempDir, fileName);
fs.writeFileSync(filePath, "Dummy content");
rp.addResource(resourceName, filePath);

expect(rp.package?.resources).toContainEqual(expect.objectContaining({
name: resourceName,
filename: tempDir + "/" + fileName,
// size: depends on the content written
}));
// Add more assertions as necessary
});

it("should save package configuration", () => {
const packageName = "@test/package";
rp.create(packageName);
rp.savePackage();
const packageJsonPath = path.join(tempDir, "package.json");
expect(fs.existsSync(packageJsonPath)).toBe(true);
// Further assertions on the contents of package.json can be added
});

// Add more tests for other methods, e.g., build(), as needed.
});
22 changes: 15 additions & 7 deletions test/Scopes.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import os from "os";
import path from "path";
import fs from "fs";
import Config from "../src/Config";
import Scopes from "../src/Scopes";

const configDir = "./ScopesTestDir";
function createTempDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "scopes-test-"));
}

const scopeName1 = "@scope-" + Math.random().toString(36).substr(2, 5);
const scopeName2 = "scope-" + Math.random().toString(36).substr(2, 5);

Expand All @@ -19,14 +24,17 @@ const author2 = {
};

describe("test Scopes", () => {
let configDir: string;
let config: Config;
let scopes: Scopes;

beforeAll(() => {
fs.mkdirSync(configDir);
configDir = createTempDir();
config = new Config(configDir);
config.loadConfig();
scopes = new Scopes(config);
});

const config = new Config(configDir);
config.loadConfig();
const scopes = new Scopes(config);

it("should return empty list", () => {
expect(scopes.getScopes().length).toEqual(0);
});
Expand All @@ -48,6 +56,6 @@ describe("test Scopes", () => {
});

afterAll(() => {
fs.rmSync(configDir, { recursive: true })
fs.rmSync(configDir, { recursive: true });
});
});

0 comments on commit d5422fd

Please sign in to comment.