"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_utils_1 = require("../testing/test-utils");
const ExternalEditWatcher_1 = require("./ExternalEditWatcher");
const promises_1 = require("fs/promises");
const Note_1 = require("../models/Note");
const time_1 = require("@joplin/utils/time");
const waitFor_1 = require("../testing/waitFor");
const createAndWatchNotes = async (notes) => {
    const watcher = new ExternalEditWatcher_1.default();
    const openedPaths = [];
    watcher.initialize(jest.fn(() => ({
        openItem: (filePath) => {
            openedPaths.push(filePath);
        },
    })), jest.fn());
    const savedNotes = [];
    for (const note of notes) {
        const savedNote = await Note_1.default.save(note);
        await watcher.openAndWatch(savedNote);
        savedNotes.push(savedNote);
    }
    expect(openedPaths).toHaveLength(savedNotes.length);
    return {
        notes: savedNotes,
        filePaths: openedPaths,
        watcher,
    };
};
const createAndWatchNote = async (note) => {
    const { notes, filePaths, watcher } = await createAndWatchNotes([note]);
    return {
        note: notes[0],
        filePath: filePaths[0],
        watcher,
    };
};
describe('ExternalEditWatcher', () => {
    beforeEach(async () => {
        await (0, test_utils_1.setupDatabaseAndSynchronizer)(0);
        await (0, test_utils_1.switchClient)(0);
        jest.useRealTimers();
    });
    test('should handle rapid changes to a file', async () => {
        const { filePath, watcher, note } = await createAndWatchNote({
            title: 'Testing',
            body: 'test',
        });
        try {
            await (0, promises_1.appendFile)(filePath, '1');
            // Change the note several times, with a brief delay between each:
            await (0, time_1.msleep)(10);
            await (0, promises_1.appendFile)(filePath, '2');
            await (0, time_1.msleep)(10);
            await (0, promises_1.appendFile)(filePath, '3');
            await (0, time_1.msleep)(10);
            await (0, promises_1.appendFile)(filePath, '4');
            // Should detect both changes
            await (0, waitFor_1.default)(async () => {
                expect(await Note_1.default.load(note.id)).toMatchObject({
                    title: 'Testing',
                    body: 'test1234',
                });
            });
        }
        finally {
            await watcher.stopWatchingAll();
        }
    });
    test('should detect changes made to different files', async () => {
        const { filePaths, watcher, notes } = await createAndWatchNotes([
            { title: 'Test 1', body: 'Test' },
            { title: 'Test 2', body: 'Test' },
        ]);
        try {
            await (0, promises_1.appendFile)(filePaths[0], ' (updated 1)');
            await (0, promises_1.appendFile)(filePaths[1], ' (updated 2)');
            await (0, waitFor_1.default)(async () => {
                expect(await Note_1.default.load(notes[0].id)).toMatchObject({
                    title: 'Test 1',
                    body: 'Test (updated 1)',
                });
                expect(await Note_1.default.load(notes[1].id)).toMatchObject({
                    title: 'Test 2',
                    body: 'Test (updated 2)',
                });
            });
        }
        finally {
            await watcher.stopWatchingAll();
        }
    });
});
//# sourceMappingURL=ExternalEditWatcher.test.js.map