Skip to content

Commit 13b5e58

Browse files
committed
fix: stabilize flaky AI snapshot tests
Fix race condition in expectFileDeleted test helper where fileExists() was fired on every awaitsFor poll but the result was checked before the promise resolved, causing one-poll-behind behavior and timeouts. Make _createOrUpdateFile resilient to stale FileSystem cache: when file.exists() returns true but getDocumentForPath fails (e.g. after a delete+recreate cycle), fall back to creating the file on disk and retry.
1 parent 35c079d commit 13b5e58

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/core-ai/AISnapshotStore.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,42 @@ define(function (require, exports, module) {
254254
});
255255
}
256256

257+
function _createThenSet() {
258+
const file = FileSystem.getFileForPath(vfsPath);
259+
file.write("", function (writeErr) {
260+
if (writeErr) {
261+
result.reject(new Error("Could not create file: " + writeErr));
262+
return;
263+
}
264+
_setContent();
265+
});
266+
}
267+
257268
const file = FileSystem.getFileForPath(vfsPath);
258269
file.exists(function (existErr, exists) {
259270
if (exists) {
260-
_setContent();
271+
// File may appear to exist due to stale FS cache after a
272+
// delete+recreate cycle. Try opening first; if it fails,
273+
// recreate the file on disk and retry.
274+
DocumentManager.getDocumentForPath(vfsPath)
275+
.done(function (doc) {
276+
try {
277+
doc.setText(content);
278+
saveDocToDisk(doc).always(function () {
279+
CommandManager.execute(Commands.CMD_OPEN, { fullPath: vfsPath })
280+
.always(function () {
281+
result.resolve();
282+
});
283+
});
284+
} catch (err) {
285+
result.reject(err);
286+
}
287+
})
288+
.fail(function () {
289+
_createThenSet();
290+
});
261291
} else {
262-
file.write("", function (writeErr) {
263-
if (writeErr) {
264-
result.reject(new Error("Could not create file: " + writeErr));
265-
return;
266-
}
267-
_setContent();
268-
});
292+
_createThenSet();
269293
}
270294
});
271295

test/spec/ai-snapshot-test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,15 @@ define(function (require, exports, module) {
143143

144144
async function expectFileDeleted(name) {
145145
let gone = false;
146+
let checking = false;
146147
await awaitsFor(function () {
147-
fileExists(name).then(function (e) { gone = !e; });
148+
if (!checking && !gone) {
149+
checking = true;
150+
fileExists(name).then(function (e) {
151+
gone = !e;
152+
checking = false;
153+
});
154+
}
148155
return gone;
149156
}, name + " to be deleted", 5000);
150157
}

0 commit comments

Comments
 (0)