Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Mar 6, 2025
1 parent 32ff135 commit a001213
Show file tree
Hide file tree
Showing 3 changed files with 548 additions and 18 deletions.
31 changes: 19 additions & 12 deletions lib/util/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function generateHtmlForm(currentSettings: RecursivePartial<Settings>, devices:
let devicesSelect = '';

if (devices.length > 0) {
devicesSelect += '<select id="found_device" name="found_device" onchange="setFoundDevice(this)">';
devicesSelect += '<select id="found_device" onchange="setFoundDevice(this)">';
devicesSelect += '<option value="">Select a device</option>';

for (const device of devices) {
Expand All @@ -70,6 +70,7 @@ function generateHtmlForm(currentSettings: RecursivePartial<Settings>, devices:
devicesSelect = '<small>No device found</small>';
}

/* v8 ignore start */
return `
<!doctype html>
<html lang="en">
Expand All @@ -84,7 +85,7 @@ function generateHtmlForm(currentSettings: RecursivePartial<Settings>, devices:
<h1>Zigbee2MQTT Onboarding</h1>
<form method="post" action="/">
<fieldset>
<label for="serial_port">Found Devices</label>
<label for="found_device">Found Devices</label>
${devicesSelect}
</fieldset>
<fieldset>
Expand Down Expand Up @@ -213,6 +214,7 @@ function generateHtmlForm(currentSettings: RecursivePartial<Settings>, devices:
</body>
</html>
`;
/* v8 ignore stop */
}

function generateHtmlError(errors: string): string {
Expand Down Expand Up @@ -252,10 +254,11 @@ async function startOnboardingServer(currentSettings: RecursivePartial<Settings>

const success = await new Promise<boolean>((resolve) => {
server = createServer(async (req, res) => {
if (req.method == 'POST') {
if (req.method === 'POST') {
if (failed) {
res.end();
resolve(false);
res.end(() => {
resolve(false);
});
} else {
let body = '';

Expand Down Expand Up @@ -310,18 +313,21 @@ async function startOnboardingServer(currentSettings: RecursivePartial<Settings>
res.end(
generateHtmlDone(
redirect
? `${protocol}://${currentSettings.frontend?.host ?? 'localhost'}:${currentSettings.frontend?.port ?? '8080'}${currentSettings.frontend?.base_url ?? '/'}`
? /* v8 ignore next */ `${protocol}://${currentSettings.frontend?.host ?? 'localhost'}:${currentSettings.frontend?.port ?? '8080'}${currentSettings.frontend?.base_url ?? '/'}`
: undefined,
),
() => {
resolve(true);
},
);
resolve(true);
} catch (error) {
console.error(`Failed to apply configuration: ${(error as Error).message}`);
failed = true;

if (process.env.ONBOARDING_NO_FAILURE_PAGE) {
res.end();
resolve(false);
res.end(() => {
resolve(false);
});
} else {
res.setHeader('Content-Type', 'text/html');
res.writeHead(406);
Expand Down Expand Up @@ -353,9 +359,10 @@ async function startFailureServer(errors: string): Promise<void> {

await new Promise<void>((resolve) => {
server = createServer(async (req, res) => {
if (req.method == 'POST') {
res.end();
resolve();
if (req.method === 'POST') {
res.end(() => {
resolve();
});
} else {
res.setHeader('Content-Type', 'text/html');
res.writeHead(406);
Expand Down
24 changes: 18 additions & 6 deletions test/mocks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import tmp from 'tmp';
import yaml from '../../lib/util/yaml';

export const mockDir: string = tmp.dirSync().name;
const configFile = path.join(mockDir, 'configuration.yaml');
const stateFile = path.join(mockDir, 'state.json');
const databaseFile = path.join(mockDir, 'database.db');

export const DEFAULT_CONFIGURATION = {
homeassistant: {enabled: false},
Expand Down Expand Up @@ -250,17 +252,23 @@ export const DEFAULT_CONFIGURATION = {
export function writeDefaultConfiguration(config: unknown = undefined): void {
config = config || DEFAULT_CONFIGURATION;

yaml.writeIfChanged(path.join(mockDir, 'configuration.yaml'), config);
yaml.writeIfChanged(configFile, config);
}

export function read(): ReturnType<typeof yaml.read> {
return yaml.read(configFile);
}

export function removeConfiguration(): void {
fs.rmSync(configFile, {force: true});
}

export function writeEmptyState(): void {
fs.writeFileSync(stateFile, stringify({}));
}

export function removeState(): void {
if (stateExists()) {
fs.unlinkSync(stateFile);
}
fs.rmSync(stateFile, {force: true});
}

export function stateExists(): boolean {
Expand Down Expand Up @@ -290,8 +298,12 @@ export function writeDefaultState(): void {
fs.writeFileSync(path.join(mockDir, 'state.json'), stringify(defaultState));
}

export function read(): ReturnType<typeof yaml.read> {
return yaml.read(path.join(mockDir, 'configuration.yaml'));
export function writeEmptyDatabase(): void {
fs.writeFileSync(databaseFile, '');
}

export function removeDatabase(): void {
fs.rmSync(databaseFile, {force: true});
}

vi.mock('../../lib/util/data', async () => {
Expand Down
Loading

0 comments on commit a001213

Please sign in to comment.