kit.program/kit/test/remote_test.js
cheney e05ce19eac
Some checks are pending
TDevOPsCICD / build-image (push) Waiting to run
整理远程模块
2026-06-16 16:40:34 +08:00

101 lines
4.3 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
}
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("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("check validates an available webdav client", async () => {
const remote = await createRemote();
expect(await remote.check()).toBe(true);
});
test("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");
});
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);
});
});