Migrate to node:test

This commit is contained in:
Andras Schmelczer 2025-08-30 09:58:55 +01:00
parent 7aab7b05d6
commit bbbfc1a059
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
24 changed files with 759 additions and 6421 deletions

View file

@ -20,7 +20,14 @@ export default [
"no-unused-vars": "off", "no-unused-vars": "off",
"@typescript-eslint/restrict-template-expressions": "off", "@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/no-unused-vars": "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/parameter-properties": "off",
"@typescript-eslint/require-await": "off", "@typescript-eslint/require-await": "off",
"@typescript-eslint/class-methods-use-this": "off", "@typescript-eslint/class-methods-use-this": "off",

View file

@ -1,3 +0,0 @@
module.exports = {
preset: "ts-jest"
};

View file

@ -6,35 +6,33 @@
"scripts": { "scripts": {
"dev": "webpack watch --mode development", "dev": "webpack watch --mode development",
"build": "webpack --mode production", "build": "webpack --mode production",
"test": "jest", "test": "tsx --test src/**/*.test.ts",
"version": "node version-bump.mjs" "version": "node version-bump.mjs"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/jest": "^30.0.0",
"@types/node": "^22.15.30", "@types/node": "^22.15.30",
"css-loader": "^7.1.2", "css-loader": "^7.1.2",
"date-fns": "^4.1.0", "date-fns": "^4.1.0",
"file-loader": "^6.2.0", "file-loader": "^6.2.0",
"fs-extra": "^11.3.0", "fs-extra": "^11.3.0",
"jest": "^30.1.1",
"mini-css-extract-plugin": "^2.9.2", "mini-css-extract-plugin": "^2.9.2",
"obsidian": "1.8.7", "obsidian": "1.8.7",
"reconcile-text": "^0.5.0",
"resolve-url-loader": "^5.0.0", "resolve-url-loader": "^5.0.0",
"sass": "^1.91.0", "sass": "^1.91.0",
"sass-loader": "^16.0.5", "sass-loader": "^16.0.5",
"sync-client": "file:../sync-client", "sync-client": "file:../sync-client",
"terser-webpack-plugin": "^5.3.14", "terser-webpack-plugin": "^5.3.14",
"ts-jest": "^29.4.1",
"ts-loader": "^9.5.2", "ts-loader": "^9.5.2",
"tslib": "2.8.1", "tslib": "2.8.1",
"tsx": "^4.20.5",
"typescript": "5.8.3", "typescript": "5.8.3",
"url": "^0.11.4", "url": "^0.11.4",
"virtual-scroller": "^1.13.1", "virtual-scroller": "^1.13.1",
"webpack": "^5.99.9", "webpack": "^5.99.9",
"webpack-cli": "^6.0.1", "webpack-cli": "^6.0.1"
"reconcile-text": "^0.5.0"
} }
} }

View file

