Add a few tests
This commit is contained in:
parent
5178cb6381
commit
d9c2c5b2a1
7 changed files with 3462 additions and 3 deletions
79
plugin/src/sync-operations/document-lock.test.ts
Normal file
79
plugin/src/sync-operations/document-lock.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
27
plugin/src/utils/is-equal-bytes.test.ts
Normal file
27
plugin/src/utils/is-equal-bytes.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue