Skip to content

Commit

Permalink
Removed duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Nov 3, 2023
1 parent bf12702 commit da7e03c
Showing 1 changed file with 28 additions and 189 deletions.
217 changes: 28 additions & 189 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ async function sleep(duration) {
}

async function assertStateChangesTo(harness, id, value, action) {
const result = await waitUntilStateChangesTo(harness, id, value, action);

expect(result, `${id} should change to value ${value}`).to.equal(value);
}

async function waitUntilStateChangesTo(harness, id, value, action) {
return new Promise((resolve, reject) => {
const ac = new AbortController();

Expand All @@ -21,14 +27,12 @@ async function assertStateChangesTo(harness, id, value, action) {
}, 10 * 1000);

harness.on('stateChange', async (changedId, state) => {
if (id === changedId && state) {
if (id === changedId && state && state.val === value) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

expect(state.val, `${id} should change to value ${value}`).to.equal(value);

resolve(true);
resolve(state.val);
}
}
}, { signal: ac.signal });
Expand Down Expand Up @@ -96,25 +100,7 @@ tests.integration(path.join(__dirname, '..'), {
});

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down Expand Up @@ -255,25 +241,7 @@ tests.integration(path.join(__dirname, '..'), {
await harness.states.setStateAsync(customNumberObjId, { val: 10, ack: true });

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down Expand Up @@ -379,25 +347,7 @@ tests.integration(path.join(__dirname, '..'), {
await harness.states.setStateAsync(customNumberObjId, { val: 10, ack: true });

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down Expand Up @@ -500,25 +450,7 @@ tests.integration(path.join(__dirname, '..'), {
await harness.states.setStateAsync(customNumberObjId, { val: 10, ack: true });

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down Expand Up @@ -629,25 +561,7 @@ tests.integration(path.join(__dirname, '..'), {
await harness.states.setStateAsync(customNumberObjId, { val: 10, ack: true });

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down Expand Up @@ -762,25 +676,7 @@ tests.integration(path.join(__dirname, '..'), {
await harness.states.setStateAsync(customNumberObjId, { val: 1000, ack: true });

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down Expand Up @@ -863,25 +759,7 @@ tests.integration(path.join(__dirname, '..'), {
});

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand All @@ -899,14 +777,12 @@ tests.integration(path.join(__dirname, '..'), {

const tempId = `${harness.adapterName}.0.temp.count.${customBooleanObjId}`;

for (let i = 0; i < 10; i++) {
await harness.states.setStateAsync(customBooleanObjId, { val: true, ack: true });
await sleep(100);
await harness.states.setStateAsync(customBooleanObjId, { val: false, ack: true });
await sleep(200);
}

await assertStateEquals(harness, `${tempId}.day`, 10);
await assertStateChangesTo(harness, `${tempId}.day`, 10, () => {
for (let i = 0; i < 10; i++) {
harness.states.setState(customBooleanObjId, { val: true, ack: true });
harness.states.setState(customBooleanObjId, { val: false, ack: true });
}
});
});

it('calculation no false', async function () {
Expand All @@ -915,12 +791,11 @@ tests.integration(path.join(__dirname, '..'), {

const tempId = `${harness.adapterName}.0.temp.count.${customBooleanObjId}`;

for (let i = 0; i < 10; i++) {
await harness.states.setStateAsync(customBooleanObjId, { val: true, ack: true });
await sleep(10);
}

await assertStateEquals(harness, `${tempId}.day`, 11);
await assertStateChangesTo(harness, `${tempId}.day`, 11, () => {
for (let i = 0; i < 10; i++) {
harness.states.setState(customBooleanObjId, { val: true, ack: true });
}
});
});
});

Expand Down Expand Up @@ -976,25 +851,7 @@ tests.integration(path.join(__dirname, '..'), {
});

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down Expand Up @@ -1118,25 +975,7 @@ tests.integration(path.join(__dirname, '..'), {
await harness.states.setStateAsync(customNumberObjId2, { val: 50, ack: true });

// Wait for adapter startup
await new Promise((resolve, reject) => {
const ac = new AbortController();

const timeout = setTimeout(() => {
ac.abort();
reject(`Adapter not started in given time range`);
}, 5 * 1000);

harness.on('stateChange', async (id, state) => {
if (id === `${harness.adapterName}.0.info.started` && state && state.val === true) {
if (!ac.signal.aborted) {
clearTimeout(timeout);
ac.abort();

resolve(true);
}
}
}, { signal: ac.signal });

await waitUntilStateChangesTo(harness, `${harness.adapterName}.0.info.started`, true, () => {
harness.startAdapterAndWait();
});
});
Expand Down

0 comments on commit da7e03c

Please sign in to comment.