Fix testing setup

This commit is contained in:
Andras Schmelczer 2025-03-22 11:53:20 +00:00
commit 7dcdc98b60
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -7,9 +7,10 @@ import { FileOperations } from "./file-operations";
import { Logger } from "../tracing/logger"; import { Logger } from "../tracing/logger";
import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly"; import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly";
import type { FileSystemOperations } from "./filesystem-operations"; import type { FileSystemOperations } from "./filesystem-operations";
import init, { base64ToBytes } from "sync_lib";
import fs from "fs";
describe("File operations", () => { class MockDatabase implements Partial<Database> {
class MockDatabase implements Partial<Database> {
public getLatestDocumentByRelativePath( public getLatestDocumentByRelativePath(
_find: RelativePath _find: RelativePath
): DocumentRecord | undefined { ): DocumentRecord | undefined {
@ -23,9 +24,9 @@ describe("File operations", () => {
): void { ): void {
// no-op // no-op
} }
} }
class FakeFileSystemOperations implements FileSystemOperations { class FakeFileSystemOperations implements FileSystemOperations {
public readonly names = new Set<string>(); public readonly names = new Set<string>();
public async listAllFiles(): Promise<RelativePath[]> { public async listAllFiles(): Promise<RelativePath[]> {
@ -68,33 +69,46 @@ describe("File operations", () => {
this.names.delete(oldPath); this.names.delete(oldPath);
this.names.add(newPath); this.names.add(newPath);
} }
} }
test("should deconflict renames", async () => { describe("File operations", () => {
const fs = new FakeFileSystemOperations(); beforeEach(async () => {
const wasmBin = fs.readFileSync(
"../../backend/sync_lib/pkg/sync_lib_bg.wasm"
);
await init({ module_or_path: wasmBin });
});
it("should deconflict renames", async () => {
const fileSystemOperations = new FakeFileSystemOperations();
const fileOperations = new FileOperations( const fileOperations = new FileOperations(
new Logger(), new Logger(),
new MockDatabase() as Database, // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion new MockDatabase() as Database, // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion
fs fileSystemOperations
); );
await fileOperations.create("a", new Uint8Array()); await fileOperations.create("a", new Uint8Array());
assertSetContainsExactly(fs.names, "a"); assertSetContainsExactly(fileSystemOperations.names, "a");
await fileOperations.move("a", "b"); await fileOperations.move("a", "b");
assertSetContainsExactly(fs.names, "b"); assertSetContainsExactly(fileSystemOperations.names, "b");
await fileOperations.create("c", new Uint8Array()); await fileOperations.create("c", new Uint8Array());
assertSetContainsExactly(fs.names, "b", "c"); assertSetContainsExactly(fileSystemOperations.names, "b", "c");
await fileOperations.move("c", "b"); await fileOperations.move("c", "b");
assertSetContainsExactly(fs.names, "b", "b (1)"); assertSetContainsExactly(fileSystemOperations.names, "b", "b (1)");
await fileOperations.create("c", new Uint8Array()); await fileOperations.create("c", new Uint8Array());
await fileOperations.move("c", "b"); await fileOperations.move("c", "b");
assertSetContainsExactly(fs.names, "b", "b (1)", "b (2)"); assertSetContainsExactly(
fileSystemOperations.names,
"b",
"b (1)",
"b (2)"
);
}); });
test("should deconflict renames with file extension", async () => { it("should deconflict renames with file extension", async () => {
const fs = new FakeFileSystemOperations(); const fs = new FakeFileSystemOperations();
const fileOperations = new FileOperations( const fileOperations = new FileOperations(
new Logger(), new Logger(),
@ -124,7 +138,7 @@ describe("File operations", () => {
); );
}); });
test("should deconflict renames with paths", async () => { it("should deconflict renames with paths", async () => {
const fs = new FakeFileSystemOperations(); const fs = new FakeFileSystemOperations();
const fileOperations = new FileOperations( const fileOperations = new FileOperations(
new Logger(), new Logger(),