@ -1,42 +1,44 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { lineAndColumnToPosition } from "./line-and-column-to-position"; import { lineAndColumnToPosition } from "./line-and-column-to-position";
describe("lineAndColumnToPosition", () => { describe("lineAndColumnToPosition", () => {
it("should return the correct position for the first line", () => { it("should return the correct position for the first line", () => {
const text = "Hello\nWorld"; const text = "Hello\nWorld";
const position = lineAndColumnToPosition(text, 0, 3); const position = lineAndColumnToPosition(text, 0, 3);
expect(position).toBe(3); assert.strictEqual(position, 3);
}); });
it("should return the correct position for the second line", () => { it("should return the correct position for the second line", () => {
const text = "Hello\nWorld"; const text = "Hello\nWorld";
const position = lineAndColumnToPosition(text, 1, 2); const position = lineAndColumnToPosition(text, 1, 2);
expect(position).toBe(8); assert.strictEqual(position, 8);
}); });
it("should return the correct position for an empty string", () => { it("should return the correct position for an empty string", () => {
const text = ""; const text = "";
const position = lineAndColumnToPosition(text, 0, 0); const position = lineAndColumnToPosition(text, 0, 0);
expect(position).toBe(0); assert.strictEqual(position, 0);
}); });
it("with carrige return", () => { it("with carrige return", () => {
expect(lineAndColumnToPosition("a\nb", 1, 1)).toBe(3); assert.strictEqual(lineAndColumnToPosition("a\nb", 1, 1), 3);
expect(lineAndColumnToPosition("a\r\nb", 1, 1)).toBe(3); assert.strictEqual(lineAndColumnToPosition("a\r\nb", 1, 1), 3);
}); });
it("should handle multi-line strings with varying lengths", () => { it("should handle multi-line strings with varying lengths", () => {
const text = "Line1\nLongerLine2\nShort3"; const text = "Line1\nLongerLine2\nShort3";
const position = lineAndColumnToPosition(text, 2, 4); 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", () => { it("should throw an error if the line number is out of range", () => {
const text = "Line1\nLine2"; 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", () => { it("should throw an error if the column number is out of range", () => {
const text = "Line1\nLine2"; const text = "Line1\nLine2";
expect(() => lineAndColumnToPosition(text, 1, 10)).toThrow(); assert.throws(() => lineAndColumnToPosition(text, 1, 10));
}); });
}); });

View file

@ -1,53 +1,58 @@
import { describe, test } from "node:test";
import assert from "node:assert";
import { positionToLineAndColumn } from "./position-to-line-and-column"; import { positionToLineAndColumn } from "./position-to-line-and-column";
describe("positionToLineAndColumn", () => { describe("positionToLineAndColumn", () => {
test("converts position to line and column in multi-line text", () => { test("converts position to line and column in multi-line text", () => {
const text = "ab\ncd\n"; const text = "ab\ncd\n";
expect(positionToLineAndColumn(text, 0)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn(text, 0), {
line: 0, line: 0,
column: 0 column: 0
}); });
expect(positionToLineAndColumn(text, 1)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn(text, 1), {
line: 0, line: 0,
column: 1 column: 1
}); });
expect(positionToLineAndColumn(text, 2)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn(text, 2), {
line: 0, line: 0,
column: 2 column: 2
}); });
expect(positionToLineAndColumn(text, 3)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
line: 1, line: 1,
column: 0 column: 0
}); });
expect(positionToLineAndColumn(text, 4)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn(text, 4), {
line: 1, line: 1,
column: 1 column: 1
}); });
expect(positionToLineAndColumn(text, 6)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn(text, 6), {
line: 2, line: 2,
column: 0 column: 0
}); });
}); });
test("with carrige returns", () => { test("with carrige returns", () => {
expect(positionToLineAndColumn("a\nb", 3)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn("a\nb", 3), {
line: 1, line: 1,
column: 1 column: 1
}); });
expect(positionToLineAndColumn("a\r\nb", 3)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn("a\r\nb", 3), {
line: 1, line: 1,
column: 1 column: 1
}); });
}); });
test("handles empty input", () => { 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", () => { test("handles positions at the end of text", () => {
const text = "End"; const text = "End";
expect(positionToLineAndColumn(text, 3)).toEqual({ assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
line: 0, line: 0,
column: 3 column: 3
}); });
@ -55,7 +60,7 @@ describe("positionToLineAndColumn", () => {
test("throws error for position out of range", () => { test("throws error for position out of range", () => {
const text = "Short text"; const text = "Short text";
expect(() => positionToLineAndColumn(text, 15)).toThrow(); assert.throws(() => positionToLineAndColumn(text, 15));
expect(() => positionToLineAndColumn(text, -1)).toThrow(); assert.throws(() => positionToLineAndColumn(text, -1));
}); });
}); });

View file

