Add a few tests

This commit is contained in:
Andras Schmelczer 2025-01-03 11:35:00 +00:00
parent 5178cb6381
commit d9c2c5b2a1
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
7 changed files with 3462 additions and 3 deletions

View file

@ -50,3 +50,9 @@ jobs:
cd plugin
npm install
npm run lint
- name: Test frontend
run: |
cd plugin
npm install
npm run test

2
.gitignore vendored
View file

@ -17,3 +17,5 @@ backend/db.sqlite3*
backend/config.yaml
*.log
plugin/coverage

8
plugin/jest.config.js Normal file
View file

@ -0,0 +1,8 @@
module.exports = {
testEnvironment: "node",
moduleFileExtensions: ["ts"],
testMatch: ["**/src/**/*.test.ts"],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
},
};

3333
plugin/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,7 @@
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"test": "jest",
"lint": "eslint --fix src",
"version": "node version-bump.mjs"
},
@ -13,6 +14,7 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^16.11.6",
"builtin-modules": "3.3.0",
"date-fns": "^4.1.0",
@ -22,13 +24,15 @@
"esbuild-sass-plugin": "^3.3.1",
"eslint": "9.17.0",
"eslint-plugin-unused-imports": "^4.1.4",
"fetch-retry": "^6.0.0",
"jest": "^29.7.0",
"obsidian": "1.7.2",
"openapi-fetch": "0.13.3",
"openapi-typescript": "7.4.4",
"p-queue": "^8.0.1",
"ts-jest": "^29.2.5",
"tslib": "2.4.0",
"typescript": "5.7.2",
"typescript-eslint": "8.18.0",
"fetch-retry": "^6.0.0"
"typescript-eslint": "8.18.0"
}
}
}

View file

@ -0,0 +1,79 @@
import {
tryLockDocument,
waitForDocumentLock,
unlockDocument,
} from "./document-lock";
import type { RelativePath } from "src/database/document-metadata";
describe("Document Lock Operations", () => {
const testPath: RelativePath = "test/document/path";
beforeEach(() => {
// Reset the state before each test
(global as any).locked = new Set<RelativePath>();
(global as any).waiters = new Map<RelativePath, (() => void)[]>();
});
test("should lock a document successfully", () => {
const result = tryLockDocument(testPath);
expect(result).toBe(true);
});
test("should not lock a document that is already locked", () => {
tryLockDocument(testPath);
const result = tryLockDocument(testPath);
expect(result).toBe(false);
});
test("should unlock a locked document", () => {
tryLockDocument(testPath);
unlockDocument(testPath);
const result = tryLockDocument(testPath);
expect(result).toBe(true);
unlockDocument(testPath);
});
test("should throw an error when unlocking a document that is not locked", () => {
expect(() => unlockDocument(testPath)).toThrow(
`Document ${testPath} is not locked, cannot unlock`
);
});
test("should wait for a document lock and resolve when unlocked", async () => {
tryLockDocument(testPath);
let resolved = false;
const waitPromise = waitForDocumentLock(testPath).then(() => {
resolved = true;
});
unlockDocument(testPath);
await waitPromise;
expect(resolved).toBe(true);
});
test("should resolve multiple waiters in FIFO order", async () => {
tryLockDocument(testPath);
let firstResolved = false;
let secondResolved = false;
const firstWaitPromise = waitForDocumentLock(testPath).then(() => {
firstResolved = true;
});
const secondWaitPromise = waitForDocumentLock(testPath).then(() => {
secondResolved = true;
});
unlockDocument(testPath);
await firstWaitPromise;
expect(firstResolved).toBe(true);
expect(secondResolved).toBe(false);
unlockDocument(testPath);
await secondWaitPromise;
expect(secondResolved).toBe(true);
});
});

View file

@ -0,0 +1,27 @@
import { isEqualBytes } from "./is-equal-bytes";
describe("isEqualBytes", () => {
it("should return true for equal byte arrays", () => {
const bytes1 = new Uint8Array([1, 2, 3, 4]);
const bytes2 = new Uint8Array([1, 2, 3, 4]);
expect(isEqualBytes(bytes1, bytes2)).toBe(true);
});
it("should return false for byte arrays of different lengths", () => {
const bytes1 = new Uint8Array([1, 2, 3, 4]);
const bytes2 = new Uint8Array([1, 2, 3]);
expect(isEqualBytes(bytes1, bytes2)).toBe(false);
});
it("should return true for empty byte arrays", () => {
const bytes1 = new Uint8Array([]);
const bytes2 = new Uint8Array([]);
expect(isEqualBytes(bytes1, bytes2)).toBe(true);
});
it("should return false for byte arrays with same length but different content", () => {
const bytes1 = new Uint8Array([1, 2, 3, 4]);
const bytes2 = new Uint8Array([4, 3, 2, 1]);
expect(isEqualBytes(bytes1, bytes2)).toBe(false);
});
});