/**
* @jest-environment jsdom
*/
import React from 'react';
import { render } from '@testing-library/react';
import { DockerLauncher } from '../src/components/DockerLauncher.jsx';
import {
LEGIT,
expectNoControlBytes,
hostile,
nodeWithText,
sanitised,
} from './support/terminalControls.js';
const METHOD = {
kind: 'linux-system',
command: 'sudo',
args: ['systemctl', 'start', 'docker'],
needsPrivileges: true,
display: 'sudo systemctl start docker',
typicalWaitMs: 30000,
};
const WINDOWS_METHOD = {
...METHOD,
kind: 'windows-desktop',
needsPrivileges: false,
display: '"C:\\Program Files\\Docker\\Docker\\Docker Desktop.exe"',
typicalWaitMs: 60000,
};
describe('DockerLauncher', () => {
test('idle renders nothing', () => {
const { container } = render(
);
expect(container.firstChild).toBeNull();
});
test('confirming shows the command and the password note when privileged', () => {
const { getByText } = render(
);
expect(getByText(METHOD.display)).toBeTruthy();
expect(getByText(/You will be asked for your password/)).toBeTruthy();
expect(getByText(/never type an admin password/)).toBeTruthy();
});
test('confirming omits the password note when no privileges are needed', () => {
const { queryByText } = render(
);
expect(queryByText(/You will be asked for your password/)).toBeNull();
});
test('waiting shows the elapsed counter and the Esc hint', () => {
const { getByText } = render(
);
expect(getByText(/Started 5s ago/)).toBeTruthy();
expect(getByText('[Esc] stop waiting (Docker keeps starting)')).toBeTruthy();
});
test('failed shows the error message', () => {
const { getByText } = render(
);
expect(getByText('ENOENT: no such file')).toBeTruthy();
expect(getByText(/Try again/)).toBeTruthy();
});
test('timeout shows the note and includes the WSL2 note for windows-desktop', () => {
const { getByText } = render(
);
expect(getByText(/Docker started but is not responding yet/)).toBeTruthy();
expect(getByText(/WSL2 engine still initializing/)).toBeTruthy();
expect(getByText('[Enter] keep waiting')).toBeTruthy();
expect(getByText('[Esc] give up waiting (Docker keeps starting)')).toBeTruthy();
});
test('timeout omits the WSL2 note for non-windows platforms', () => {
const { queryByText } = render(
);
expect(queryByText(/WSL2 engine/)).toBeNull();
});
test('ready shows a ready message', () => {
const { getByText } = render(
);
expect(getByText('Docker is ready.')).toBeTruthy();
});
});
// The failure message is the output of the launcher process, not CDD's own
// wording. A terminal executes part of what it is given, so it has to reach
// the screen without its control bytes.
describe('DockerLauncher — the launch error reaches the screen sanitised', () => {
test('the failure message leaves no control byte in the DOM', () => {
const { container } = render(
);
const message = nodeWithText(container, sanitised('error'));
expect(message).toBeTruthy();
expectNoControlBytes(message.textContent);
});
test('the words of the failure message survive', () => {
const { container } = render(
);
expect(container.textContent).toContain(sanitised('error'));
});
test('the url of the hyperlink does not stay on screen', () => {
const { container } = render(
);
expect(container.textContent).not.toContain('evil.example');
});
test('legitimate text is not mangled', () => {
const { container } = render(
);
const message = nodeWithText(container, LEGIT);
expect(message).toBeTruthy();
expect(message.textContent).toContain(LEGIT);
expectNoControlBytes(message.textContent);
});
});