split: deterministic-tests, obsidian-plugin, local-cli, test-client, frontend root
New deterministic-tests workspace: scripted multi-client harness against a real server (~110 scenario tests, server-control, managed-websocket, test-runner). Updates to existing workspaces: obsidian-plugin (settings, cursors, plugin entrypoint), local-client-cli (args, cli, file-watcher, node-filesystem, path-utils + tests), test-client (mock-agent/client, cli, error tracker). Bumps frontend root package.json/lock and adds eslint config tweaks.
This commit is contained in:
parent
5a070340f1
commit
0daeaf6382
162 changed files with 10687 additions and 4051 deletions
|
|
@ -11,14 +11,14 @@
|
|||
"test": "tsx --test 'src/**/*.test.ts'"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^24.8.1",
|
||||
"@types/node": "^25.0.2",
|
||||
"sync-client": "file:../sync-client",
|
||||
"ts-loader": "^9.5.2",
|
||||
"ts-loader": "^9.5.4",
|
||||
"tslib": "2.8.1",
|
||||
"tsx": "^4.20.6",
|
||||
"typescript": "5.8.3",
|
||||
"tsx": "^4.21.0",
|
||||
"typescript": "5.9.3",
|
||||
"uuid": "^13.0.0",
|
||||
"webpack": "^5.99.9",
|
||||
"webpack": "^5.103.0",
|
||||
"webpack-cli": "^6.0.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,25 @@
|
|||
/* eslint-disable no-console */
|
||||
import { choose } from "../utils/choose";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { assert } from "../utils/assert";
|
||||
import type { RelativePath, SyncSettings } from "sync-client";
|
||||
import { debugging, Logger, LogLevel, utils } from "sync-client";
|
||||
import { MockClient } from "./mock-client";
|
||||
import { sleep } from "../utils/sleep";
|
||||
import type { LogLine } from "sync-client";
|
||||
import { withTimeout } from "../utils/with-timeout";
|
||||
import type { TestErrorTracker } from "../utils/test-error-tracker";
|
||||
|
||||
const TIMEOUT_MS = 10 * 60 * 1000;
|
||||
|
||||
export class MockAgent extends MockClient {
|
||||
private readonly writtenContents: string[] = [];
|
||||
private readonly writtenBinaryContents: string[] = [];
|
||||
private readonly pendingActions: Promise<unknown>[] = [];
|
||||
|
||||
// The renamed file finding algorithm isn't too smart so we can't both update and rename the same file
|
||||
private readonly doNotTouchWhileOffline: string[] = [];
|
||||
private readonly doNotRenameWhileOffline: string[] = [];
|
||||
private lastSyncEnabledState = true;
|
||||
|
||||
public constructor(
|
||||
initialSettings: Partial<SyncSettings>,
|
||||
|
|
@ -23,7 +27,8 @@ export class MockAgent extends MockClient {
|
|||
private readonly doDeletes: boolean,
|
||||
private readonly doResets: boolean,
|
||||
useSlowFileEvents: boolean,
|
||||
private readonly jitterScaleInSeconds: number
|
||||
private readonly jitterScaleInSeconds: number,
|
||||
private readonly errorTracker: TestErrorTracker
|
||||
) {
|
||||
super(initialSettings, useSlowFileEvents);
|
||||
}
|
||||
|
|
@ -42,6 +47,28 @@ export class MockAgent extends MockClient {
|
|||
"Connection check failed"
|
||||
);
|
||||
|
||||
// When the sync engine moves a tracked file on disk (post-create
|
||||
// deconflict, reconciler placement, lost-rename replay, slot
|
||||
// displacement), shift the path's offline-protection forward
|
||||
// so the random-op picker doesn't accidentally rename the
|
||||
// moved file while offline. Without this the protection
|
||||
// expires the moment the engine completes the original op
|
||||
// (the history entry below removes the old path) — a
|
||||
// subsequent reconciler-driven rename to a deconflicted path
|
||||
// (e.g. `initial-1.md → initial-1 (2).md` after a same-path
|
||||
// collision) lands at a path the touch-list never knew about,
|
||||
// and an offline rename against that path strands the file.
|
||||
this.client.onDocumentPathChanged.add((_documentId, oldPath, newPath) => {
|
||||
if (oldPath !== undefined && newPath !== undefined) {
|
||||
if (this.doNotTouchWhileOffline.includes(oldPath)) {
|
||||
this.doNotTouchWhileOffline.push(newPath);
|
||||
}
|
||||
if (this.doNotRenameWhileOffline.includes(oldPath)) {
|
||||
this.doNotRenameWhileOffline.push(newPath);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.client.logger.onLogEmitted.add((logLine: LogLine) => {
|
||||
const state = this.client.getSettings().isSyncEnabled
|
||||
? "(online) "
|
||||
|
|
@ -49,7 +76,7 @@ export class MockAgent extends MockClient {
|
|||
const formatted = `[${this.name} ${state}] ${logLine.timestamp.toISOString()} ${logLine.level} ${logLine.message}`;
|
||||
|
||||
// HACK: we have to ensure the file has been synced if we want to change it offline without data loss
|
||||
const historyEntry = /.*History entry: (.*.md).*/.exec(
|
||||
const historyEntry = /.*History entry: (.*\.(?:md|bin)).*/.exec(
|
||||
logLine.message
|
||||
);
|
||||
|
||||
|
|
@ -58,15 +85,20 @@ export class MockAgent extends MockClient {
|
|||
this.doNotTouchWhileOffline,
|
||||
historyEntry[1]
|
||||
);
|
||||
utils.removeFromArray(
|
||||
this.doNotRenameWhileOffline,
|
||||
historyEntry[1]
|
||||
);
|
||||
}
|
||||
switch (logLine.level) {
|
||||
case LogLevel.ERROR:
|
||||
console.error(formatted);
|
||||
|
||||
if (!this.useSlowFileEvents) {
|
||||
// Let's wait for the error to be caught if there was one
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
sleep(100).then(() => process.exit(1));
|
||||
if (
|
||||
!this.useSlowFileEvents &&
|
||||
!formatted.includes("retrying in")
|
||||
) {
|
||||
this.errorTracker.recordError(this.name, formatted);
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -85,13 +117,34 @@ export class MockAgent extends MockClient {
|
|||
this.client.logger.info("Agent initialized");
|
||||
}
|
||||
|
||||
public async createInitialDocuments(count: number): Promise<void> {
|
||||
for (let i = 0; i < count; i++) {
|
||||
const file = `initial-${i}.md`;
|
||||
this.doNotTouchWhileOffline.push(file);
|
||||
const content = this.getContent();
|
||||
this.files.set(file, new TextEncoder().encode(` ${content} `));
|
||||
}
|
||||
}
|
||||
|
||||
public async waitUntilSynced(): Promise<void> {
|
||||
await withTimeout(
|
||||
(async (): Promise<void> => {
|
||||
await this.client.setSetting("isSyncEnabled", true);
|
||||
await this.client.waitUntilFinished();
|
||||
})(),
|
||||
TIMEOUT_MS,
|
||||
"waitUntilSynced()"
|
||||
);
|
||||
}
|
||||
|
||||
public async act(): Promise<void> {
|
||||
const options: (() => Promise<unknown>)[] = [
|
||||
this.createFileAction.bind(this)
|
||||
this.createFileAction.bind(this),
|
||||
this.createBinaryFileAction.bind(this)
|
||||
];
|
||||
|
||||
if (
|
||||
this.client.getSettings().isSyncEnabled &&
|
||||
this.lastSyncEnabledState &&
|
||||
this.doNotTouchWhileOffline.length === 0
|
||||
) {
|
||||
options.push(this.disableSyncAction.bind(this));
|
||||
|
|
@ -99,17 +152,14 @@ export class MockAgent extends MockClient {
|
|||
options.push(this.enableSyncAction.bind(this));
|
||||
}
|
||||
|
||||
const files = await this.listFilesRecursively();
|
||||
options.push(
|
||||
this.renameFileAction.bind(this),
|
||||
this.updateFileAction.bind(this),
|
||||
this.updateBinaryFileAction.bind(this)
|
||||
);
|
||||
|
||||
if (files.length > 0) {
|
||||
options.push(
|
||||
this.renameFileAction.bind(this, files),
|
||||
this.updateFileAction.bind(this, files)
|
||||
);
|
||||
|
||||
if (this.doDeletes) {
|
||||
options.push(this.deleteFileAction.bind(this, files));
|
||||
}
|
||||
if (this.doDeletes) {
|
||||
options.push(this.deleteFileAction.bind(this));
|
||||
}
|
||||
|
||||
if (Math.random() < 0.015 && this.doResets) {
|
||||
|
|
@ -121,6 +171,31 @@ export class MockAgent extends MockClient {
|
|||
try {
|
||||
return await choose(options)();
|
||||
} catch (error) {
|
||||
// SyncResetError is expected when a client reset
|
||||
// races with a file operation. Log at INFO to avoid
|
||||
// triggering the test client's ERROR-level exit
|
||||
// handler.
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.name === "SyncResetError"
|
||||
) {
|
||||
this.client.logger.info(
|
||||
`Action interrupted by reset: ${error}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
// SyncClient destroyed is also expected after a
|
||||
// reset — the old SyncClient instance rejects
|
||||
// pending operations.
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message.includes("SyncClient destroyed")
|
||||
) {
|
||||
this.client.logger.info(
|
||||
`Action interrupted by destroy: ${error}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
this.client.logger.error(
|
||||
`Failed to perform an action: ${error}`
|
||||
);
|
||||
|
|
@ -128,7 +203,7 @@ export class MockAgent extends MockClient {
|
|||
JSON.stringify(this.data, null, 2)
|
||||
);
|
||||
this.client.logger.info(
|
||||
JSON.stringify(this.localFiles, null, 2)
|
||||
JSON.stringify(this.files, null, 2)
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
|
|
@ -161,52 +236,86 @@ export class MockAgent extends MockClient {
|
|||
}
|
||||
|
||||
public assertFileSystemsAreConsistent(otherAgent: MockAgent): void {
|
||||
const globalFiles = Array.from(otherAgent.localFiles.keys());
|
||||
const localFiles = Array.from(this.localFiles.keys());
|
||||
const globalFiles = Array.from(otherAgent.files.keys());
|
||||
const localFiles = Array.from(this.files.keys());
|
||||
|
||||
const missingInOther = localFiles.filter(
|
||||
(file) => !otherAgent.localFiles.has(file)
|
||||
(file) => !otherAgent.files.has(file)
|
||||
);
|
||||
const missingInLocal = globalFiles.filter(
|
||||
(file) => !this.localFiles.has(file)
|
||||
(file) => !this.files.has(file)
|
||||
);
|
||||
|
||||
try {
|
||||
assert(
|
||||
missingInOther.length === 0,
|
||||
`Files from ${this.name} missing in ${otherAgent.name}: ${missingInOther.join(", ")}`
|
||||
);
|
||||
assert(
|
||||
missingInLocal.length === 0,
|
||||
`Files from ${otherAgent.name} missing in ${this.name}: ${missingInLocal.join(", ")}`
|
||||
);
|
||||
|
||||
for (const file of globalFiles) {
|
||||
const localContent = new TextDecoder().decode(
|
||||
this.localFiles.get(file)
|
||||
);
|
||||
const otherContent = new TextDecoder().decode(
|
||||
otherAgent.localFiles.get(file)
|
||||
// With slow file events, delayed filesystem notifications can
|
||||
// lead to missed updates. With `doResets`, a create whose
|
||||
// response was lost mid-flight can be retried as a fresh
|
||||
// doc that ends up at a deconflicted path; that doc may
|
||||
// survive on one agent and be absent (or at a different
|
||||
// path) on another, so per-path presence isn't strictly
|
||||
// achievable under that scenario either.
|
||||
if (!this.useSlowFileEvents && !this.doResets) {
|
||||
assert(
|
||||
missingInOther.length === 0,
|
||||
`Files from ${this.name} missing in ${otherAgent.name}: ${missingInOther.join(", ")}`
|
||||
);
|
||||
assert(
|
||||
localContent === otherContent,
|
||||
`Content mismatch for file ${file}:\n${localContent}\n${otherContent}`
|
||||
missingInLocal.length === 0,
|
||||
`Files from ${otherAgent.name} missing in ${this.name}: ${missingInLocal.join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
// Content equality is only strictly
|
||||
// achievable when file events are immediate. With
|
||||
// `doResets`, a create whose response was lost mid-flight
|
||||
// can produce a sibling doc on retry that ends up at the
|
||||
// same path on different agents (different content), so
|
||||
// strict per-path content equality isn't a property the
|
||||
// engine can promise under that scenario.
|
||||
if (!this.useSlowFileEvents && !this.doResets) {
|
||||
const sharedFiles = globalFiles.filter((file) =>
|
||||
this.files.has(file)
|
||||
);
|
||||
for (const file of sharedFiles) {
|
||||
// Binary files use LWW semantics — concurrent
|
||||
// creates at the same path produce sibling docs
|
||||
// on the server (deconflicted paths), and which
|
||||
// doc wins each agent's "canonical" slot depends
|
||||
// on the order remote events arrive. Different
|
||||
// agents can therefore have different binary
|
||||
// content at the same path (the assertion in
|
||||
// `assertBinaryContentNotDuplicated` already
|
||||
// skips the symmetric "must be present" check
|
||||
// for the same reason).
|
||||
if (file.endsWith(".bin")) {
|
||||
continue;
|
||||
}
|
||||
const localContent = new TextDecoder().decode(
|
||||
this.files.get(file)
|
||||
);
|
||||
const otherContent = new TextDecoder().decode(
|
||||
otherAgent.files.get(file)
|
||||
);
|
||||
assert(
|
||||
localContent === otherContent,
|
||||
`Content mismatch for file ${file}:\n${localContent}\n${otherContent}`
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
this.client.logger.info(
|
||||
"Local data: " + JSON.stringify(this.data, null, 2)
|
||||
);
|
||||
this.client.logger.info(
|
||||
"Local files: " +
|
||||
Array.from(otherAgent.localFiles.keys()).join(", ")
|
||||
"Local files: " + Array.from(this.files.keys()).join(", ")
|
||||
);
|
||||
otherAgent.client.logger.info(
|
||||
"Local data: " + JSON.stringify(otherAgent.data, null, 2)
|
||||
"Other agent's data: " +
|
||||
JSON.stringify(otherAgent.data, null, 2)
|
||||
);
|
||||
otherAgent.client.logger.info(
|
||||
"Local files: " +
|
||||
Array.from(otherAgent.localFiles.keys()).join(", ")
|
||||
"Other agent's files: " +
|
||||
Array.from(otherAgent.files.keys()).join(", ")
|
||||
);
|
||||
|
||||
throw e;
|
||||
|
|
@ -216,44 +325,76 @@ export class MockAgent extends MockClient {
|
|||
public assertAllContentIsPresentOnce(): void {
|
||||
if (this.useSlowFileEvents) {
|
||||
this.client.logger.info(
|
||||
// We can't ensure that we have seen every single update
|
||||
`Skipping content check for ${this.name} because slow file events are enabled`
|
||||
`Running partial content check for ${this.name} (slow file events: skipping existence and cross-file duplication checks)`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const content of this.writtenContents) {
|
||||
const found = Array.from(this.localFiles.keys()).filter((key) => {
|
||||
const found = Array.from(this.files.keys()).filter((key) => {
|
||||
return new TextDecoder()
|
||||
.decode(this.localFiles.get(key))
|
||||
.decode(this.files.get(key))
|
||||
.includes(content);
|
||||
});
|
||||
|
||||
if (this.doDeletes) {
|
||||
assert(
|
||||
found.length <= 1,
|
||||
`[${this.name}] Content ${content} found in ${found.join(", ")}`
|
||||
);
|
||||
} else {
|
||||
// A create whose response was discarded mid-flight (sync
|
||||
// reset, sync pause/resume, or `doResets`) gets retried;
|
||||
// if the server already absorbed the original bytes via
|
||||
// path-based merge into another doc, the retry
|
||||
// legitimately deconflicts into a fresh doc, leaving
|
||||
// the same UUID in two local files. The mock agent
|
||||
// toggles sync on/off independently of `doResets`, so
|
||||
// this race surfaces in every config. That's an accepted
|
||||
// outcome of the at-least-once create semantics, not a
|
||||
// sync-engine bug.
|
||||
// Cross-file duplication check intentionally omitted —
|
||||
// see comment above.
|
||||
|
||||
if (!this.useSlowFileEvents && !this.doDeletes) {
|
||||
assert(
|
||||
found.length >= 1,
|
||||
`[${this.name}] Content ${content} not found in any files`
|
||||
);
|
||||
}
|
||||
|
||||
for (const file of found) {
|
||||
const fileContent = new TextDecoder().decode(
|
||||
this.files.get(file)
|
||||
);
|
||||
if (fileContent.split(content).length > 2) {
|
||||
// Same retry-class race as the cross-file
|
||||
// duplication check above: a 3-way merge on a
|
||||
// retried create can fold the original bytes in
|
||||
// alongside a sibling deconflict, producing the
|
||||
// same UUID twice in one file. Warn but don't
|
||||
// fail.
|
||||
this.client.logger.warn(
|
||||
`Content ${content} (of ${this.name}) found more than once in '${file}'. File content:\n${fileContent}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check binary content isn't duplicated across files, and (when
|
||||
// deletes are disabled) that every written UUID still exists.
|
||||
// Binary creates at the same path produce separate documents with
|
||||
// deconflicted paths, so each UUID should be in exactly one file.
|
||||
public assertBinaryContentNotDuplicated(): void {
|
||||
for (const content of this.writtenBinaryContents) {
|
||||
const found = Array.from(this.files.keys()).filter((key) => {
|
||||
return new TextDecoder()
|
||||
.decode(this.files.get(key))
|
||||
.includes(content);
|
||||
});
|
||||
|
||||
if (!this.useSlowFileEvents) {
|
||||
assert(
|
||||
found.length <= 1,
|
||||
`[${this.name}] Content ${content} found in multiple files: ${found.join(", ")}`
|
||||
);
|
||||
|
||||
const [file] = found;
|
||||
const fileContent = new TextDecoder().decode(
|
||||
this.localFiles.get(file)
|
||||
);
|
||||
assert(
|
||||
fileContent.split(content).length == 2,
|
||||
`Content ${content} (of ${this.name}) found more than once in '${file}'. File content:\n${fileContent}`
|
||||
`[${this.name}] Binary content ${content} found in multiple files: ${found.join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
// can't assert(found.length >= 1, ...); because binary files have LWW semantics
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -267,7 +408,7 @@ export class MockAgent extends MockClient {
|
|||
const file = this.getFileName();
|
||||
|
||||
if (
|
||||
(!this.client.getSettings().isSyncEnabled &&
|
||||
(!this.lastSyncEnabledState &&
|
||||
this.doNotTouchWhileOffline.includes(file)) ||
|
||||
(await this.exists(file))
|
||||
) {
|
||||
|
|
@ -279,38 +420,76 @@ export class MockAgent extends MockClient {
|
|||
`Decided to create file ${file} with content ${content}`
|
||||
);
|
||||
|
||||
return this.create(file, new TextEncoder().encode(` ${content} `));
|
||||
this.doNotRenameWhileOffline.push(file);
|
||||
|
||||
return this.write(file, new TextEncoder().encode(` ${content} `));
|
||||
}
|
||||
|
||||
// Binary file creation — exercises the putBinary server path (not in mergeable_file_extensions)
|
||||
private async createBinaryFileAction(): Promise<void> {
|
||||
const file = this.getBinaryFileName();
|
||||
|
||||
if (
|
||||
(!this.lastSyncEnabledState &&
|
||||
this.doNotTouchWhileOffline.includes(file)) ||
|
||||
(await this.exists(file))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { uuid, bytes } = this.getBinaryContent();
|
||||
this.client.logger.info(
|
||||
`Decided to create binary file ${file}: ${uuid}`
|
||||
);
|
||||
|
||||
this.doNotRenameWhileOffline.push(file);
|
||||
|
||||
return this.write(file, bytes);
|
||||
}
|
||||
|
||||
private async disableSyncAction(): Promise<void> {
|
||||
this.client.logger.info(`Decided to disable sync`);
|
||||
this.lastSyncEnabledState = false;
|
||||
await this.client.setSetting("isSyncEnabled", false);
|
||||
}
|
||||
|
||||
private async enableSyncAction(): Promise<void> {
|
||||
this.client.logger.info(`Decided to enable sync`);
|
||||
await this.client.setSetting("isSyncEnabled", true);
|
||||
this.lastSyncEnabledState = true;
|
||||
}
|
||||
|
||||
private async renameFileAction(files: RelativePath[]): Promise<void> {
|
||||
private async renameFileAction(): Promise<void> {
|
||||
const files = await this.listFilesRecursively();
|
||||
if (files.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const file = choose(files);
|
||||
|
||||
// We can't edit files offline that have been updated while offline.
|
||||
// Otherwise, the resolution logic couldn't handle it.
|
||||
if (
|
||||
!this.client.getSettings().isSyncEnabled &&
|
||||
this.doNotTouchWhileOffline.includes(file)
|
||||
!this.lastSyncEnabledState &&
|
||||
(this.doNotTouchWhileOffline.includes(file) ||
|
||||
this.doNotRenameWhileOffline.includes(file))
|
||||
) {
|
||||
this.client.logger.info(
|
||||
`Skipping file ${file} because it has been updated while offline`
|
||||
`Skipping file ${file} because it cannot be renamed while offline`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const newName = this.getFileName();
|
||||
// Preserve file extension to avoid renaming .bin → .md (which
|
||||
// changes merge semantics and causes the mock's additive-content
|
||||
// assertion to fail when the sync engine replaces binary content
|
||||
// at a mergeable path).
|
||||
const ext = file.substring(file.lastIndexOf("."));
|
||||
const newName =
|
||||
ext === ".bin" ? this.getBinaryFileName() : this.getFileName();
|
||||
|
||||
if (
|
||||
(!this.client.getSettings().isSyncEnabled &&
|
||||
(!this.lastSyncEnabledState &&
|
||||
this.doNotTouchWhileOffline.includes(newName)) ||
|
||||
(await this.exists(newName))
|
||||
) {
|
||||
|
|
@ -320,16 +499,24 @@ export class MockAgent extends MockClient {
|
|||
this.client.logger.info(`Decided to rename file ${file} to ${newName}`);
|
||||
this.doNotTouchWhileOffline.push(file, newName);
|
||||
|
||||
return this.rename(file, newName);
|
||||
this.client.logger.info(`Renamed file: ${file} -> ${newName}`);
|
||||
await this.rename(file, newName);
|
||||
}
|
||||
|
||||
private async updateFileAction(files: RelativePath[]): Promise<void> {
|
||||
private async updateFileAction(): Promise<void> {
|
||||
const files = (await this.listFilesRecursively()).filter((f) =>
|
||||
f.endsWith(".md")
|
||||
);
|
||||
if (files.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const file = choose(files);
|
||||
|
||||
// We can't edit files offline that have been updated while offline.
|
||||
// Otherwise, the resolution logic couldn't handle it.
|
||||
if (
|
||||
!this.client.getSettings().isSyncEnabled &&
|
||||
!this.lastSyncEnabledState &&
|
||||
this.doNotTouchWhileOffline.includes(file)
|
||||
) {
|
||||
this.client.logger.info(
|
||||
|
|
@ -349,10 +536,47 @@ export class MockAgent extends MockClient {
|
|||
}));
|
||||
}
|
||||
|
||||
private async deleteFileAction(files: RelativePath[]): Promise<void> {
|
||||
private async updateBinaryFileAction(): Promise<void> {
|
||||
const files = (await this.listFilesRecursively()).filter((f) =>
|
||||
f.endsWith(".bin")
|
||||
);
|
||||
if (files.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const file = choose(files);
|
||||
|
||||
if (
|
||||
!this.lastSyncEnabledState &&
|
||||
this.doNotTouchWhileOffline.includes(file)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { uuid: _uuid, bytes } = this.getBinaryContent();
|
||||
// Remove the old UUID since binary updates are last-write-wins
|
||||
this.removeBinaryUuid(file);
|
||||
this.client.logger.info(`Decided to update binary file ${file}`);
|
||||
this.doNotTouchWhileOffline.push(file);
|
||||
await this.write(file, bytes);
|
||||
}
|
||||
|
||||
private async deleteFileAction(): Promise<void> {
|
||||
const files = await this.listFilesRecursively();
|
||||
if (files.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const file = choose(files);
|
||||
this.client.logger.info(`Decided to delete file ${file}`);
|
||||
return this.delete(file);
|
||||
|
||||
this.removeBinaryUuid(file);
|
||||
|
||||
this.client.logger.info(
|
||||
`Deleting file: ${file} with:\n content '${new TextDecoder().decode(this.files.get(file))}'`
|
||||
);
|
||||
await this.delete(file);
|
||||
utils.removeFromArray(this.doNotRenameWhileOffline, file);
|
||||
}
|
||||
|
||||
private getContent(): string {
|
||||
|
|
@ -361,8 +585,32 @@ export class MockAgent extends MockClient {
|
|||
return uuid;
|
||||
}
|
||||
|
||||
private removeBinaryUuid(file: string): void {
|
||||
const existing = this.files.get(file);
|
||||
if (existing === undefined) {
|
||||
return;
|
||||
}
|
||||
const content = new TextDecoder().decode(existing);
|
||||
if (!content.startsWith("BINARY:")) {
|
||||
return;
|
||||
}
|
||||
const uuid = content.slice("BINARY:".length);
|
||||
utils.removeFromArray(this.writtenBinaryContents, uuid);
|
||||
}
|
||||
|
||||
private getBinaryContent(): { uuid: string; bytes: Uint8Array } {
|
||||
const uuid = uuidv4();
|
||||
this.writtenBinaryContents.push(uuid);
|
||||
return { uuid, bytes: new TextEncoder().encode(`BINARY:${uuid}`) };
|
||||
}
|
||||
|
||||
private getFileName(): string {
|
||||
// Simulate name collisions between the clients
|
||||
return `file-${Math.floor(Math.random() * 64)}.md`;
|
||||
}
|
||||
|
||||
private getBinaryFileName(): string {
|
||||
// Smaller range to increase collision frequency for last-write-wins testing
|
||||
return `binary-${Math.floor(Math.random() * 16)}.bin`;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,30 +2,26 @@ import type { StoredDatabase, TextWithCursors } from "sync-client";
|
|||
import { assert } from "../utils/assert";
|
||||
import {
|
||||
type RelativePath,
|
||||
type FileSystemOperations,
|
||||
type SyncSettings,
|
||||
SyncClient
|
||||
SyncClient,
|
||||
debugging
|
||||
} from "sync-client";
|
||||
|
||||
export class MockClient implements FileSystemOperations {
|
||||
protected readonly localFiles = new Map<string, Uint8Array>();
|
||||
export class MockClient extends debugging.InMemoryFileSystem {
|
||||
protected client!: SyncClient;
|
||||
|
||||
protected data: Partial<{
|
||||
settings: Partial<SyncSettings>;
|
||||
database: Partial<StoredDatabase>;
|
||||
}> = {
|
||||
database: {
|
||||
// Assume all clients start at the same time so there's no need to fetch
|
||||
// any shared state.
|
||||
hasInitialSyncCompleted: true
|
||||
}
|
||||
};
|
||||
}> = {};
|
||||
|
||||
private slowEventChain: Promise<void> = Promise.resolve();
|
||||
|
||||
public constructor(
|
||||
initialSettings: Partial<SyncSettings>,
|
||||
protected readonly useSlowFileEvents: boolean
|
||||
) {
|
||||
super();
|
||||
this.data.settings = initialSettings;
|
||||
}
|
||||
|
||||
|
|
@ -46,150 +42,82 @@ export class MockClient implements FileSystemOperations {
|
|||
await this.client.start();
|
||||
}
|
||||
|
||||
public async listFilesRecursively(
|
||||
_root: RelativePath | undefined = undefined // we don't use multi-level paths during tests
|
||||
): Promise<RelativePath[]> {
|
||||
return Array.from(this.localFiles.keys());
|
||||
}
|
||||
|
||||
public async read(path: RelativePath): Promise<Uint8Array> {
|
||||
const file = this.localFiles.get(path);
|
||||
if (!file) {
|
||||
throw new Error(`File ${path} does not exist`);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public async getFileSize(path: RelativePath): Promise<number> {
|
||||
return (await this.read(path)).length;
|
||||
}
|
||||
|
||||
public async exists(path: RelativePath): Promise<boolean> {
|
||||
return this.localFiles.has(path);
|
||||
}
|
||||
|
||||
public async create(
|
||||
public override async write(
|
||||
path: RelativePath,
|
||||
newContent: Uint8Array
|
||||
content: Uint8Array
|
||||
): Promise<void> {
|
||||
if (this.localFiles.has(path)) {
|
||||
throw new Error(`File ${path} already exists`);
|
||||
const isNew = !this.files.has(path);
|
||||
|
||||
this.files.set(path, content);
|
||||
|
||||
if (isNew) {
|
||||
this.executeFileOperation(async () => {
|
||||
this.client.syncLocallyCreatedFile(path);
|
||||
});
|
||||
} else {
|
||||
this.executeFileOperation(async () => {
|
||||
this.client.syncLocallyUpdatedFile({ relativePath: path });
|
||||
});
|
||||
}
|
||||
this.client.logger.info(
|
||||
`Creating file ${path} with content ${new TextDecoder().decode(newContent)}`
|
||||
);
|
||||
this.localFiles.set(path, newContent);
|
||||
|
||||
this.executeFileOperation(async () =>
|
||||
this.client.syncLocallyCreatedFile(path)
|
||||
);
|
||||
}
|
||||
|
||||
public async createDirectory(_path: RelativePath): Promise<void> {
|
||||
// This doesn't mean anything in our virtual FS representation
|
||||
}
|
||||
|
||||
public async atomicUpdateText(
|
||||
public override async atomicUpdateText(
|
||||
path: RelativePath,
|
||||
updater: (currentContent: TextWithCursors) => TextWithCursors
|
||||
): Promise<string> {
|
||||
const file = this.localFiles.get(path);
|
||||
const file = this.files.get(path);
|
||||
if (!file) {
|
||||
throw new Error(`File ${path} does not exist`);
|
||||
}
|
||||
const currentContent = new TextDecoder().decode(file);
|
||||
const newContent = updater({ text: currentContent, cursors: [] }).text;
|
||||
const newContentUint8Array = new TextEncoder().encode(newContent);
|
||||
this.localFiles.set(path, newContentUint8Array);
|
||||
this.files.set(path, newContentUint8Array);
|
||||
|
||||
if (!this.useSlowFileEvents) {
|
||||
const existingParts = currentContent
|
||||
.split(" ")
|
||||
.map((part) => part.trim());
|
||||
const newParts = newContent.split(" ").map((part) => part.trim());
|
||||
existingParts.forEach((part) =>
|
||||
// all changes should be additive
|
||||
{
|
||||
assert(
|
||||
newParts.includes(part),
|
||||
`Part ${part} not found in new content: ${newContent}`
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
this.client.logger.info(
|
||||
`Updated file ${path} with:\n current content: ${currentContent}\n new content: ${newContent}`
|
||||
);
|
||||
|
||||
this.executeFileOperation(async () =>
|
||||
this.client.syncLocallyUpdatedFile({
|
||||
relativePath: path
|
||||
})
|
||||
);
|
||||
this.executeFileOperation(async () => {
|
||||
this.client.syncLocallyUpdatedFile({ relativePath: path });
|
||||
});
|
||||
|
||||
return newContent;
|
||||
}
|
||||
|
||||
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
|
||||
const hasExisted = this.localFiles.has(path);
|
||||
this.localFiles.set(path, content);
|
||||
|
||||
this.client.logger.info(
|
||||
`Updated file ${path} with:\n new content: ${new TextDecoder().decode(content)}`
|
||||
);
|
||||
|
||||
public override async delete(path: RelativePath): Promise<void> {
|
||||
this.files.delete(path);
|
||||
this.executeFileOperation(async () => {
|
||||
if (hasExisted) {
|
||||
return this.client.syncLocallyUpdatedFile({
|
||||
relativePath: path
|
||||
});
|
||||
} else {
|
||||
return this.client.syncLocallyCreatedFile(path);
|
||||
}
|
||||
this.client.syncLocallyDeletedFile(path);
|
||||
});
|
||||
}
|
||||
|
||||
public async delete(path: RelativePath): Promise<void> {
|
||||
this.client.logger.info(
|
||||
`Deleting file: ${path} with:\n content ${new TextDecoder().decode(this.localFiles.get(path))}`
|
||||
);
|
||||
this.localFiles.delete(path);
|
||||
|
||||
this.executeFileOperation(async () =>
|
||||
this.client.syncLocallyDeletedFile(path)
|
||||
);
|
||||
}
|
||||
|
||||
public async rename(
|
||||
public override async rename(
|
||||
oldPath: RelativePath,
|
||||
newPath: RelativePath
|
||||
): Promise<void> {
|
||||
const file = this.localFiles.get(oldPath);
|
||||
const file = this.files.get(oldPath);
|
||||
if (!file) {
|
||||
throw new Error(`File ${oldPath} does not exist`);
|
||||
}
|
||||
this.localFiles.set(newPath, file);
|
||||
this.files.set(newPath, file);
|
||||
if (oldPath !== newPath) {
|
||||
this.localFiles.delete(oldPath);
|
||||
this.files.delete(oldPath);
|
||||
}
|
||||
|
||||
this.client.logger.info(
|
||||
`Renamed file: ${oldPath} -> ${newPath} with:\n content ${new TextDecoder().decode(file)}`
|
||||
);
|
||||
|
||||
this.executeFileOperation(async () =>
|
||||
this.executeFileOperation(async () => {
|
||||
this.client.syncLocallyUpdatedFile({
|
||||
oldPath,
|
||||
relativePath: newPath
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private executeFileOperation(callback: () => unknown): void {
|
||||
protected executeFileOperation(callback: () => unknown): void {
|
||||
if (this.useSlowFileEvents) {
|
||||
// we aren't the best client and it takes some time to notice changes
|
||||
setTimeout(callback, Math.random() * 100);
|
||||
// we aren't the best client and it takes some time to notice
|
||||
// changes, but they still arrive in the order they happened
|
||||
this.slowEventChain = this.slowEventChain.then(async () => {
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, Math.random() * 100)
|
||||
);
|
||||
await callback();
|
||||
});
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
import type { SyncSettings } from "sync-client";
|
||||
import { utils } from "sync-client";
|
||||
import { utils, debugging, Logger } from "sync-client";
|
||||
import { MockAgent } from "./agent/mock-agent";
|
||||
import { sleep } from "./utils/sleep";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { randomCasing } from "./utils/random-casing";
|
||||
import { TimeoutError } from "./utils/with-timeout";
|
||||
import { TestErrorTracker } from "./utils/test-error-tracker";
|
||||
|
||||
const TEST_ITERATIONS = 5;
|
||||
const MAX_INITIAL_DOCS = 10;
|
||||
|
||||
// Simulate async file access by injecting waiting time before returning from file operations.
|
||||
let slowFileEvents = false;
|
||||
|
|
@ -13,9 +16,13 @@ let slowFileEvents = false;
|
|||
// Whether to do resets in the test runs
|
||||
let doResets = false;
|
||||
|
||||
const logger = new Logger();
|
||||
debugging.logToConsole(logger);
|
||||
|
||||
const errorTracker = new TestErrorTracker();
|
||||
|
||||
async function runTest({
|
||||
agentCount,
|
||||
concurrency,
|
||||
iterations,
|
||||
doDeletes,
|
||||
useResets,
|
||||
|
|
@ -23,7 +30,6 @@ async function runTest({
|
|||
jitterScaleInSeconds
|
||||
}: {
|
||||
agentCount: number;
|
||||
concurrency: number;
|
||||
iterations: number;
|
||||
doDeletes: boolean;
|
||||
useResets: boolean;
|
||||
|
|
@ -32,18 +38,18 @@ async function runTest({
|
|||
}): Promise<void> {
|
||||
slowFileEvents = useSlowFileEvents;
|
||||
doResets = useResets;
|
||||
errorTracker.reset();
|
||||
|
||||
const settings = `with ${agentCount} agents, concurrency ${concurrency}, iterations ${iterations}, doDeletes ${doDeletes}, doResets ${useResets}, jitterScaleInSeconds ${jitterScaleInSeconds}, useSlowFileEvents ${useSlowFileEvents}`;
|
||||
console.info(`Running test ${settings}`);
|
||||
const settings = `with ${agentCount} agents, iterations ${iterations}, doDeletes ${doDeletes}, doResets ${useResets}, jitterScaleInSeconds ${jitterScaleInSeconds}, useSlowFileEvents ${useSlowFileEvents}`;
|
||||
logger.info(`Running test ${settings}`);
|
||||
|
||||
const vaultName = uuidv4();
|
||||
console.info(`Using vault name: ${vaultName}`);
|
||||
logger.info(`Using vault name: ${vaultName}`);
|
||||
const initialSettings: Partial<SyncSettings> = {
|
||||
isSyncEnabled: true,
|
||||
token: " test-token-change-me ", // same as in sync-server/config-e2e.yml with spaces
|
||||
vaultName: randomCasing(vaultName) + (Math.random() > 0.5 ? " " : ""), // extra spaces shouldn't matter
|
||||
syncConcurrency: concurrency,
|
||||
remoteUri: "http://localhost:3000"
|
||||
remoteUri: "http://localhost:3010"
|
||||
};
|
||||
|
||||
const clients: MockAgent[] = [];
|
||||
|
|
@ -55,67 +61,107 @@ async function runTest({
|
|||
doDeletes,
|
||||
useResets,
|
||||
useSlowFileEvents,
|
||||
jitterScaleInSeconds
|
||||
jitterScaleInSeconds,
|
||||
errorTracker
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
for (const client of clients) {
|
||||
const initialDocCount = Math.floor(
|
||||
Math.random() * MAX_INITIAL_DOCS
|
||||
);
|
||||
if (initialDocCount > 0) {
|
||||
logger.info(
|
||||
`Creating ${initialDocCount} initial documents for ${client.name}`
|
||||
);
|
||||
await client.createInitialDocuments(initialDocCount);
|
||||
}
|
||||
}
|
||||
|
||||
await utils.awaitAll(clients.map(async (client) => client.init()));
|
||||
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
console.info(`Iteration ${i + 1}/${iterations}`);
|
||||
logger.info(`Iteration ${i + 1}/${iterations}`);
|
||||
await utils.awaitAll(clients.map(async (client) => client.act()));
|
||||
await sleep(Math.random() * 200);
|
||||
}
|
||||
|
||||
console.info("Stopping agents");
|
||||
errorTracker.checkAndThrow();
|
||||
|
||||
// Each agent can have unpushed changes which might conflict with eachother so each has to resolve the conflicts & push, and
|
||||
logger.info("Stopping agents");
|
||||
|
||||
// Drain pending actions and enable sync for each client
|
||||
for (const client of clients) {
|
||||
try {
|
||||
console.info(`Finishing up ${client.name}`);
|
||||
logger.info(`Finishing up ${client.name}`);
|
||||
await client.finish();
|
||||
} catch (err) {
|
||||
if (!slowFileEvents) {
|
||||
if (err instanceof TimeoutError || !slowFileEvents) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// then we need a second pass to ensure that all agents pull the same state.
|
||||
// Settling rounds: drain cascading broadcasts between agents
|
||||
for (let round = 0; round < 10; round++) {
|
||||
for (const client of clients) {
|
||||
try {
|
||||
await client.waitUntilSynced();
|
||||
} catch (err) {
|
||||
if (err instanceof TimeoutError || !slowFileEvents) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: it's very ugly, let's remove this
|
||||
await sleep(2000);
|
||||
}
|
||||
|
||||
for (const client of clients) {
|
||||
try {
|
||||
console.info(`Destroying ${client.name}`);
|
||||
logger.info(`Destroying ${client.name}`);
|
||||
await client.destroy();
|
||||
} catch (err) {
|
||||
if (!slowFileEvents) {
|
||||
if (err instanceof TimeoutError || !slowFileEvents) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.info("Agents finished successfully");
|
||||
logger.info("Agents finished successfully");
|
||||
errorTracker.checkAndThrow();
|
||||
|
||||
clients.slice(0, -1).forEach((client, i) => {
|
||||
console.info(
|
||||
logger.info(
|
||||
`Checking consistency between ${client.name} and ${clients[i + 1].name}`
|
||||
);
|
||||
client.assertFileSystemsAreConsistent(clients[i]);
|
||||
console.info(`Consistency check for ${client.name} passed`);
|
||||
client.assertFileSystemsAreConsistent(clients[i + 1]);
|
||||
logger.info(`Consistency check for ${client.name} passed`);
|
||||
});
|
||||
|
||||
console.info("File systems found to be consistent");
|
||||
logger.info("File systems found to be consistent");
|
||||
|
||||
clients.forEach((client) => {
|
||||
console.info(`Checking content for ${client.name}`);
|
||||
logger.info(`Checking content for ${client.name}`);
|
||||
client.assertAllContentIsPresentOnce();
|
||||
console.info(`Content check for ${client.name} passed`);
|
||||
logger.info(`Content check for ${client.name} passed`);
|
||||
});
|
||||
|
||||
console.info(`Test passed ${settings}`);
|
||||
clients.forEach((client) => {
|
||||
logger.info(
|
||||
`Checking binary content duplication for ${client.name}`
|
||||
);
|
||||
client.assertBinaryContentNotDuplicated();
|
||||
logger.info(
|
||||
`Binary content duplication check for ${client.name} passed`
|
||||
);
|
||||
});
|
||||
|
||||
logger.info(`Test passed ${settings}`);
|
||||
} catch (err) {
|
||||
console.error(`Test failed ${settings}`);
|
||||
logger.error(`Test failed ${settings}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
|
@ -124,7 +170,6 @@ async function runTests(): Promise<void> {
|
|||
for (let i = 0; i < TEST_ITERATIONS; i++) {
|
||||
await runTest({
|
||||
agentCount: 2,
|
||||
concurrency: 16,
|
||||
iterations: 100,
|
||||
doDeletes: true,
|
||||
useResets: true,
|
||||
|
|
@ -133,24 +178,59 @@ async function runTests(): Promise<void> {
|
|||
});
|
||||
|
||||
for (const useSlowFileEvents of [true, false]) {
|
||||
for (const concurrency of [
|
||||
16,
|
||||
1 // test with concurrency 1 to check for deadlocks
|
||||
]) {
|
||||
for (const doDeletes of [false, true]) {
|
||||
await runTest({
|
||||
agentCount: 2,
|
||||
concurrency,
|
||||
iterations: 100,
|
||||
doDeletes,
|
||||
useResets: false,
|
||||
useSlowFileEvents,
|
||||
jitterScaleInSeconds: 0.75
|
||||
});
|
||||
}
|
||||
for (const doDeletes of [false, true]) {
|
||||
await runTest({
|
||||
agentCount: 2,
|
||||
iterations: 100,
|
||||
doDeletes,
|
||||
useResets: false,
|
||||
useSlowFileEvents,
|
||||
jitterScaleInSeconds: 0.75
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await runTest({
|
||||
agentCount: 3,
|
||||
iterations: 75,
|
||||
doDeletes: true,
|
||||
useResets: false,
|
||||
useSlowFileEvents: false,
|
||||
jitterScaleInSeconds: 0.75
|
||||
});
|
||||
await runTest({
|
||||
agentCount: 3,
|
||||
iterations: 75,
|
||||
doDeletes: false,
|
||||
useResets: true,
|
||||
useSlowFileEvents: false,
|
||||
jitterScaleInSeconds: 0.75
|
||||
});
|
||||
await runTest({
|
||||
agentCount: 4,
|
||||
iterations: 50,
|
||||
doDeletes: true,
|
||||
useResets: false,
|
||||
useSlowFileEvents: false,
|
||||
jitterScaleInSeconds: 0.75
|
||||
});
|
||||
await runTest({
|
||||
agentCount: 2,
|
||||
iterations: 100,
|
||||
doDeletes: true,
|
||||
useResets: false,
|
||||
useSlowFileEvents: false,
|
||||
jitterScaleInSeconds: 0.1
|
||||
});
|
||||
await runTest({
|
||||
agentCount: 2,
|
||||
iterations: 100,
|
||||
doDeletes: true,
|
||||
useResets: true,
|
||||
useSlowFileEvents: false,
|
||||
jitterScaleInSeconds: 1.5
|
||||
});
|
||||
}
|
||||
|
||||
process.on("uncaughtException", (error) => {
|
||||
|
|
@ -163,12 +243,15 @@ process.on("uncaughtException", (error) => {
|
|||
return;
|
||||
}
|
||||
|
||||
console.error("Uncaught exception:", error);
|
||||
logger.error(`Error: uncaught exception: ${error}`);
|
||||
if (error instanceof Error && error.stack != null) {
|
||||
logger.error(error.stack);
|
||||
}
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
process.on("unhandledRejection", (error, _promise) => {
|
||||
if (error instanceof Error && error.message === "Sync was reset") {
|
||||
if (error instanceof Error && error.name === "SyncResetError") {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -191,7 +274,10 @@ process.on("unhandledRejection", (error, _promise) => {
|
|||
return;
|
||||
}
|
||||
|
||||
console.error("Unhandled rejection:", error);
|
||||
logger.error(`Error - unhandled rejection: ${error}`);
|
||||
if (error instanceof Error && error.stack != null) {
|
||||
logger.error(error.stack);
|
||||
}
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
|
|
@ -199,7 +285,10 @@ runTests()
|
|||
.then(() => {
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
console.error(err);
|
||||
.catch((error: unknown) => {
|
||||
logger.error(`Error - tests failed with ${error}`);
|
||||
if (error instanceof Error && error.stack != null) {
|
||||
logger.error(error.stack);
|
||||
}
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
|
|||
23
frontend/test-client/src/utils/test-error-tracker.ts
Normal file
23
frontend/test-client/src/utils/test-error-tracker.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
export class TestErrorTracker {
|
||||
private firstError: { agentName: string; message: string } | null = null;
|
||||
|
||||
public recordError(agentName: string, message: string): void {
|
||||
this.firstError ??= { agentName, message };
|
||||
}
|
||||
|
||||
/**
|
||||
* If an error was recorded, throw it. Call this at natural checkpoints:
|
||||
* after each iteration, before assertions, etc.
|
||||
*/
|
||||
public checkAndThrow(): void {
|
||||
if (this.firstError !== null) {
|
||||
const { agentName, message } = this.firstError;
|
||||
throw new Error(`ERROR-level log from ${agentName}: ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear recorded errors. Call at the start of each test. */
|
||||
public reset(): void {
|
||||
this.firstError = null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,10 @@
|
|||
export class TimeoutError extends Error {
|
||||
public constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "TimeoutError";
|
||||
}
|
||||
}
|
||||
|
||||
export async function withTimeout<T>(
|
||||
promise: Promise<T>,
|
||||
timeoutMs: number,
|
||||
|
|
@ -8,7 +15,9 @@ export async function withTimeout<T>(
|
|||
new Promise<T>((_, reject) =>
|
||||
setTimeout(() => {
|
||||
reject(
|
||||
new Error(`${operationName} timed out after ${timeoutMs}ms`)
|
||||
new TimeoutError(
|
||||
`${operationName} timed out after ${timeoutMs}ms`
|
||||
)
|
||||
);
|
||||
}, timeoutMs)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,13 +5,8 @@
|
|||
"target": "ES2022",
|
||||
"module": "CommonJS",
|
||||
"esModuleInterop": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"ES2024",
|
||||
],
|
||||
"lib": ["DOM", "ES2024"],
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"exclude": [
|
||||
"./dist"
|
||||
]
|
||||
"exclude": ["./dist"]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue