Lint & format

This commit is contained in:
Andras Schmelczer 2025-02-22 17:25:26 +00:00
parent 27423bf3cd
commit ca225a71be
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
17 changed files with 94 additions and 83 deletions

View file

@ -1,14 +1,15 @@
import { choose } from "../utils/choose";
import { v4 as uuidv4 } from "uuid";
import { assert } from "../utils/assert";
import { LogLevel, SyncSettings } from "sync-client";
import type { SyncSettings } from "sync-client";
import { LogLevel } from "sync-client";
import { MockClient } from "./mock-client";
import chalk from "chalk";
import { sleep } from "../utils/sleep";
export class MockAgent extends MockClient {
private writtenContents: Array<string> = [];
private pendingActions: Array<Promise<unknown>> = [];
private readonly writtenContents: string[] = [];
private readonly pendingActions: Promise<unknown>[] = [];
public constructor(
globalFiles: Record<string, Uint8Array>,
@ -48,8 +49,8 @@ export class MockAgent extends MockClient {
}
public async act(): Promise<void> {
let options: Array<() => Promise<unknown>> = [
() => {
const options: (() => Promise<unknown>)[] = [
async (): Promise<unknown> => {
const file = this.getFileName();
this.client.logger.info(`Decided to create file ${file}`);
return this.create(
@ -57,7 +58,7 @@ export class MockAgent extends MockClient {
new TextEncoder().encode(this.getContent())
);
},
() => {
async (): Promise<unknown> => {
this.client.logger.info(
`Decided to change fetchChangesUpdateIntervalMs`
);
@ -66,21 +67,21 @@ export class MockAgent extends MockClient {
Math.random() * 1000
);
},
() => {
async (): Promise<unknown> => {
this.client.logger.info(`Decided to disable sync`);
return this.client.settings.setSetting("isSyncEnabled", false);
},
() => {
async (): Promise<unknown> => {
this.client.logger.info(`Decided to enable sync`);
return this.client.settings.setSetting("isSyncEnabled", true);
}
];
let files = await this.listAllFiles();
const files = await this.listAllFiles();
if (files.length > 0) {
options.push(
() => {
async (): Promise<unknown> => {
const file = choose(files);
const newName = this.getFileName();
@ -89,7 +90,7 @@ export class MockAgent extends MockClient {
);
return this.rename(file, newName);
},
() => {
async (): Promise<unknown> => {
const file = choose(files);
this.client.logger.info(`Decided to update file ${file}`);
@ -101,23 +102,13 @@ export class MockAgent extends MockClient {
);
if (this.doDeletes) {
options.push(() => this.delete(choose(files)));
options.push(async () => this.delete(choose(files)));
}
}
this.pendingActions.push(choose(options)());
}
private getContent() {
const uuid = uuidv4();
this.writtenContents.push(uuid);
return uuid;
}
private getFileName() {
return `${this.name}-${uuidv4()}.md`;
}
public async finish(): Promise<void> {
await Promise.all(this.pendingActions);
await this.client.settings.setSetting("isSyncEnabled", true);
@ -178,7 +169,7 @@ export class MockAgent extends MockClient {
`Content ${content} found in ${found.length} files`
);
const file = found[0];
const [file] = found;
assert(
new TextDecoder().decode(file).split(content).length === 2,
`Content ${content} found more than once in a file`
@ -186,4 +177,14 @@ export class MockAgent extends MockClient {
}
}
}
private getContent(): string {
const uuid = uuidv4();
this.writtenContents.push(uuid);
return uuid;
}
private getFileName(): string {
return `${this.name}-${uuidv4()}.md`;
}
}

View file

@ -1,9 +1,9 @@
import {
SyncClient,
import type {
RelativePath,
FileSystemOperations,
SyncSettings
} from "sync-client";
import { SyncClient } from "sync-client";
import { assert } from "../utils/assert";
export class MockClient implements FileSystemOperations {
@ -15,7 +15,7 @@ export class MockClient implements FileSystemOperations {
private readonly initialSettings: Partial<SyncSettings>
) {}
public async init() {
public async init(): Promise<void> {
let _data: unknown = "";
this.client = await SyncClient.create(this, {
@ -23,12 +23,14 @@ export class MockClient implements FileSystemOperations {
save: async (data: unknown) => void (_data = data)
});
Object.keys(this.initialSettings).forEach((key) => {
this.client.settings.setSetting(
key as keyof SyncSettings,
this.initialSettings[key as keyof SyncSettings]
);
});
await Promise.all(
Object.keys(this.initialSettings).map(async (key) => {
return this.client.settings.setSetting(
key as keyof SyncSettings,
this.initialSettings[key as keyof SyncSettings]
);
})
);
assert(
(await this.client.checkConnection()).isSuccessful,
@ -37,7 +39,7 @@ export class MockClient implements FileSystemOperations {
}
public async listAllFiles(): Promise<RelativePath[]> {
return Object.keys(this.localFiles) as RelativePath[];
return Object.keys(this.localFiles);
}
public async read(path: RelativePath): Promise<Uint8Array> {
@ -77,7 +79,9 @@ export class MockClient implements FileSystemOperations {
this.client.syncer.syncLocallyCreatedFile(path, new Date());
}
public async createDirectory(path: RelativePath): Promise<void> {}
public async createDirectory(path: RelativePath): Promise<void> {
// This doesn't mean anything in our virtual FS representation
}
public async atomicUpdateText(
path: RelativePath,

View file

@ -1,4 +1,4 @@
import { SyncSettings } from "sync-client";
import type { SyncSettings } from "sync-client";
import { MockAgent } from "./agent/mock-agent";
import { sleep } from "./utils/sleep";
import { v4 as uuidv4 } from "uuid";
@ -55,14 +55,14 @@ async function runTest(): Promise<void> {
)
];
await Promise.all(clients.map((client) => client.init()));
await Promise.all(clients.map(async (client) => client.init()));
for (let i = 0; i < iterations; i++) {
await Promise.all(clients.map((client) => client.act()));
await Promise.all(clients.map(async (client) => client.act()));
await sleep(100);
}
await Promise.all(clients.map((client) => client.finish()));
await Promise.all(clients.map(async (client) => client.finish()));
console.info("Agents finished successfully");
@ -83,4 +83,11 @@ async function runTest(): Promise<void> {
console.info("Test completed successfully");
}
runTest();
runTest()
.then(() => {
process.exit(0);
})
.catch((err: unknown) => {
console.error(err);
process.exit(1);
});

View file

@ -1,3 +1,3 @@
export function sleep(ms: number): Promise<void> {
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}