164 lines
5.0 KiB
TypeScript
164 lines
5.0 KiB
TypeScript
import Repository, { IDataStore } from "./repo";
|
|
import FSDataStore from "./datasources/fs";
|
|
import * as Fs from "fs";
|
|
import { expect } from "chai";
|
|
import * as Crypto from "crypto";
|
|
|
|
const t = (t: string) => new TextEncoder().encode(t);
|
|
const td = (t: Uint8Array | undefined) => new TextDecoder().decode(t);
|
|
const exists = (path: string) => Fs.promises.access(path).then(() => true).catch(() => false)
|
|
|
|
let test = 0;
|
|
let repoPath = "";
|
|
|
|
beforeEach(async () => {
|
|
repoPath = "./testrepo_" + test;
|
|
console.log(" \tRunning at repo:", repoPath);
|
|
await Fs.promises.rm(repoPath, { recursive: true, force: true });
|
|
})
|
|
|
|
afterEach(async () => {
|
|
test++;
|
|
})
|
|
|
|
describe("NodeJS DataStore", () => {
|
|
it("should throw error when creating new instance", () => {
|
|
const ds = new FSDataStore(repoPath);
|
|
expect(ds).to.not.be.undefined;
|
|
})
|
|
|
|
it("should return undefined on read on non-existant field", async () => {
|
|
const ds = new FSDataStore(repoPath);
|
|
let val = await ds.get("test");
|
|
expect(val).to.be.undefined;
|
|
})
|
|
|
|
it("should not fail when writing a new key and create the necessary files", async () => {
|
|
const ds = new FSDataStore(repoPath);
|
|
const text = "Hallo Welt";
|
|
await ds.set("test", t(text));
|
|
|
|
let ex = await exists(repoPath + "/test");
|
|
expect(ex).to.be.true;
|
|
|
|
let file_cont = await Fs.promises.readFile(repoPath + "/test", "utf-8");
|
|
expect(file_cont).to.equal(text);
|
|
})
|
|
|
|
it("has should return false on non-existing field", async () => {
|
|
const ds = new FSDataStore(repoPath);
|
|
expect(await ds.has("test")).to.be.false;
|
|
})
|
|
|
|
it("has should return true on existing field", async () => {
|
|
const ds = new FSDataStore(repoPath);
|
|
const cont = t("Hallo Welt");
|
|
await ds.set("test", cont);
|
|
|
|
let ex = await exists(repoPath + "/test");
|
|
expect(ex).to.be.true;
|
|
})
|
|
|
|
it("get should return value of existing field", async () => {
|
|
const ds = new FSDataStore(repoPath);
|
|
const text = "Hallo Welt";
|
|
await ds.set("test", t(text));
|
|
|
|
expect(td(await ds.get("test"))).to.equal(text);
|
|
})
|
|
})
|
|
|
|
describe("Basic Repo functions", () => {
|
|
let ds: IDataStore = new FSDataStore(repoPath);
|
|
beforeEach(() => {
|
|
ds = new FSDataStore(repoPath);
|
|
})
|
|
|
|
it("should not throw error when creating new instance", () => {
|
|
const repo = new Repository(ds)
|
|
expect(repo).to.not.be.undefined;
|
|
})
|
|
|
|
it("should return an empty list on an empty repository", async () => {
|
|
const repo = new Repository(ds)
|
|
const list = await repo.readdir("/");
|
|
expect(list).to.be.an("array");
|
|
expect(list.length).to.equal(0)
|
|
})
|
|
|
|
it("should return undefined on non-existant file", async () => {
|
|
const repo = new Repository(ds)
|
|
let res = await repo.read("test");
|
|
expect(res).to.be.undefined;
|
|
})
|
|
|
|
it("should be possible to write data", async () => {
|
|
const testData = "Hallo Welt";
|
|
const repo = new Repository(ds)
|
|
await repo.write("test", t(testData));
|
|
|
|
let res = await Fs.promises.readdir(repoPath + "/objects");
|
|
expect(res.length).to.be.equal(3);
|
|
|
|
expect(await exists(repoPath + "/HEAD")).to.be.true;
|
|
})
|
|
|
|
it("should be possible to write nested data", async () => {
|
|
const testData = "Hallo Welt";
|
|
const repo = new Repository(ds)
|
|
await repo.write("test/hallo-welt", t(testData));
|
|
|
|
let res = await Fs.promises.readdir(repoPath + "/objects");
|
|
|
|
expect(res.length).to.be.equal(4);
|
|
expect(await exists(repoPath + "/HEAD")).to.be.true;
|
|
})
|
|
|
|
it("readdir should return the given entries", async () => {
|
|
const testData = "Hallo Welt";
|
|
const repo = new Repository(ds)
|
|
await repo.write("test/hallo-welt", t(testData));
|
|
await repo.write("tests", t(testData));
|
|
|
|
const res = await repo.readdir("/")
|
|
expect(res.length).to.be.equal(2);
|
|
// expect(res).to.include.
|
|
})
|
|
|
|
it("should be possible to read back written data", async () => {
|
|
const testData = "Hallo Welt";
|
|
const repo = new Repository(ds)
|
|
await repo.write("test", t(testData));
|
|
|
|
const res = await repo.read("test");
|
|
expect(td(res)).to.equal(testData);
|
|
})
|
|
|
|
it("should be possible to delete entries", async () => {
|
|
const testData = "Hallo Welt";
|
|
const repo = new Repository(ds)
|
|
await repo.write("test", t(testData));
|
|
|
|
const res = await repo.read("test");
|
|
expect(td(res)).to.equal(testData);
|
|
|
|
await repo.delete("test");
|
|
|
|
const res2 = await repo.read("test");
|
|
expect(res2).to.be.undefined;
|
|
})
|
|
|
|
it("should be possible to get a list of all versions of a file", async () => {
|
|
const testData = "Hallo Welt";
|
|
const repo = new Repository(ds)
|
|
await repo.write("test", t(testData));
|
|
await repo.write("test", t(testData + 1));
|
|
await repo.write("test", t(testData + 2));
|
|
await repo.write("test", t(testData + 3));
|
|
await repo.write("test", t(testData + 4));
|
|
|
|
const res = await repo.log("test");
|
|
expect(res).to.not.be.undefined;
|
|
expect(res.length).to.equal(5);
|
|
})
|
|
}) |