128 lines
5.0 KiB
JavaScript
128 lines
5.0 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import fs from "fs";
|
|
import os from "os";
|
|
import path from "path";
|
|
|
|
require("../src/init")
|
|
async function createRemote() {
|
|
const Remote = require("../src/remote")
|
|
const remote = new Remote();
|
|
await remote.init()
|
|
return remote
|
|
}
|
|
|
|
function writeFixtureProject(tempDir, version, content) {
|
|
const projectDir = path.resolve(tempDir, `fixture-${version}`);
|
|
fs.mkdirSync(projectDir, { recursive: true });
|
|
|
|
const filePath = path.resolve(projectDir, "myapp.txt");
|
|
fs.writeFileSync(filePath, content, "utf-8");
|
|
|
|
fs.writeFileSync(path.resolve(projectDir, "meta.json"), JSON.stringify({
|
|
id: "myapp",
|
|
version,
|
|
desc: "test app",
|
|
official_url: "https://example.test/myapp",
|
|
dist: [
|
|
{
|
|
path: filePath,
|
|
os: "any",
|
|
platform: "any",
|
|
filename: "myapp.txt",
|
|
type: "data"
|
|
}
|
|
]
|
|
}, null, 4), "utf-8");
|
|
|
|
return projectDir;
|
|
}
|
|
|
|
let tempDir;
|
|
let localStore;
|
|
|
|
beforeEach(() => {
|
|
tempDir = fs.mkdtempSync(path.resolve(os.tmpdir(), "kit-remote-test"));
|
|
localStore = $sConfig.getLocalStorePath();
|
|
});
|
|
|
|
afterEach(() => {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe("remote module public API", () => {
|
|
test("1-1 exposes documented public methods", async() => {
|
|
const remote = await createRemote();
|
|
expect(typeof remote.check).toBe("function");
|
|
expect(typeof remote.publish).toBe("function");
|
|
expect(typeof remote.install).toBe("function");
|
|
expect(typeof remote.list).toBe("function");
|
|
expect(typeof remote.use).toBe("function");
|
|
expect(typeof remote.rollback).toBe("function");
|
|
expect(typeof remote.remove).toBe("function");
|
|
});
|
|
|
|
test("1-2 check validates an available webdav client", async () => {
|
|
const remote = await createRemote();
|
|
expect(await remote.check()).toBe(true);
|
|
});
|
|
|
|
test("1-3 publish stores metadata and version artifacts in webdav layout", async () => {
|
|
const remote = await createRemote();
|
|
const projectDir = writeFixtureProject(tempDir, "1.0.0", "hello v1");
|
|
|
|
await remote.publish(projectDir);
|
|
|
|
const metadata = JSON.parse(await remote.client.getFileContents("/myapp/metadata.json", { format: "text" }));
|
|
const version = JSON.parse(await remote.client.getFileContents("/myapp/1.0.0/version.json", { format: "text" }));
|
|
const artifact = await remote.client.getFileContents("/myapp/1.0.0/any/any/myapp.txt", { format: "text" });
|
|
|
|
expect(metadata.project_id).toBe("myapp");
|
|
expect(metadata.latest_version).toBe("1.0.0");
|
|
expect(metadata.versions).toContain("1.0.0");
|
|
expect(version.artifacts[0].path).toBe("any/any/myapp.txt");
|
|
expect(artifact).toBe("hello v1");
|
|
}, 30000);
|
|
|
|
test("install downloads latest matching artifact and records local state", async () => {
|
|
const remote = await createRemote();
|
|
const projectDir = writeFixtureProject(tempDir, "1.0.0", "hello v1");
|
|
await remote.publish(projectDir);
|
|
|
|
const installed = await remote.install("myapp");
|
|
|
|
const moduleRoot = path.resolve(localStore, "modules", "myapp");
|
|
const installJson = JSON.parse(fs.readFileSync(path.resolve(moduleRoot, ".meta", "install.json"), "utf-8"));
|
|
const installedFile = fs.readFileSync(path.resolve(moduleRoot, "versions", "1.0.0", "any", "any", "myapp.txt"), "utf-8");
|
|
|
|
expect(installed).toBe("1.0.0");
|
|
expect(installedFile).toBe("hello v1");
|
|
expect(installJson.current_version).toBe("1.0.0");
|
|
expect(installJson.installed_versions).toContain("1.0.0");
|
|
expect(fs.existsSync(path.resolve(moduleRoot, "current"))).toBe(true);
|
|
});
|
|
|
|
test("list use rollback and remove manage installed versions", async () => {
|
|
const remote = await createRemote();
|
|
await remote.publish(writeFixtureProject(tempDir, "1.0.0", "hello v1"));
|
|
await remote.install("myapp");
|
|
await remote.publish(writeFixtureProject(tempDir, "2.0.0", "hello v2"));
|
|
await remote.install("myapp");
|
|
|
|
expect(await remote.list("myapp")).toEqual(["1.0.0", "2.0.0"]);
|
|
|
|
await remote.rollback("myapp");
|
|
let installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", "myapp", ".meta", "install.json"), "utf-8"));
|
|
expect(installJson.current_version).toBe("1.0.0");
|
|
|
|
await remote.use("myapp@2.0.0");
|
|
installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", "myapp", ".meta", "install.json"), "utf-8"));
|
|
expect(installJson.current_version).toBe("2.0.0");
|
|
|
|
await remote.remove("myapp@1.0.0");
|
|
installJson = JSON.parse(fs.readFileSync(path.resolve(localStore, "modules", "myapp", ".meta", "install.json"), "utf-8"));
|
|
expect(installJson.installed_versions).toEqual(["2.0.0"]);
|
|
expect(fs.existsSync(path.resolve(localStore, "modules", "myapp", "versions", "1.0.0"))).toBe(false);
|
|
});
|
|
});
|
|
|