Add import template into tui. Fix tests that fail on macos. Fix some updates.
This commit is contained in:
78
tests/utils/load-template-from-file.test.ts
Normal file
78
tests/utils/load-template-from-file.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
||||
|
||||
import { parseTemplate } from "@xo-cash/utils";
|
||||
|
||||
import {
|
||||
loadTemplateFromFile,
|
||||
TemplateLoadError,
|
||||
} from "../../src/utils/load-template-from-file.js";
|
||||
import { p2pkhTemplate } from "../cli/mocks/template-p2pkh.js";
|
||||
|
||||
describe("loadTemplateFromFile", () => {
|
||||
let tempRoot: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempRoot = mkdtempSync(path.join(tmpdir(), "xo-load-template-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (existsSync(tempRoot)) {
|
||||
rmSync(tempRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("loads JSON templates directly", async () => {
|
||||
const jsonPath = path.join(tempRoot, "template.json");
|
||||
writeFileSync(jsonPath, JSON.stringify(p2pkhTemplate));
|
||||
|
||||
const contents = await loadTemplateFromFile(jsonPath);
|
||||
const parsed = parseTemplate(contents);
|
||||
|
||||
expect(parsed.name).toBe(p2pkhTemplate.name);
|
||||
});
|
||||
|
||||
test("loads TypeScript templates via child process", async () => {
|
||||
const tsTemplatePath = path.resolve(
|
||||
process.cwd(),
|
||||
"../templates/source/p2pkh.ts",
|
||||
);
|
||||
expect(existsSync(tsTemplatePath)).toBe(true);
|
||||
|
||||
const contents = await loadTemplateFromFile(tsTemplatePath);
|
||||
const parsed = parseTemplate(contents);
|
||||
|
||||
expect(parsed.name).toBe("Wallet (P2PKH)");
|
||||
});
|
||||
|
||||
test("loads JavaScript templates via child process", async () => {
|
||||
const jsPath = path.join(tempRoot, "template.mjs");
|
||||
writeFileSync(
|
||||
jsPath,
|
||||
`export default ${JSON.stringify(p2pkhTemplate)};\n`,
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const contents = await loadTemplateFromFile(jsPath);
|
||||
const parsed = parseTemplate(contents);
|
||||
|
||||
expect(parsed.name).toBe(p2pkhTemplate.name);
|
||||
});
|
||||
|
||||
test("throws TemplateLoadError for missing files", async () => {
|
||||
await expect(
|
||||
loadTemplateFromFile(path.join(tempRoot, "missing.json")),
|
||||
).rejects.toBeInstanceOf(TemplateLoadError);
|
||||
});
|
||||
|
||||
test("throws TemplateLoadError for unsupported extensions", async () => {
|
||||
const txtPath = path.join(tempRoot, "template.txt");
|
||||
writeFileSync(txtPath, "hello");
|
||||
|
||||
await expect(loadTemplateFromFile(txtPath)).rejects.toThrow(
|
||||
/Unsupported template file extension/,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user