Migrate to node:test
This commit is contained in:
parent
7aab7b05d6
commit
bbbfc1a059
24 changed files with 759 additions and 6421 deletions
|
|
@ -20,7 +20,14 @@ export default [
|
|||
"no-unused-vars": "off",
|
||||
"@typescript-eslint/restrict-template-expressions": "off",
|
||||
"@typescript-eslint/no-unused-vars": "off",
|
||||
"@typescript-eslint/no-floating-promises": "error",
|
||||
"@typescript-eslint/no-floating-promises": [
|
||||
"error",
|
||||
{
|
||||
allowForKnownSafeCalls: [
|
||||
{ from: "package", name: ["suite", "test"], package: "node:test" },
|
||||
],
|
||||
},
|
||||
],
|
||||
"@typescript-eslint/parameter-properties": "off",
|
||||
"@typescript-eslint/require-await": "off",
|
||||
"@typescript-eslint/class-methods-use-this": "off",
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
module.exports = {
|
||||
preset: "ts-jest"
|
||||
};
|
||||
|
|
@ -6,35 +6,33 @@
|
|||
"scripts": {
|
||||
"dev": "webpack watch --mode development",
|
||||
"build": "webpack --mode production",
|
||||
"test": "jest",
|
||||
"test": "tsx --test src/**/*.test.ts",
|
||||
"version": "node version-bump.mjs"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^30.0.0",
|
||||
"@types/node": "^22.15.30",
|
||||
"css-loader": "^7.1.2",
|
||||
"date-fns": "^4.1.0",
|
||||
"file-loader": "^6.2.0",
|
||||
"fs-extra": "^11.3.0",
|
||||
"jest": "^30.1.1",
|
||||
"mini-css-extract-plugin": "^2.9.2",
|
||||
"obsidian": "1.8.7",
|
||||
"reconcile-text": "^0.5.0",
|
||||
"resolve-url-loader": "^5.0.0",
|
||||
"sass": "^1.91.0",
|
||||
"sass-loader": "^16.0.5",
|
||||
"sync-client": "file:../sync-client",
|
||||
"terser-webpack-plugin": "^5.3.14",
|
||||
"ts-jest": "^29.4.1",
|
||||
"ts-loader": "^9.5.2",
|
||||
"tslib": "2.8.1",
|
||||
"tsx": "^4.20.5",
|
||||
"typescript": "5.8.3",
|
||||
"url": "^0.11.4",
|
||||
"virtual-scroller": "^1.13.1",
|
||||
"webpack": "^5.99.9",
|
||||
"webpack-cli": "^6.0.1",
|
||||
"reconcile-text": "^0.5.0"
|
||||
"webpack-cli": "^6.0.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,42 +1,44 @@
|
|||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { lineAndColumnToPosition } from "./line-and-column-to-position";
|
||||
|
||||
describe("lineAndColumnToPosition", () => {
|
||||
it("should return the correct position for the first line", () => {
|
||||
const text = "Hello\nWorld";
|
||||
const position = lineAndColumnToPosition(text, 0, 3);
|
||||
expect(position).toBe(3);
|
||||
assert.strictEqual(position, 3);
|
||||
});
|
||||
|
||||
it("should return the correct position for the second line", () => {
|
||||
const text = "Hello\nWorld";
|
||||
const position = lineAndColumnToPosition(text, 1, 2);
|
||||
expect(position).toBe(8);
|
||||
assert.strictEqual(position, 8);
|
||||
});
|
||||
|
||||
it("should return the correct position for an empty string", () => {
|
||||
const text = "";
|
||||
const position = lineAndColumnToPosition(text, 0, 0);
|
||||
expect(position).toBe(0);
|
||||
assert.strictEqual(position, 0);
|
||||
});
|
||||
|
||||
it("with carrige return", () => {
|
||||
expect(lineAndColumnToPosition("a\nb", 1, 1)).toBe(3);
|
||||
expect(lineAndColumnToPosition("a\r\nb", 1, 1)).toBe(3);
|
||||
assert.strictEqual(lineAndColumnToPosition("a\nb", 1, 1), 3);
|
||||
assert.strictEqual(lineAndColumnToPosition("a\r\nb", 1, 1), 3);
|
||||
});
|
||||
|
||||
it("should handle multi-line strings with varying lengths", () => {
|
||||
const text = "Line1\nLongerLine2\nShort3";
|
||||
const position = lineAndColumnToPosition(text, 2, 4);
|
||||
expect(position).toBe(22);
|
||||
assert.strictEqual(position, 22);
|
||||
});
|
||||
|
||||
it("should throw an error if the line number is out of range", () => {
|
||||
const text = "Line1\nLine2";
|
||||
expect(() => lineAndColumnToPosition(text, 3, 0)).toThrow();
|
||||
assert.throws(() => lineAndColumnToPosition(text, 3, 0));
|
||||
});
|
||||
|
||||
it("should throw an error if the column number is out of range", () => {
|
||||
const text = "Line1\nLine2";
|
||||
expect(() => lineAndColumnToPosition(text, 1, 10)).toThrow();
|
||||
assert.throws(() => lineAndColumnToPosition(text, 1, 10));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,53 +1,58 @@
|
|||
import { describe, test } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { positionToLineAndColumn } from "./position-to-line-and-column";
|
||||
|
||||
describe("positionToLineAndColumn", () => {
|
||||
test("converts position to line and column in multi-line text", () => {
|
||||
const text = "ab\ncd\n";
|
||||
expect(positionToLineAndColumn(text, 0)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn(text, 0), {
|
||||
line: 0,
|
||||
column: 0
|
||||
});
|
||||
expect(positionToLineAndColumn(text, 1)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn(text, 1), {
|
||||
line: 0,
|
||||
column: 1
|
||||
});
|
||||
expect(positionToLineAndColumn(text, 2)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn(text, 2), {
|
||||
line: 0,
|
||||
column: 2
|
||||
});
|
||||
expect(positionToLineAndColumn(text, 3)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
|
||||
line: 1,
|
||||
column: 0
|
||||
});
|
||||
expect(positionToLineAndColumn(text, 4)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn(text, 4), {
|
||||
line: 1,
|
||||
column: 1
|
||||
});
|
||||
expect(positionToLineAndColumn(text, 6)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn(text, 6), {
|
||||
line: 2,
|
||||
column: 0
|
||||
});
|
||||
});
|
||||
|
||||
test("with carrige returns", () => {
|
||||
expect(positionToLineAndColumn("a\nb", 3)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn("a\nb", 3), {
|
||||
line: 1,
|
||||
column: 1
|
||||
});
|
||||
|
||||
expect(positionToLineAndColumn("a\r\nb", 3)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn("a\r\nb", 3), {
|
||||
line: 1,
|
||||
column: 1
|
||||
});
|
||||
});
|
||||
|
||||
test("handles empty input", () => {
|
||||
expect(positionToLineAndColumn("", 0)).toEqual({ line: 0, column: 0 });
|
||||
assert.deepStrictEqual(positionToLineAndColumn("", 0), {
|
||||
line: 0,
|
||||
column: 0
|
||||
});
|
||||
});
|
||||
|
||||
test("handles positions at the end of text", () => {
|
||||
const text = "End";
|
||||
expect(positionToLineAndColumn(text, 3)).toEqual({
|
||||
assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
|
||||
line: 0,
|
||||
column: 3
|
||||
});
|
||||
|
|
@ -55,7 +60,7 @@ describe("positionToLineAndColumn", () => {
|
|||
|
||||
test("throws error for position out of range", () => {
|
||||
const text = "Short text";
|
||||
expect(() => positionToLineAndColumn(text, 15)).toThrow();
|
||||
expect(() => positionToLineAndColumn(text, -1)).toThrow();
|
||||
assert.throws(() => positionToLineAndColumn(text, 15));
|
||||
assert.throws(() => positionToLineAndColumn(text, -1));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
"allowSyntheticDefaultImports": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"ESNext"
|
||||
"ES2024"
|
||||
]
|
||||
},
|
||||
"exclude": [
|
||||
|
|
|
|||
6828
frontend/package-lock.json
generated
6828
frontend/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -1,3 +0,0 @@
|
|||
module.exports = {
|
||||
preset: "ts-jest"
|
||||
};
|
||||
|
|
@ -10,22 +10,20 @@
|
|||
"scripts": {
|
||||
"dev": "webpack watch --mode development",
|
||||
"build": "webpack --mode production",
|
||||
"test": "jest"
|
||||
"test": "tsx --test src/**/*.test.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"byte-base64": "^1.1.0",
|
||||
"minimatch": "^10.0.1",
|
||||
"p-queue": "^8.1.0",
|
||||
"uuid": "^11.1.0",
|
||||
"reconcile-text": "^0.5.0"
|
||||
"reconcile-text": "^0.5.0",
|
||||
"uuid": "^11.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^30.0.0",
|
||||
"@types/node": "^22.15.30",
|
||||
"jest": "^30.1.1",
|
||||
"ts-jest": "^29.4.1",
|
||||
"ts-loader": "^9.5.2",
|
||||
"tslib": "2.8.1",
|
||||
"tsx": "^4.20.5",
|
||||
"typescript": "5.8.3",
|
||||
"webpack": "^5.99.9",
|
||||
"webpack-cli": "^6.0.1",
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { describe, it } from "node:test";
|
||||
import type {
|
||||
Database,
|
||||
DocumentRecord,
|
||||
|
|
|
|||
|
|
@ -70,7 +70,10 @@ export class SyncService {
|
|||
formData.append("document_id", documentId);
|
||||
}
|
||||
formData.append("relative_path", relativePath);
|
||||
formData.append("content", new Blob([contentBytes]));
|
||||
formData.append(
|
||||
"content",
|
||||
new Blob([new Uint8Array(contentBytes)])
|
||||
);
|
||||
|
||||
const response = await this.client(this.getUrl("/documents"), {
|
||||
method: "POST",
|
||||
|
|
@ -117,7 +120,10 @@ export class SyncService {
|
|||
const formData = new FormData();
|
||||
formData.append("parent_version_id", parentVersionId.toString());
|
||||
formData.append("relative_path", relativePath);
|
||||
formData.append("content", new Blob([contentBytes]));
|
||||
formData.append(
|
||||
"content",
|
||||
new Blob([new Uint8Array(contentBytes)])
|
||||
);
|
||||
|
||||
const response = await this.client(
|
||||
this.getUrl(`/documents/${documentId}`),
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import * as assert from "assert";
|
||||
import assert from "node:assert";
|
||||
|
||||
export function assertSetContainsExactly<T>(set: Set<T>, ...values: T[]): void {
|
||||
assert(
|
||||
assert.ok(
|
||||
set.size === values.length &&
|
||||
Array.from(set).every((value) => values.includes(value)),
|
||||
`Expected set to contain only ${values.map((v) => '"' + v + '"').join(", ")}, but it contained ${Array.from(
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { Logger } from "../tracing/logger";
|
||||
import { globsToRegexes } from "./globs-to-regexes";
|
||||
|
||||
|
|
@ -5,7 +7,7 @@ describe("globsToRegexes", () => {
|
|||
it("basicExample", async () => {
|
||||
const [regex] = globsToRegexes([".git/**"], new Logger());
|
||||
|
||||
expect(regex.test(".git/objects/object")).toBeTruthy();
|
||||
expect(regex.test(".git/objects/.object")).toBeTruthy();
|
||||
assert.ok(regex.test(".git/objects/object"));
|
||||
assert.ok(regex.test(".git/objects/.object"));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,27 +1,29 @@
|
|||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
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);
|
||||
assert.strictEqual(isEqualBytes(bytes1, bytes2), 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);
|
||||
assert.strictEqual(isEqualBytes(bytes1, bytes2), false);
|
||||
});
|
||||
|
||||
it("should return true for empty byte arrays", () => {
|
||||
const bytes1 = new Uint8Array([]);
|
||||
const bytes2 = new Uint8Array([]);
|
||||
expect(isEqualBytes(bytes1, bytes2)).toBe(true);
|
||||
assert.strictEqual(isEqualBytes(bytes1, bytes2), 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);
|
||||
assert.strictEqual(isEqualBytes(bytes1, bytes2), false);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,28 +1,42 @@
|
|||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { isFileTypeMergable } from "./is-file-type-mergable";
|
||||
|
||||
describe("isFileTypeMergable", () => {
|
||||
it("should return true for .md files", () => {
|
||||
expect(isFileTypeMergable(".md")).toBe(true);
|
||||
expect(isFileTypeMergable("hi.md")).toBe(true);
|
||||
expect(isFileTypeMergable("my/path/to/my/document.md")).toBe(true);
|
||||
assert.strictEqual(isFileTypeMergable(".md"), true);
|
||||
assert.strictEqual(isFileTypeMergable("hi.md"), true);
|
||||
assert.strictEqual(
|
||||
isFileTypeMergable("my/path/to/my/document.md"),
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it("should return true for .txt files", () => {
|
||||
expect(isFileTypeMergable(".txt")).toBe(true);
|
||||
expect(isFileTypeMergable("hi.txt")).toBe(true);
|
||||
expect(isFileTypeMergable("my/path/to/my/document.txt")).toBe(true);
|
||||
assert.strictEqual(isFileTypeMergable(".txt"), true);
|
||||
assert.strictEqual(isFileTypeMergable("hi.txt"), true);
|
||||
assert.strictEqual(
|
||||
isFileTypeMergable("my/path/to/my/document.txt"),
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it("should be case insensitive", () => {
|
||||
expect(isFileTypeMergable("hi.MD")).toBe(true);
|
||||
expect(isFileTypeMergable("my/path/to/my/DOCUMENT.MD")).toBe(true);
|
||||
expect(isFileTypeMergable("hi.TXT")).toBe(true);
|
||||
expect(isFileTypeMergable("my/path/to/my/DOCUMENT.TXT")).toBe(true);
|
||||
assert.strictEqual(isFileTypeMergable("hi.MD"), true);
|
||||
assert.strictEqual(
|
||||
isFileTypeMergable("my/path/to/my/DOCUMENT.MD"),
|
||||
true
|
||||
);
|
||||
assert.strictEqual(isFileTypeMergable("hi.TXT"), true);
|
||||
assert.strictEqual(
|
||||
isFileTypeMergable("my/path/to/my/DOCUMENT.TXT"),
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it("should return false for non-mergable file types", () => {
|
||||
expect(isFileTypeMergable(".json")).toBe(false);
|
||||
expect(isFileTypeMergable("HELLO.JSON")).toBe(false);
|
||||
expect(isFileTypeMergable("my/config.yml")).toBe(false);
|
||||
assert.strictEqual(isFileTypeMergable(".json"), false);
|
||||
assert.strictEqual(isFileTypeMergable("HELLO.JSON"), false);
|
||||
assert.strictEqual(isFileTypeMergable("my/config.yml"), false);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { describe, it, beforeEach } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { Logger } from "../tracing/logger";
|
||||
import type { RelativePath } from "../persistence/database";
|
||||
import { Locks } from "./locks";
|
||||
|
|
@ -14,18 +16,18 @@ describe("withLock", () => {
|
|||
locks = new Locks<RelativePath>(logger);
|
||||
});
|
||||
|
||||
test("should execute function with single key lock", async () => {
|
||||
it("should execute function with single key lock", async () => {
|
||||
let executionCount = 0;
|
||||
const result = await locks.withLock(testPath, () => {
|
||||
executionCount++;
|
||||
return "success";
|
||||
});
|
||||
|
||||
expect(result).toBe("success");
|
||||
expect(executionCount).toBe(1);
|
||||
assert.strictEqual(result, "success");
|
||||
assert.strictEqual(executionCount, 1);
|
||||
});
|
||||
|
||||
test("should execute async function with single key lock", async () => {
|
||||
it("should execute async function with single key lock", async () => {
|
||||
let executionCount = 0;
|
||||
const result = await locks.withLock(testPath, async () => {
|
||||
executionCount++;
|
||||
|
|
@ -33,22 +35,22 @@ describe("withLock", () => {
|
|||
return "async-success";
|
||||
});
|
||||
|
||||
expect(result).toBe("async-success");
|
||||
expect(executionCount).toBe(1);
|
||||
assert.strictEqual(result, "async-success");
|
||||
assert.strictEqual(executionCount, 1);
|
||||
});
|
||||
|
||||
test("should execute function with multiple key locks", async () => {
|
||||
it("should execute function with multiple key locks", async () => {
|
||||
let executionCount = 0;
|
||||
const result = await locks.withLock([testPath, testPath2], () => {
|
||||
executionCount++;
|
||||
return "multi-success";
|
||||
});
|
||||
|
||||
expect(result).toBe("multi-success");
|
||||
expect(executionCount).toBe(1);
|
||||
assert.strictEqual(result, "multi-success");
|
||||
assert.strictEqual(executionCount, 1);
|
||||
});
|
||||
|
||||
test("should sort multiple keys to prevent deadlocks", async () => {
|
||||
it("should sort multiple keys to prevent deadlocks", async () => {
|
||||
const executionOrder: string[] = [];
|
||||
|
||||
// Start two concurrent operations with keys in different orders
|
||||
|
|
@ -68,10 +70,10 @@ describe("withLock", () => {
|
|||
|
||||
const [result1, result2] = await Promise.all([promise1, promise2]);
|
||||
|
||||
expect(result1).toBe("result1");
|
||||
expect(result2).toBe("result2");
|
||||
assert.strictEqual(result1, "result1");
|
||||
assert.strictEqual(result2, "result2");
|
||||
// One operation should complete entirely before the other starts
|
||||
expect(executionOrder).toEqual([
|
||||
assert.deepStrictEqual(executionOrder, [
|
||||
"operation1-start",
|
||||
"operation1-end",
|
||||
"operation2-start",
|
||||
|
|
@ -79,7 +81,7 @@ describe("withLock", () => {
|
|||
]);
|
||||
});
|
||||
|
||||
test("should serialize access to same key", async () => {
|
||||
it("should serialize access to same key", async () => {
|
||||
const executionOrder: string[] = [];
|
||||
|
||||
const promise1 = locks.withLock(testPath, async () => {
|
||||
|
|
@ -98,9 +100,9 @@ describe("withLock", () => {
|
|||
|
||||
const [result1, result2] = await Promise.all([promise1, promise2]);
|
||||
|
||||
expect(result1).toBe("result1");
|
||||
expect(result2).toBe("result2");
|
||||
expect(executionOrder).toEqual([
|
||||
assert.strictEqual(result1, "result1");
|
||||
assert.strictEqual(result2, "result2");
|
||||
assert.deepStrictEqual(executionOrder, [
|
||||
"operation1-start",
|
||||
"operation1-end",
|
||||
"operation2-start",
|
||||
|
|
@ -108,7 +110,7 @@ describe("withLock", () => {
|
|||
]);
|
||||
});
|
||||
|
||||
test("should allow concurrent access to different keys", async () => {
|
||||
it("should allow concurrent access to different keys", async () => {
|
||||
const executionOrder: string[] = [];
|
||||
|
||||
const promise1 = locks.withLock(testPath, async () => {
|
||||
|
|
@ -127,54 +129,56 @@ describe("withLock", () => {
|
|||
|
||||
const [result1, result2] = await Promise.all([promise1, promise2]);
|
||||
|
||||
expect(result1).toBe("result1");
|
||||
expect(result2).toBe("result2");
|
||||
assert.strictEqual(result1, "result1");
|
||||
assert.strictEqual(result2, "result2");
|
||||
// Both operations should run concurrently
|
||||
expect(executionOrder[0]).toBe("operation1-start");
|
||||
expect(executionOrder[1]).toBe("operation2-start");
|
||||
assert.strictEqual(executionOrder[0], "operation1-start");
|
||||
assert.strictEqual(executionOrder[1], "operation2-start");
|
||||
});
|
||||
|
||||
test("should release locks even if function throws", async () => {
|
||||
it("should release locks even if function throws", async () => {
|
||||
const error = new Error("test error");
|
||||
|
||||
await expect(
|
||||
await assert.rejects(
|
||||
locks.withLock(testPath, () => {
|
||||
throw error;
|
||||
})
|
||||
).rejects.toThrow("test error");
|
||||
}),
|
||||
{ message: "test error" }
|
||||
);
|
||||
|
||||
// Lock should be released, allowing another operation
|
||||
const result = await locks.withLock(
|
||||
testPath,
|
||||
() => "success-after-error"
|
||||
);
|
||||
expect(result).toBe("success-after-error");
|
||||
assert.strictEqual(result, "success-after-error");
|
||||
});
|
||||
|
||||
test("should release locks even if async function throws", async () => {
|
||||
it("should release locks even if async function throws", async () => {
|
||||
const error = new Error("async test error");
|
||||
|
||||
await expect(
|
||||
await assert.rejects(
|
||||
locks.withLock(testPath, async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
throw error;
|
||||
})
|
||||
).rejects.toThrow("async test error");
|
||||
}),
|
||||
{ message: "async test error" }
|
||||
);
|
||||
|
||||
// Lock should be released, allowing another operation
|
||||
const result = await locks.withLock(
|
||||
testPath,
|
||||
() => "success-after-async-error"
|
||||
);
|
||||
expect(result).toBe("success-after-async-error");
|
||||
assert.strictEqual(result, "success-after-async-error");
|
||||
});
|
||||
|
||||
test("should handle empty array of keys", async () => {
|
||||
it("should handle empty array of keys", async () => {
|
||||
const result = await locks.withLock([], () => "empty-keys");
|
||||
expect(result).toBe("empty-keys");
|
||||
assert.strictEqual(result, "empty-keys");
|
||||
});
|
||||
|
||||
test("should maintain FIFO order for multiple waiters", async () => {
|
||||
it("should maintain FIFO order for multiple waiters", async () => {
|
||||
const executionOrder: string[] = [];
|
||||
|
||||
// Start first operation that holds the lock
|
||||
|
|
@ -209,10 +213,10 @@ describe("withLock", () => {
|
|||
thirdPromise
|
||||
]);
|
||||
|
||||
expect(first).toBe("first");
|
||||
expect(second).toBe("second");
|
||||
expect(third).toBe("third");
|
||||
expect(executionOrder).toEqual([
|
||||
assert.strictEqual(first, "first");
|
||||
assert.strictEqual(second, "second");
|
||||
assert.strictEqual(third, "third");
|
||||
assert.deepStrictEqual(executionOrder, [
|
||||
"first-start",
|
||||
"first-end",
|
||||
"second-start",
|
||||
|
|
|
|||
|
|
@ -1,60 +1,62 @@
|
|||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { CoveredValues } from "./min-covered";
|
||||
|
||||
describe("CoveredValues", () => {
|
||||
test("should initialize with the given min value", () => {
|
||||
it("should initialize with the given min value", () => {
|
||||
const covered = new CoveredValues(5);
|
||||
expect(covered.min).toBe(5);
|
||||
assert.strictEqual(covered.min, 5);
|
||||
});
|
||||
|
||||
test("should add values greater than min", () => {
|
||||
it("should add values greater than min", () => {
|
||||
const covered = new CoveredValues(0);
|
||||
covered.add(3);
|
||||
expect(covered.min).toBe(0);
|
||||
assert.strictEqual(covered.min, 0);
|
||||
covered.add(1);
|
||||
expect(covered.min).toBe(1);
|
||||
assert.strictEqual(covered.min, 1);
|
||||
covered.add(4);
|
||||
expect(covered.min).toBe(1);
|
||||
assert.strictEqual(covered.min, 1);
|
||||
covered.add(2);
|
||||
expect(covered.min).toBe(4);
|
||||
assert.strictEqual(covered.min, 4);
|
||||
});
|
||||
|
||||
test("should ignore duplicate values", () => {
|
||||
it("should ignore duplicate values", () => {
|
||||
const covered = new CoveredValues(0);
|
||||
covered.add(3);
|
||||
covered.add(3);
|
||||
covered.add(3);
|
||||
expect(covered.min).toBe(0);
|
||||
assert.strictEqual(covered.min, 0);
|
||||
covered.add(1);
|
||||
covered.add(2);
|
||||
expect(covered.min).toBe(3);
|
||||
assert.strictEqual(covered.min, 3);
|
||||
});
|
||||
|
||||
test("should handle multiple consecutive values", () => {
|
||||
it("should handle multiple consecutive values", () => {
|
||||
const covered = new CoveredValues(132);
|
||||
for (let i = 250; i > 132; i--) {
|
||||
expect(covered.min).toBe(132);
|
||||
assert.strictEqual(covered.min, 132);
|
||||
covered.add(i);
|
||||
}
|
||||
expect(covered.min).toBe(250);
|
||||
assert.strictEqual(covered.min, 250);
|
||||
});
|
||||
|
||||
test("should handle adding values lower than current min", () => {
|
||||
it("should handle adding values lower than current min", () => {
|
||||
const covered = new CoveredValues(5);
|
||||
covered.add(3);
|
||||
expect(covered.min).toBe(5);
|
||||
assert.strictEqual(covered.min, 5);
|
||||
covered.add(6);
|
||||
expect(covered.min).toBe(6);
|
||||
assert.strictEqual(covered.min, 6);
|
||||
});
|
||||
|
||||
test("should handle force setting min value", () => {
|
||||
it("should handle force setting min value", () => {
|
||||
const covered = new CoveredValues(5);
|
||||
covered.add(7);
|
||||
covered.add(8);
|
||||
covered.add(9);
|
||||
expect(covered.min).toBe(5);
|
||||
assert.strictEqual(covered.min, 5);
|
||||
covered.min = 6;
|
||||
expect(covered.min).toBe(6);
|
||||
assert.strictEqual(covered.min, 6);
|
||||
covered.add(10);
|
||||
expect(covered.min).toBe(10);
|
||||
assert.strictEqual(covered.min, 10);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,66 +1,64 @@
|
|||
import { rateLimit } from "./rate-limit";
|
||||
import { jest } from "@jest/globals";
|
||||
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||
import assert from "node:assert";
|
||||
|
||||
describe("rateLimit", () => {
|
||||
beforeEach(() => {
|
||||
jest.useFakeTimers();
|
||||
mock.timers.enable({ apis: ["setTimeout"] });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.useRealTimers();
|
||||
mock.timers.reset();
|
||||
});
|
||||
|
||||
it("should call the function immediately on first invocation", async () => {
|
||||
const mockFn = jest
|
||||
.fn<() => Promise<string>>()
|
||||
.mockResolvedValue("result");
|
||||
const mockFn = mock.fn<() => Promise<string>>();
|
||||
mockFn.mock.mockImplementation(async () => "result");
|
||||
const rateLimited = rateLimit(mockFn, 100);
|
||||
|
||||
const promise = rateLimited();
|
||||
expect(mockFn).toHaveBeenCalledTimes(1);
|
||||
assert.strictEqual(mockFn.mock.callCount(), 1);
|
||||
|
||||
await promise;
|
||||
});
|
||||
|
||||
it("should call the function again after the interval has passed", async () => {
|
||||
const mockFn = jest
|
||||
.fn<(value: number) => Promise<string>>()
|
||||
.mockResolvedValue("result");
|
||||
const mockFn = mock.fn<(value: number) => Promise<string>>();
|
||||
mockFn.mock.mockImplementation(async () => "result");
|
||||
|
||||
const rateLimited = rateLimit(mockFn, 100);
|
||||
|
||||
const promise1 = rateLimited(1);
|
||||
await promise1;
|
||||
|
||||
jest.advanceTimersByTime(200);
|
||||
mock.timers.tick(200);
|
||||
|
||||
const promise2 = rateLimited(2);
|
||||
await promise2;
|
||||
|
||||
expect(mockFn).toHaveBeenCalledTimes(2);
|
||||
expect(mockFn).toHaveBeenCalledWith(2);
|
||||
assert.strictEqual(mockFn.mock.callCount(), 2);
|
||||
assert.deepStrictEqual(mockFn.mock.calls[1].arguments, [2]);
|
||||
});
|
||||
|
||||
it("should use the most recent arguments if multiple calls are made within interval", async () => {
|
||||
const mockFn = jest
|
||||
.fn<(value: string) => Promise<string>>()
|
||||
.mockImplementation(async (val) => `${val}-result`);
|
||||
const mockFn = mock.fn<(value: string) => Promise<string>>();
|
||||
mockFn.mock.mockImplementation(async (val: string) => `${val}-result`);
|
||||
const rateLimited = rateLimit(mockFn, 100);
|
||||
|
||||
const promise1 = rateLimited("first");
|
||||
jest.advanceTimersByTime(10);
|
||||
mock.timers.tick(10);
|
||||
const promise2 = rateLimited("second");
|
||||
jest.advanceTimersByTime(10);
|
||||
mock.timers.tick(10);
|
||||
const promise3 = rateLimited("third");
|
||||
|
||||
jest.advanceTimersByTime(1000);
|
||||
mock.timers.tick(1000);
|
||||
|
||||
expect(await promise1).toEqual("first-result");
|
||||
expect(await promise2).toEqual("third-result");
|
||||
expect(await promise3).toBeUndefined();
|
||||
assert.strictEqual(await promise1, "first-result");
|
||||
assert.strictEqual(await promise2, "third-result");
|
||||
assert.strictEqual(await promise3, undefined);
|
||||
|
||||
expect(mockFn).toHaveBeenCalledTimes(2);
|
||||
expect(mockFn).toHaveBeenNthCalledWith(1, "first");
|
||||
expect(mockFn).toHaveBeenNthCalledWith(2, "third");
|
||||
assert.strictEqual(mockFn.mock.callCount(), 2);
|
||||
assert.deepStrictEqual(mockFn.mock.calls[0].arguments, ["first"]);
|
||||
assert.deepStrictEqual(mockFn.mock.calls[1].arguments, ["third"]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@
|
|||
"allowSyntheticDefaultImports": true,
|
||||
"moduleResolution": "bundler",
|
||||
"lib": [
|
||||
"DOM" // to get `fetch` & `WebSocket`
|
||||
"DOM", // to get `fetch` & `WebSocket`
|
||||
"ES2024"
|
||||
],
|
||||
"declaration": true,
|
||||
"declarationDir": "./dist/types"
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
module.exports = {
|
||||
preset: "ts-jest"
|
||||
};
|
||||
|
|
@ -8,17 +8,18 @@
|
|||
"scripts": {
|
||||
"dev": "webpack watch --mode development",
|
||||
"build": "webpack --mode production",
|
||||
"test": "jest"
|
||||
"test": "tsx --test src/**/*.test.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.15.30",
|
||||
"bufferutil": "^4.0.9",
|
||||
"sync-client": "file:../sync-client",
|
||||
"ts-loader": "^9.5.2",
|
||||
"tslib": "2.8.1",
|
||||
"tsx": "^4.20.5",
|
||||
"typescript": "5.8.3",
|
||||
"uuid": "^11.1.0",
|
||||
"webpack": "^5.99.9",
|
||||
"webpack-cli": "^6.0.1",
|
||||
"bufferutil": "^4.0.9"
|
||||
"webpack-cli": "^6.0.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { randomCasing } from "./random-casing";
|
||||
|
||||
describe("randomCasing", () => {
|
||||
|
|
@ -5,7 +7,7 @@ describe("randomCasing", () => {
|
|||
const input =
|
||||
"hello, this is a really long string with a lot of characters";
|
||||
const result = randomCasing(input);
|
||||
expect(result.toLowerCase()).toBe(input.toLowerCase());
|
||||
expect(result).not.toBe(input);
|
||||
assert.strictEqual(result.toLowerCase(), input.toLowerCase());
|
||||
assert.notStrictEqual(result, input);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
"esModuleInterop": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"ESNext"
|
||||
"ES2024",
|
||||
],
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -4,17 +4,17 @@ set -e
|
|||
|
||||
echo "Running checks in sync-server"
|
||||
cd sync-server
|
||||
cargo test --verbose
|
||||
cargo clippy --all-targets --all-features
|
||||
cargo fmt --all -- --check
|
||||
cargo machete
|
||||
cargo test --verbose
|
||||
|
||||
echo "Running checks in frontend"
|
||||
cd ../frontend
|
||||
npm ci
|
||||
npm run build
|
||||
npm run lint
|
||||
npm run test
|
||||
npm run lint
|
||||
|
||||
if [[ $(git status --porcelain) ]]; then
|
||||
git status --porcelain
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue