Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/switcher-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export class Switcher {
return;
}

Switcher._watcher = Deno.watchFs(`${Switcher._options.snapshotLocation}${Switcher._context.environment}.json`);
const snapshotFile = `${Switcher._options.snapshotLocation}${Switcher._context.environment}.json`;
Switcher._watcher = Deno.watchFs(snapshotFile);
for await (const event of Switcher._watcher) {
const dataString = JSON.stringify(event);
if (Switcher._watchDebounce.has(dataString)) {
Expand Down
4 changes: 2 additions & 2 deletions test/datemoment.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertThrows, assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertThrows, assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from './helper/utils.ts';
import DateMoment from '../src/lib/utils/datemoment.ts';

Expand Down
2 changes: 1 addition & 1 deletion test/helper/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// deno-lint-ignore-file no-explicit-any
import { assertEquals } from "https://deno.land/std@0.176.0/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts";
import * as mf from 'https://deno.land/x/mock_fetch@0.3.0/mod.ts';

export function given(route: string, expect: any, status = 200) {
Expand Down
4 changes: 2 additions & 2 deletions test/strategy-operations/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from '../helper/utils.ts';
import {
OperationsType,
Expand Down
4 changes: 2 additions & 2 deletions test/strategy-operations/network.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from '../helper/utils.ts';
import {
OperationsType,
Expand Down
4 changes: 2 additions & 2 deletions test/strategy-operations/numeric.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from '../helper/utils.ts';
import {
OperationsType,
Expand Down
4 changes: 2 additions & 2 deletions test/strategy-operations/payload.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertFalse, assertArrayIncludes } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertFalse, assertArrayIncludes } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from '../helper/utils.ts';
import {
OperationsType,
Expand Down
4 changes: 2 additions & 2 deletions test/strategy-operations/regex.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from '../helper/utils.ts';
import {
OperationsType,
Expand Down
4 changes: 2 additions & 2 deletions test/strategy-operations/time.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from '../helper/utils.ts';
import {
OperationsType,
Expand Down
4 changes: 2 additions & 2 deletions test/strategy-operations/value.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { describe, it } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertTrue } from '../helper/utils.ts';
import {
OperationsType,
Expand Down
61 changes: 33 additions & 28 deletions test/switcher-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, afterAll, beforeEach, beforeAll } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertEquals, assertRejects, assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it, afterAll, beforeEach, beforeAll } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertEquals, assertRejects, assertFalse, assertExists } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { delay } from 'https://deno.land/std@0.177.0/async/delay.ts';
import { assertTrue } from './helper/utils.ts'

import { StrategiesType } from '../src/lib/snapshot.ts';
Expand All @@ -11,7 +12,7 @@ import {
checkRegex
} from '../mod.ts';

const testSettings = { sanitizeExit: false, sanitizeResources: false };
const testSettings = { sanitizeOps: false, sanitizeResources: false, sanitizeExit: false };

describe('E2E test - Switcher offline:', function () {
let switcher: Switcher;
Expand Down Expand Up @@ -39,7 +40,7 @@ describe('E2E test - Switcher offline:', function () {
switcher = Switcher.factory();
});

it('should be valid - isItOn', async function () {
it('should be valid - isItOn', testSettings, async function () {
await switcher.prepare('FF2FOR2020', [
checkValue('Japan'),
checkNetwork('10.0.0.3')
Expand All @@ -48,7 +49,7 @@ describe('E2E test - Switcher offline:', function () {
assertTrue(await switcher.isItOn('FF2FOR2020'));
});

it('should be valid - No prepare function needed', async function () {
it('should be valid - No prepare function needed', testSettings, async function () {
const result = await switcher.isItOn('FF2FOR2020', [
checkValue('Japan'),
checkNetwork('10.0.0.3')
Expand All @@ -57,22 +58,22 @@ describe('E2E test - Switcher offline:', function () {
assertTrue(result);
});

it('should be valid - No prepare function needed (no input as well)', async function () {
it('should be valid - No prepare function needed (no input as well)', testSettings, async function () {
const result = await switcher.isItOn('FF2FOR2030');
assertTrue(result);
});

it('should be valid - Switcher strategy disabled', async function () {
it('should be valid - Switcher strategy disabled', testSettings, async function () {
const result = await switcher.isItOn('FF2FOR2021', [checkNetwork('192.168.0.1')]);
assertTrue(result);
});

it('should be valid - No Switcher strategy', async function () {
it('should be valid - No Switcher strategy', testSettings, async function () {
const result = await switcher.isItOn('FF2FOR2022');
assertTrue(result);
});

it('should be valid - JSON Payload matches all keys', async function () {
it('should be valid - JSON Payload matches all keys', testSettings, async function () {
await switcher.prepare('FF2FOR2023', [
checkPayload(JSON.stringify({
id: 1,
Expand All @@ -87,7 +88,7 @@ describe('E2E test - Switcher offline:', function () {
assertTrue(result);
});

it('should be invalid - REGEX failed to perform in time', async function () {
it('should be invalid - REGEX failed to perform in time', testSettings, async function () {
const getTimer = (timer: number) => (timer - Date.now()) * -1;

let timer = Date.now();
Expand All @@ -99,7 +100,7 @@ describe('E2E test - Switcher offline:', function () {
assertTrue(timer < 600);
});

it('should be invalid - JSON Payload does NOT match all keys', async function () {
it('should be invalid - JSON Payload does NOT match all keys', testSettings, async function () {
await switcher.prepare('FF2FOR2023', [
checkPayload(JSON.stringify({
id: 1,
Expand All @@ -114,7 +115,7 @@ describe('E2E test - Switcher offline:', function () {
`Strategy '${StrategiesType.PAYLOAD}' does not agree`);
});

it('should be invalid - Input (IP) does not match', async function () {
it('should be invalid - Input (IP) does not match', testSettings, async function () {
await switcher.prepare('FF2FOR2020', [
checkValue('Japan'),
checkNetwork('192.168.0.2')
Expand All @@ -125,25 +126,25 @@ describe('E2E test - Switcher offline:', function () {
`Strategy '${StrategiesType.NETWORK}' does not agree`);
});

it('should be invalid - Input not provided', async function () {
it('should be invalid - Input not provided', testSettings, async function () {
assertFalse(await switcher.isItOn('FF2FOR2020'));
assertEquals(Switcher.getLogger('FF2FOR2020')[0].response.reason,
`Strategy '${StrategiesType.NETWORK}' did not receive any input`);
});

it('should be invalid - Switcher config disabled', async function () {
it('should be invalid - Switcher config disabled', testSettings, async function () {
assertFalse(await switcher.isItOn('FF2FOR2031'));
assertEquals(Switcher.getLogger('FF2FOR2031')[0].response.reason,
'Config disabled');
});

it('should be invalid - Switcher group disabled', async function () {
it('should be invalid - Switcher group disabled', testSettings, async function () {
assertFalse(await switcher.isItOn('FF2FOR2040'));
assertEquals(Switcher.getLogger('FF2FOR2040')[0].response.reason,
'Group disabled');
});

it('should be valid assuming key to be false and then forgetting it', async function () {
it('should be valid assuming key to be false and then forgetting it', testSettings, async function () {
await switcher.prepare('FF2FOR2020', [
checkValue('Japan'),
checkNetwork('10.0.0.3')
Expand All @@ -157,7 +158,7 @@ describe('E2E test - Switcher offline:', function () {
assertTrue(await switcher.isItOn());
});

it('should be valid assuming unknown key to be true', async function () {
it('should be valid assuming unknown key to be true', testSettings, async function () {
await switcher.prepare('UNKNOWN', [
checkValue('Japan'),
checkNetwork('10.0.0.3')
Expand All @@ -172,7 +173,7 @@ describe('E2E test - Switcher offline:', function () {
Error, 'Something went wrong: {"error":"Unable to load a key UNKNOWN"}');
});

it('should enable test mode which will prevent a snapshot to be watchable', async function () {
it('should enable test mode which will prevent a snapshot to be watchable', testSettings, async function () {
//given
Switcher.buildContext({ url, apiKey, domain, component, environment }, {
offline: true, logger: true
Expand All @@ -187,7 +188,21 @@ describe('E2E test - Switcher offline:', function () {
assertTrue(await switcher.isItOn('FF2FOR2020'));
});

it('should be valid - Offline mode', testSettings, async function () {
await delay(2000);

Switcher.buildContext({ url, apiKey, domain, component, environment }, {
offline: true,
snapshotLocation: 'generated-snapshots/'
});

await Switcher.loadSnapshot();
assertExists(Switcher.snapshot);
});

it('should be invalid - Offline mode cannot load snapshot from an invalid path', testSettings, async function () {
await delay(2000);

Switcher.buildContext({ url, apiKey, domain, component, environment }, {
offline: true,
snapshotLocation: '//somewhere/'
Expand All @@ -199,14 +214,4 @@ describe('E2E test - Switcher offline:', function () {
Error, 'Something went wrong: It was not possible to load the file at //somewhere/');
});

it('should be valid - Offline mode', testSettings, function () {
Switcher.buildContext({ url, apiKey, domain, component, environment }, {
offline: true,
snapshotLocation: 'generated-snapshots/'
});

// Igored test: not working
// await Switcher.loadSnapshot();
// assertExists(Switcher.snapshot);
});
});
6 changes: 3 additions & 3 deletions test/switcher-integrated.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// deno-lint-ignore-file no-explicit-any
import { describe, it, afterAll, afterEach, beforeEach } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertEquals, assertNotEquals, assertRejects, assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { assertSpyCalls, spy } from 'https://deno.land/std@0.176.0/testing/mock.ts';
import { describe, it, afterAll, afterEach, beforeEach } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertEquals, assertNotEquals, assertRejects, assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { assertSpyCalls, spy } from 'https://deno.land/std@0.177.0/testing/mock.ts';
import { given, givenError, tearDown, assertTrue, generateAuth, generateResult } from './helper/utils.ts'

import {
Expand Down
43 changes: 29 additions & 14 deletions test/switcher-snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// deno-lint-ignore-file no-explicit-any
import { describe, it, afterAll, beforeEach } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertRejects, assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it, afterAll, beforeEach } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertRejects, assertFalse, assertExists } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { delay } from 'https://deno.land/std@0.177.0/async/delay.ts';
import { existsSync } from 'https://deno.land/std@0.110.0/fs/mod.ts';
import { given, givenError, tearDown, generateAuth, generateStatus } from './helper/utils.ts';
import { given, givenError, tearDown, generateAuth, generateStatus, assertTrue } from './helper/utils.ts';

import { Switcher } from '../mod.ts';

const testSettings = { sanitizeExit: false, sanitizeResources: false };
const testSettings = { sanitizeOps: false, sanitizeResources: false, sanitizeExit: false };

describe('E2E test - Switcher offline - Snapshot:', function () {
const token = '[token]';
Expand All @@ -16,6 +17,8 @@ describe('E2E test - Switcher offline - Snapshot:', function () {
const dataJSON = dataBuffer.toString();

beforeEach(function() {
Switcher.unloadSnapshot();

contextSettings = {
url: 'http://localhost:3000',
apiKey: '[api_key]',
Expand All @@ -38,7 +41,9 @@ describe('E2E test - Switcher offline - Snapshot:', function () {
Deno.removeSync('generated-snapshots/', { recursive: true });
});

it('should update snapshot', testSettings, function () {
it('should update snapshot', testSettings, async function () {
await delay(2000);

//give
given('POST@/criteria/auth', generateAuth(token, 5));
given('GET@/criteria/snapshot_check/:version', generateStatus(false));
Expand All @@ -50,15 +55,16 @@ describe('E2E test - Switcher offline - Snapshot:', function () {
offline: true
});

// Ignored test: not working
// await Switcher.loadSnapshot(true);
// assertTrue(await Switcher.checkSnapshot());
await Switcher.loadSnapshot(true);
assertTrue(await Switcher.checkSnapshot());

// //restore state to avoid process leakage
// Switcher.unloadSnapshot();
//restore state to avoid process leakage
Switcher.unloadSnapshot();
});

it('should NOT update snapshot', testSettings, async function () {
await delay(2000);

//given
given('POST@/criteria/auth', generateAuth(token, 5));
given('GET@/criteria/snapshot_check/:version', generateStatus(true)); // No available update
Expand All @@ -69,6 +75,8 @@ describe('E2E test - Switcher offline - Snapshot:', function () {
});

it('should NOT update snapshot - check Snapshot Error', testSettings, async function () {
await delay(2000);

//given
given('POST@/criteria/auth', generateAuth(token, 5));
givenError('GET@/criteria/snapshot_check/:version', 'ECONNREFUSED');
Expand All @@ -82,6 +90,8 @@ describe('E2E test - Switcher offline - Snapshot:', function () {
});

it('should NOT update snapshot - resolve Snapshot Error', testSettings, async function () {
await delay(2000);

//given
given('POST@/criteria/auth', generateAuth(token, 5));
given('GET@/criteria/snapshot_check/:version', generateStatus(false)); // Snapshot outdated
Expand All @@ -95,7 +105,9 @@ describe('E2E test - Switcher offline - Snapshot:', function () {
Error, 'Something went wrong: Connection has been refused - ECONNREFUSED');
});

it('should update snapshot', testSettings, function () {
it('should update snapshot', testSettings, async function () {
await delay(2000);

//given
given('POST@/criteria/auth', generateAuth(token, 5));
given('GET@/criteria/snapshot_check/:version', generateStatus(false)); // Snapshot outdated
Expand All @@ -106,17 +118,20 @@ describe('E2E test - Switcher offline - Snapshot:', function () {
snapshotLocation: 'generated-snapshots/'
});

// Ignored test: not working
// await Switcher.loadSnapshot();
// assertExists(Switcher.snapshot);
await Switcher.loadSnapshot();
assertExists(Switcher.snapshot);
});

it('should not throw when switcher keys provided were configured properly', testSettings, async function () {
await delay(2000);

await Switcher.loadSnapshot();
await Switcher.checkSwitchers(['FF2FOR2030']);
});

it('should throw when switcher keys provided were not configured properly', testSettings, async function () {
await delay(2000);

await Switcher.loadSnapshot();
await assertRejects(async () =>
await Switcher.checkSwitchers(['FEATURE02']),
Expand Down
4 changes: 2 additions & 2 deletions test/switcher-watch-snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// deno-lint-ignore-file no-explicit-any
import { describe, it, afterAll, beforeEach } from 'https://deno.land/std@0.176.0/testing/bdd.ts';
import { assertEquals, assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { describe, it, afterAll, beforeEach } from 'https://deno.land/std@0.177.0/testing/bdd.ts';
import { assertEquals, assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { existsSync } from 'https://deno.land/std@0.110.0/fs/mod.ts';
import { assertTrue } from './helper/utils.ts';

Expand Down
4 changes: 2 additions & 2 deletions test/timed-match.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, it } from "https://deno.land/std@0.176.0/testing/bdd.ts";
import { assertFalse } from 'https://deno.land/std@0.176.0/testing/asserts.ts';
import { beforeEach, describe, it } from "https://deno.land/std@0.177.0/testing/bdd.ts";
import { assertFalse } from 'https://deno.land/std@0.177.0/testing/asserts.ts';
import { assertTrue } from './helper/utils.ts';
import TimedMatch from "../src/lib/utils/timed-match/index.ts";

Expand Down