// Copyright 2023 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 { renderElementIntoDOM, } from '../../../../testing/DOMHelpers.js';
import { describeWithEnvironment } from '../../../../testing/EnvironmentHelpers.js';
import { createViewFunctionStub } from '../../../../testing/ViewFunctionHelpers.js';
import * as PreloadingComponents from './components.js';
function renderRuleSetDetailsView() {
    const view = createViewFunctionStub(PreloadingComponents.RuleSetDetailsView.RuleSetDetailsView);
    const component = new PreloadingComponents.RuleSetDetailsView.RuleSetDetailsView(undefined, view);
    renderElementIntoDOM(component);
    return { component, view };
}
describeWithEnvironment('RuleSetDetailsView', () => {
    it('renders placeholder if not selected', async () => {
        const { component, view } = renderRuleSetDetailsView();
        component.ruleSet = null;
        assert.isNull(await view.nextInput);
    });
    it('renders rule set', async () => {
        const { component, view } = renderRuleSetDetailsView();
        const sourceText = `
{
  "prefetch": [
    {
      "source": "list",
      "urls": ["/subresource.js"]
    }
  ]
}
`;
        component.ruleSet = {
            id: 'ruleSetId:1',
            loaderId: 'loaderId:1',
            sourceText,
            backendNodeId: 1,
        };
        component.shouldPrettyPrint = false;
        const input = await view.nextInput;
        assert.exists(input);
        assert.isUndefined(input.errorMessage);
        assert.strictEqual(input.sourceText, sourceText);
    });
    it('renders url when included', async () => {
        const { component, view } = renderRuleSetDetailsView();
        component.ruleSet = {
            id: 'ruleSetId:1',
            loaderId: 'loaderId:1',
            sourceText: '<something valid>',
            url: 'https://example.com/speculationrules.json',
            requestId: 'requestId',
        };
        const input = await view.nextInput;
        assert.exists(input);
        assert.strictEqual(input.url, 'https://example.com/speculationrules.json');
    });
    it('renders the error message', async () => {
        const { component, view } = renderRuleSetDetailsView();
        component.ruleSet = {
            id: 'ruleSetId:1',
            loaderId: 'loaderId:1',
            sourceText: '<something invalid>',
            backendNodeId: 1,
            errorType: "SourceIsNotJsonObject" /* Protocol.Preload.RuleSetErrorType.SourceIsNotJsonObject */,
            errorMessage: 'Line: 6, column: 1, Syntax error.',
        };
        const input = await view.nextInput;
        assert.exists(input);
        assert.strictEqual(input.errorMessage, 'Line: 6, column: 1, Syntax error.');
    });
    it('formats the source text', async () => {
        const { component, view } = renderRuleSetDetailsView();
        component.ruleSet = {
            id: 'ruleSetId:1',
            loaderId: 'loaderId:1',
            sourceText: '{"prefetch":[{"source": "list","urls": ["/subresource.js"]}]}',
            backendNodeId: 1,
        };
        const input = await view.nextInput;
        assert.exists(input);
        assert.strictEqual(input.sourceText, `{
    "prefetch": [
        {
            "source": "list",
            "urls": [
                "/subresource.js"
            ]
        }
    ]
}`);
    });
});
//# sourceMappingURL=RuleSetDetailsView.test.js.map