// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import { assert } from 'chai';
import * as Common from '../../core/common/common.js';
import * as Platform from '../../core/platform/platform.js';
import * as SDK from '../../core/sdk/sdk.js';
import { setupLocaleHooks } from '../../testing/LocaleHelpers.js';
import * as TextUtils from '../text_utils/text_utils.js';
import * as HAR from './har.js';
const { urlString } = Platform.DevToolsPath;
const simulateRequestWithStartTime = (startTime) => {
    const requestId = 'r0';
    const request = SDK.NetworkRequest.NetworkRequest.create(requestId, urlString `p0.com`, Platform.DevToolsPath.EmptyUrlString, null, null, null);
    request.setIssueTime(startTime, startTime);
    request.setContentDataProvider(() => Promise.resolve(new TextUtils.ContentData.ContentData('', false, request.mimeType)));
    return request;
};
describe('HARWriter', () => {
    setupLocaleHooks();
    it('can correctly sort exported requests logs', async () => {
        const req1Time = new Date(2020, 0, 3);
        const req2Time = new Date(2020, 1, 3);
        const req3Time = new Date(2020, 2, 3);
        const req1 = simulateRequestWithStartTime(req1Time.getTime() / 1000);
        const req2 = simulateRequestWithStartTime(req2Time.getTime() / 1000);
        const req3 = simulateRequestWithStartTime(req3Time.getTime() / 1000);
        const progress = new Common.Progress.Progress();
        const compositeProgress = new Common.Progress.CompositeProgress(progress);
        const result = await HAR.Writer.Writer.harStringForRequests([
            req3,
            req2,
            req1,
        ], { sanitize: false }, compositeProgress);
        const resultEntries = JSON.parse(result).log.entries;
        assert.strictEqual(resultEntries[0].startedDateTime, req1Time.toJSON(), 'earlier request should come first');
        assert.strictEqual(resultEntries[1].startedDateTime, req2Time.toJSON(), 'earlier request should come first');
        assert.strictEqual(resultEntries[2].startedDateTime, req3Time.toJSON(), 'earlier request should come first');
    });
    it('exports multiple EventSource messages for an unfinished request', async () => {
        const request = simulateRequestWithStartTime(Date.now() / 1000);
        request.finished = false;
        request.mimeType = 'text/event-stream';
        request.addEventSourceMessage(1773352390.598671, 'session', '', '{"sid":"11111111-2222-3333-4444-555555555555","tenant":"66666666-7777-8888-9999-000000000000"}');
        request.addEventSourceMessage(1773352391.102345, 'message', '2', '{"role":"assistant","content":"hello"}');
        const progress = new Common.Progress.Progress();
        const compositeProgress = new Common.Progress.CompositeProgress(progress);
        const result = await HAR.Writer.Writer.harStringForRequests([request], { sanitize: false }, compositeProgress);
        const resultEntries = JSON.parse(result).log.entries;
        assert.lengthOf(resultEntries, 1);
        assert.lengthOf(resultEntries[0]._eventSourceMessages, 2);
        assert.strictEqual(resultEntries[0]._eventSourceMessages[0].eventName, 'session');
        assert.strictEqual(resultEntries[0]._eventSourceMessages[1].eventId, '2');
    });
});
//# sourceMappingURL=Writer.test.js.map