Bug fixes

This commit is contained in:
Andras Schmelczer 2026-03-15 13:27:18 +00:00
parent bbec7f14dd
commit df37e6c236
15 changed files with 632 additions and 157 deletions

View file

@ -13,6 +13,7 @@ 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
@ -51,7 +52,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
);
@ -115,9 +116,11 @@ export class MockAgent extends MockClient {
);
}
public async act(): Promise<void> {
const options: (() => Promise<unknown>)[] = [
this.createFileAction.bind(this)
this.createFileAction.bind(this),
this.createBinaryFileAction.bind(this)
];
if (
@ -132,7 +135,8 @@ export class MockAgent extends MockClient {
options.push(
this.renameFileAction.bind(this),
this.updateFileAction.bind(this)
this.updateFileAction.bind(this),
this.updateBinaryFileAction.bind(this)
);
if (this.doDeletes) {
@ -226,26 +230,26 @@ export class MockAgent extends MockClient {
"Local data: " + JSON.stringify(this.data, null, 2)
);
this.client.logger.info(
"Local files: " + Array.from(otherAgent.files.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.files.keys()).join(", ")
"Other agent's files: " + Array.from(otherAgent.files.keys()).join(", ")
);
throw e;
}
}
// For slow file events, still check for duplicates (skip existence check).
// Duplication is always a bug regardless of timing.
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 check)`
);
return;
}
for (const content of this.writtenContents) {
@ -260,14 +264,13 @@ export class MockAgent extends MockClient {
`[${this.name}] Content ${content} found in multiple files: ${found.join(", ")}`
);
if (!this.doDeletes) {
if (!this.useSlowFileEvents && !this.doDeletes) {
assert(
found.length >= 1,
`[${this.name}] Content ${content} not found in any files`
);
}
if (found.length === 1) {
const [file] = found;
const fileContent = new TextDecoder().decode(
@ -281,6 +284,31 @@ export class MockAgent extends MockClient {
}
}
// Check binary content isn't duplicated across files.
// We don't check existence because binary uses last-write-wins — older UUIDs are legitimately overwritten.
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);
});
assert(
found.length <= 1,
`[${this.name}] Binary content ${content} found in multiple files: ${found.join(", ")}`
);
}
}
public getFileList(): string[] {
return Array.from(this.files.keys());
}
public getFileContent(path: string): Uint8Array | undefined {
return this.files.get(path);
}
private async resetClient(): Promise<void> {
this.client.logger.info(`Resetting client ${this.name}`);
await this.client.destroy();
@ -308,6 +336,28 @@ export class MockAgent extends MockClient {
});
}
// 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 content = this.getBinaryContent();
this.client.logger.info(
`Decided to create binary file ${file}`
);
return this.create(file, content, {
ignoreSlowFileEvents: true
});
}
private async disableSyncAction(): Promise<void> {
this.client.logger.info(`Decided to disable sync`);
this.lastSyncEnabledState = false;
@ -357,7 +407,9 @@ export class MockAgent extends MockClient {
}
private async updateFileAction(): Promise<void> {
const files = await this.listFilesRecursively();
const files = (await this.listFilesRecursively()).filter((f) =>
f.endsWith(".md")
);
if (files.length === 0) {
return;
}
@ -391,6 +443,40 @@ export class MockAgent extends MockClient {
);
}
// Binary file update — complete replacement (last-write-wins)
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 content = this.getBinaryContent();
this.client.logger.info(
`Decided to update binary file ${file}`
);
this.doNotTouchWhileOffline.push(file);
this.files.set(file, content);
this.executeFileOperation(
async () =>
this.client.syncLocallyUpdatedFile({
relativePath: file
}),
true
);
}
private async deleteFileAction(): Promise<void> {
const files = await this.listFilesRecursively();
if (files.length === 0) {
@ -408,8 +494,19 @@ export class MockAgent extends MockClient {
return uuid;
}
private getBinaryContent(): Uint8Array {
const uuid = uuidv4();
this.writtenBinaryContents.push(uuid);
return 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`;
}
}

View file

@ -177,7 +177,7 @@ export class MockClient extends debugging.InMemoryFileSystem {
);
}
private executeFileOperation(
protected executeFileOperation(
callback: () => unknown,
ignoreSlowFileEvents = false
): void {

View file

@ -119,7 +119,7 @@ async function runTest({
logger.info(
`Checking consistency between ${client.name} and ${clients[i + 1].name}`
);
client.assertFileSystemsAreConsistent(clients[i]);
client.assertFileSystemsAreConsistent(clients[i + 1]);
logger.info(`Consistency check for ${client.name} passed`);
});