@ -8,7 +8,7 @@
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"lib": [ "lib": [
"DOM", "DOM",
"ESNext" "ES2024"
] ]
}, },
"exclude": [ "exclude": [

File diff suppressed because it is too large Load diff

View file

@ -1,3 +0,0 @@
module.exports = {
preset: "ts-jest"
};

View file

@ -10,22 +10,20 @@
"scripts": { "scripts": {
"dev": "webpack watch --mode development", "dev": "webpack watch --mode development",
"build": "webpack --mode production", "build": "webpack --mode production",
"test": "jest" "test": "tsx --test src/**/*.test.ts"
}, },
"dependencies": { "dependencies": {
"byte-base64": "^1.1.0", "byte-base64": "^1.1.0",
"minimatch": "^10.0.1", "minimatch": "^10.0.1",
"p-queue": "^8.1.0", "p-queue": "^8.1.0",
"uuid": "^11.1.0", "reconcile-text": "^0.5.0",
"reconcile-text": "^0.5.0" "uuid": "^11.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^30.0.0",
"@types/node": "^22.15.30", "@types/node": "^22.15.30",
"jest": "^30.1.1",
"ts-jest": "^29.4.1",
"ts-loader": "^9.5.2", "ts-loader": "^9.5.2",
"tslib": "2.8.1", "tslib": "2.8.1",
"tsx": "^4.20.5",
"typescript": "5.8.3", "typescript": "5.8.3",
"webpack": "^5.99.9", "webpack": "^5.99.9",
"webpack-cli": "^6.0.1", "webpack-cli": "^6.0.1",

View file

@ -1,3 +1,4 @@
import { describe, it } from "node:test";
import type { import type {
Database, Database,
DocumentRecord, DocumentRecord,

View file

@ -70,7 +70,10 @@ export class SyncService {
formData.append("document_id", documentId); formData.append("document_id", documentId);
} }
formData.append("relative_path", relativePath); 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"), { const response = await this.client(this.getUrl("/documents"), {
method: "POST", method: "POST",
@ -117,7 +120,10 @@ export class SyncService {
const formData = new FormData(); const formData = new FormData();
formData.append("parent_version_id", parentVersionId.toString()); formData.append("parent_version_id", parentVersionId.toString());
formData.append("relative_path", relativePath); formData.append("relative_path", relativePath);
formData.append("content", new Blob([contentBytes])); formData.append(
"content",
new Blob([new Uint8Array(contentBytes)])
);
const response = await this.client( const response = await this.client(
this.getUrl(`/documents/${documentId}`), this.getUrl(`/documents/${documentId}`),

View file

@ -1,7 +1,7 @@
import * as assert from "assert"; import assert from "node:assert";
export function assertSetContainsExactly<T>(set: Set<T>, ...values: T[]): void { export function assertSetContainsExactly<T>(set: Set<T>, ...values: T[]): void {
assert( assert.ok(
set.size === values.length && set.size === values.length &&
Array.from(set).every((value) => values.includes(value)), Array.from(set).every((value) => values.includes(value)),
`Expected set to contain only ${values.map((v) => '"' + v + '"').join(", ")}, but it contained ${Array.from( `Expected set to contain only ${values.map((v) => '"' + v + '"').join(", ")}, but it contained ${Array.from(

View file

@ -1,3 +1,5 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { Logger } from "../tracing/logger"; import { Logger } from "../tracing/logger";
import { globsToRegexes } from "./globs-to-regexes"; import { globsToRegexes } from "./globs-to-regexes";
@ -5,7 +7,7 @@ describe("globsToRegexes", () => {
it("basicExample", async () => { it("basicExample", async () => {
const [regex] = globsToRegexes([".git/**"], new Logger()); const [regex] = globsToRegexes([".git/**"], new Logger());
expect(regex.test(".git/objects/object")).toBeTruthy(); assert.ok(regex.test(".git/objects/object"));
expect(regex.test(".git/objects/.object")).toBeTruthy(); assert.ok(regex.test(".git/objects/.object"));
}); });
}); });

View file

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

View file

@ -1,28 +1,42 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { isFileTypeMergable } from "./is-file-type-mergable"; import { isFileTypeMergable } from "./is-file-type-mergable";
describe("isFileTypeMergable", () => { describe("isFileTypeMergable", () => {
it("should return true for .md files", () => { it("should return true for .md files", () => {
expect(isFileTypeMergable(".md")).toBe(true); assert.strictEqual(isFileTypeMergable(".md"), true);
expect(isFileTypeMergable("hi.md")).toBe(true); assert.strictEqual(isFileTypeMergable("hi.md"), true);
expect(isFileTypeMergable("my/path/to/my/document.md")).toBe(true); assert.strictEqual(
isFileTypeMergable("my/path/to/my/document.md"),
true
);
}); });
it("should return true for .txt files", () => { it("should return true for .txt files", () => {
expect(isFileTypeMergable(".txt")).toBe(true); assert.strictEqual(isFileTypeMergable(".txt"), true);
expect(isFileTypeMergable("hi.txt")).toBe(true); assert.strictEqual(isFileTypeMergable("hi.txt"), true);
expect(isFileTypeMergable("my/path/to/my/document.txt")).toBe(true); assert.strictEqual(
isFileTypeMergable("my/path/to/my/document.txt"),
true
);
}); });
it("should be case insensitive", () => { it("should be case insensitive", () => {
expect(isFileTypeMergable("hi.MD")).toBe(true); assert.strictEqual(isFileTypeMergable("hi.MD"), true);
expect(isFileTypeMergable("my/path/to/my/DOCUMENT.MD")).toBe(true); assert.strictEqual(
expect(isFileTypeMergable("hi.TXT")).toBe(true); isFileTypeMergable("my/path/to/my/DOCUMENT.MD"),
expect(isFileTypeMergable("my/path/to/my/DOCUMENT.TXT")).toBe(true); 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", () => { it("should return false for non-mergable file types", () => {
expect(isFileTypeMergable(".json")).toBe(false); assert.strictEqual(isFileTypeMergable(".json"), false);
expect(isFileTypeMergable("HELLO.JSON")).toBe(false); assert.strictEqual(isFileTypeMergable("HELLO.JSON"), false);
expect(isFileTypeMergable("my/config.yml")).toBe(false); assert.strictEqual(isFileTypeMergable("my/config.yml"), false);
}); });
}); });

View file

@ -1,3 +1,5 @@
import { describe, it, beforeEach } from "node:test";
import assert from "node:assert";
import { Logger } from "../tracing/logger"; import { Logger } from "../tracing/logger";
import type { RelativePath } from "../persistence/database"; import type { RelativePath } from "../persistence/database";
import { Locks } from "./locks"; import { Locks } from "./locks";
@ -14,18 +16,18 @@ describe("withLock", () => {
locks = new Locks<RelativePath>(logger); 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; let executionCount = 0;
const result = await locks.withLock(testPath, () => { const result = await locks.withLock(testPath, () => {
executionCount++; executionCount++;
return "success"; return "success";
}); });
expect(result).toBe("success"); assert.strictEqual(result, "success");
expect(executionCount).toBe(1); 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; let executionCount = 0;
const result = await locks.withLock(testPath, async () => { const result = await locks.withLock(testPath, async () => {
executionCount++; executionCount++;
@ -33,22 +35,22 @@ describe("withLock", () => {
return "async-success"; return "async-success";
}); });
expect(result).toBe("async-success"); assert.strictEqual(result, "async-success");
expect(executionCount).toBe(1); 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; let executionCount = 0;
const result = await locks.withLock([testPath, testPath2], () => { const result = await locks.withLock([testPath, testPath2], () => {
executionCount++; executionCount++;
return "multi-success"; return "multi-success";
}); });
expect(result).toBe("multi-success"); assert.strictEqual(result, "multi-success");
expect(executionCount).toBe(1); 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[] = []; const executionOrder: string[] = [];
// Start two concurrent operations with keys in different orders // Start two concurrent operations with keys in different orders
@ -68,10 +70,10 @@ describe("withLock", () => {
const [result1, result2] = await Promise.all([promise1, promise2]); const [result1, result2] = await Promise.all([promise1, promise2]);
expect(result1).toBe("result1"); assert.strictEqual(result1, "result1");
expect(result2).toBe("result2"); assert.strictEqual(result2, "result2");
// One operation should complete entirely before the other starts // One operation should complete entirely before the other starts
expect(executionOrder).toEqual([ assert.deepStrictEqual(executionOrder, [
"operation1-start", "operation1-start",
"operation1-end", "operation1-end",
"operation2-start", "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 executionOrder: string[] = [];
const promise1 = locks.withLock(testPath, async () => { const promise1 = locks.withLock(testPath, async () => {
@ -98,9 +100,9 @@ describe("withLock", () => {
const [result1, result2] = await Promise.all([promise1, promise2]); const [result1, result2] = await Promise.all([promise1, promise2]);
expect(result1).toBe("result1"); assert.strictEqual(result1, "result1");
expect(result2).toBe("result2"); assert.strictEqual(result2, "result2");
expect(executionOrder).toEqual([ assert.deepStrictEqual(executionOrder, [
"operation1-start", "operation1-start",
"operation1-end", "operation1-end",
"operation2-start", "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 executionOrder: string[] = [];
const promise1 = locks.withLock(testPath, async () => { const promise1 = locks.withLock(testPath, async () => {
@ -127,54 +129,56 @@ describe("withLock", () => {
const [result1, result2] = await Promise.all([promise1, promise2]); const [result1, result2] = await Promise.all([promise1, promise2]);
expect(result1).toBe("result1"); assert.strictEqual(result1, "result1");
expect(result2).toBe("result2"); assert.strictEqual(result2, "result2");
// Both operations should run concurrently // Both operations should run concurrently
expect(executionOrder[0]).toBe("operation1-start"); assert.strictEqual(executionOrder[0], "operation1-start");
expect(executionOrder[1]).toBe("operation2-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"); const error = new Error("test error");
await expect( await assert.rejects(
locks.withLock(testPath, () => { locks.withLock(testPath, () => {
throw error; throw error;
}) }),
).rejects.toThrow("test error"); { message: "test error" }
);
// Lock should be released, allowing another operation // Lock should be released, allowing another operation
const result = await locks.withLock( const result = await locks.withLock(
testPath, testPath,
() => "success-after-error" () => "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"); const error = new Error("async test error");
await expect( await assert.rejects(
locks.withLock(testPath, async () => { locks.withLock(testPath, async () => {
await new Promise((resolve) => setTimeout(resolve, 10)); await new Promise((resolve) => setTimeout(resolve, 10));
throw error; throw error;
}) }),
).rejects.toThrow("async test error"); { message: "async test error" }
);
// Lock should be released, allowing another operation // Lock should be released, allowing another operation
const result = await locks.withLock( const result = await locks.withLock(
testPath, testPath,
() => "success-after-async-error" () => "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"); 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[] = []; const executionOrder: string[] = [];
// Start first operation that holds the lock // Start first operation that holds the lock
@ -209,10 +213,10 @@ describe("withLock", () => {
thirdPromise thirdPromise
]); ]);
expect(first).toBe("first"); assert.strictEqual(first, "first");
expect(second).toBe("second"); assert.strictEqual(second, "second");
expect(third).toBe("third"); assert.strictEqual(third, "third");
expect(executionOrder).toEqual([ assert.deepStrictEqual(executionOrder, [
"first-start", "first-start",
"first-end", "first-end",
"second-start", "second-start",

View file

@ -1,60 +1,62 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { CoveredValues } from "./min-covered"; import { CoveredValues } from "./min-covered";
describe("CoveredValues", () => { describe("CoveredValues", () => {
test("should initialize with the given min value", () => { it("should initialize with the given min value", () => {
const covered = new CoveredValues(5); 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); const covered = new CoveredValues(0);
covered.add(3); covered.add(3);
expect(covered.min).toBe(0); assert.strictEqual(covered.min, 0);
covered.add(1); covered.add(1);
expect(covered.min).toBe(1); assert.strictEqual(covered.min, 1);
covered.add(4); covered.add(4);
expect(covered.min).toBe(1); assert.strictEqual(covered.min, 1);
covered.add(2); 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); const covered = new CoveredValues(0);
covered.add(3); covered.add(3);
covered.add(3); covered.add(3);
covered.add(3); covered.add(3);
expect(covered.min).toBe(0); assert.strictEqual(covered.min, 0);
covered.add(1); covered.add(1);
covered.add(2); 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); const covered = new CoveredValues(132);
for (let i = 250; i > 132; i--) { for (let i = 250; i > 132; i--) {
expect(covered.min).toBe(132); assert.strictEqual(covered.min, 132);
covered.add(i); 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); const covered = new CoveredValues(5);
covered.add(3); covered.add(3);
expect(covered.min).toBe(5); assert.strictEqual(covered.min, 5);
covered.add(6); 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); const covered = new CoveredValues(5);
covered.add(7); covered.add(7);
covered.add(8); covered.add(8);
covered.add(9); covered.add(9);
expect(covered.min).toBe(5); assert.strictEqual(covered.min, 5);
covered.min = 6; covered.min = 6;
expect(covered.min).toBe(6); assert.strictEqual(covered.min, 6);
covered.add(10); covered.add(10);
expect(covered.min).toBe(10); assert.strictEqual(covered.min, 10);
}); });
}); });

View file

@ -1,66 +1,64 @@
import { rateLimit } from "./rate-limit"; 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", () => { describe("rateLimit", () => {
beforeEach(() => { beforeEach(() => {
jest.useFakeTimers(); mock.timers.enable({ apis: ["setTimeout"] });
}); });
afterEach(() => { afterEach(() => {
jest.useRealTimers(); mock.timers.reset();
}); });
it("should call the function immediately on first invocation", async () => { it("should call the function immediately on first invocation", async () => {
const mockFn = jest const mockFn = mock.fn<() => Promise<string>>();
.fn<() => Promise<string>>() mockFn.mock.mockImplementation(async () => "result");
.mockResolvedValue("result");
const rateLimited = rateLimit(mockFn, 100); const rateLimited = rateLimit(mockFn, 100);
const promise = rateLimited(); const promise = rateLimited();
expect(mockFn).toHaveBeenCalledTimes(1); assert.strictEqual(mockFn.mock.callCount(), 1);
await promise; await promise;
}); });
it("should call the function again after the interval has passed", async () => { it("should call the function again after the interval has passed", async () => {
const mockFn = jest const mockFn = mock.fn<(value: number) => Promise<string>>();
.fn<(value: number) => Promise<string>>() mockFn.mock.mockImplementation(async () => "result");
.mockResolvedValue("result");
const rateLimited = rateLimit(mockFn, 100); const rateLimited = rateLimit(mockFn, 100);
const promise1 = rateLimited(1); const promise1 = rateLimited(1);
await promise1; await promise1;
jest.advanceTimersByTime(200); mock.timers.tick(200);
const promise2 = rateLimited(2); const promise2 = rateLimited(2);
await promise2; await promise2;
expect(mockFn).toHaveBeenCalledTimes(2); assert.strictEqual(mockFn.mock.callCount(), 2);
expect(mockFn).toHaveBeenCalledWith(2); assert.deepStrictEqual(mockFn.mock.calls[1].arguments, [2]);
}); });
it("should use the most recent arguments if multiple calls are made within interval", async () => { it("should use the most recent arguments if multiple calls are made within interval", async () => {
const mockFn = jest const mockFn = mock.fn<(value: string) => Promise<string>>();
.fn<(value: string) => Promise<string>>() mockFn.mock.mockImplementation(async (val: string) => `${val}-result`);
.mockImplementation(async (val) => `${val}-result`);
const rateLimited = rateLimit(mockFn, 100); const rateLimited = rateLimit(mockFn, 100);
const promise1 = rateLimited("first"); const promise1 = rateLimited("first");
jest.advanceTimersByTime(10); mock.timers.tick(10);
const promise2 = rateLimited("second"); const promise2 = rateLimited("second");
jest.advanceTimersByTime(10); mock.timers.tick(10);
const promise3 = rateLimited("third"); const promise3 = rateLimited("third");
jest.advanceTimersByTime(1000); mock.timers.tick(1000);
expect(await promise1).toEqual("first-result"); assert.strictEqual(await promise1, "first-result");
expect(await promise2).toEqual("third-result"); assert.strictEqual(await promise2, "third-result");
expect(await promise3).toBeUndefined(); assert.strictEqual(await promise3, undefined);
expect(mockFn).toHaveBeenCalledTimes(2); assert.strictEqual(mockFn.mock.callCount(), 2);
expect(mockFn).toHaveBeenNthCalledWith(1, "first"); assert.deepStrictEqual(mockFn.mock.calls[0].arguments, ["first"]);
expect(mockFn).toHaveBeenNthCalledWith(2, "third"); assert.deepStrictEqual(mockFn.mock.calls[1].arguments, ["third"]);
}); });
}); });

View file

@ -6,7 +6,8 @@
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"moduleResolution": "bundler", "moduleResolution": "bundler",
"lib": [ "lib": [
"DOM" // to get `fetch` & `WebSocket` "DOM", // to get `fetch` & `WebSocket`
"ES2024"
], ],
"declaration": true, "declaration": true,
"declarationDir": "./dist/types" "declarationDir": "./dist/types"

View file

@ -1,3 +0,0 @@
module.exports = {
preset: "ts-jest"
};

View file

@ -8,17 +8,18 @@
"scripts": { "scripts": {
"dev": "webpack watch --mode development", "dev": "webpack watch --mode development",
"build": "webpack --mode production", "build": "webpack --mode production",
"test": "jest" "test": "tsx --test src/**/*.test.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^22.15.30", "@types/node": "^22.15.30",
"bufferutil": "^4.0.9",
"sync-client": "file:../sync-client", "sync-client": "file:../sync-client",
"ts-loader": "^9.5.2", "ts-loader": "^9.5.2",
"tslib": "2.8.1", "tslib": "2.8.1",
"tsx": "^4.20.5",
"typescript": "5.8.3", "typescript": "5.8.3",
"uuid": "^11.1.0", "uuid": "^11.1.0",
"webpack": "^5.99.9", "webpack": "^5.99.9",
"webpack-cli": "^6.0.1", "webpack-cli": "^6.0.1"
"bufferutil": "^4.0.9"
} }
} }

View file

@ -1,3 +1,5 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { randomCasing } from "./random-casing"; import { randomCasing } from "./random-casing";
describe("randomCasing", () => { describe("randomCasing", () => {
@ -5,7 +7,7 @@ describe("randomCasing", () => {
const input = const input =
"hello, this is a really long string with a lot of characters"; "hello, this is a really long string with a lot of characters";
const result = randomCasing(input); const result = randomCasing(input);
expect(result.toLowerCase()).toBe(input.toLowerCase()); assert.strictEqual(result.toLowerCase(), input.toLowerCase());
expect(result).not.toBe(input); assert.notStrictEqual(result, input);
}); });
}); });

View file

@ -7,7 +7,7 @@
"esModuleInterop": true, "esModuleInterop": true,
"lib": [ "lib": [
"DOM", "DOM",
"ESNext" "ES2024",
], ],
"moduleResolution": "node" "moduleResolution": "node"
}, },

View file

@ -4,17 +4,17 @@ set -e
echo "Running checks in sync-server" echo "Running checks in sync-server"
cd sync-server cd sync-server
cargo test --verbose
cargo clippy --all-targets --all-features cargo clippy --all-targets --all-features
cargo fmt --all -- --check cargo fmt --all -- --check
cargo machete cargo machete
cargo test --verbose
echo "Running checks in frontend" echo "Running checks in frontend"
cd ../frontend cd ../frontend
npm ci npm ci
npm run build npm run build
npm run lint
npm run test npm run test
npm run lint
if [[ $(git status --porcelain) ]]; then if [[ $(git status --porcelain) ]]; then
git status --porcelain git status --porcelain