From 14f1042e850143f8580ad43b1ec4b755084186aa Mon Sep 17 00:00:00 2001 From: Roger Floriano <31597636+petruki@users.noreply.github.com> Date: Fri, 10 May 2024 21:43:58 -0700 Subject: [PATCH] Added Client.getExecution(switcher) (#187) --- src/client.d.ts | 7 ++++++- src/client.js | 4 ++++ src/switcher.d.ts | 5 +++++ src/switcher.js | 4 ++++ test/switcher-client.test.js | 17 +++++++++++++++++ test/switcher-snapshot.test.js | 22 ++++++++++++++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/client.d.ts b/src/client.d.ts index 010102a..8cef2a0 100644 --- a/src/client.d.ts +++ b/src/client.d.ts @@ -91,10 +91,15 @@ export class Client { static forget(key: string): void; /** - * Retrieve execution log given a switcher key + * Retrieve all execution log given a switcher key */ static getLogger(key: string): LoggerRecord[]; + /** + * Retrieve execution log from a switcher + */ + static getExecution(switcher: Switcher): LoggerRecord; + /** * Clear all results from the execution log */ diff --git a/src/client.js b/src/client.js index 5e60acf..e8837f6 100644 --- a/src/client.js +++ b/src/client.js @@ -219,6 +219,10 @@ export class Client { return ExecutionLogger.getByKey(key); } + static getExecution(switcher) { + return ExecutionLogger.getExecution(switcher.key, switcher.input); + } + static clearLogger() { ExecutionLogger.clearLogger(); } diff --git a/src/switcher.d.ts b/src/switcher.d.ts index 1a0440b..5049aee 100644 --- a/src/switcher.d.ts +++ b/src/switcher.d.ts @@ -81,6 +81,11 @@ export class Switcher { */ checkPayload(input: string): Switcher; + /** + * Return switcher key + */ + get key(): string; + /** * Return switcher current strategy input */ diff --git a/src/switcher.js b/src/switcher.js index 0f754e6..704d906 100644 --- a/src/switcher.js +++ b/src/switcher.js @@ -230,6 +230,10 @@ export class Switcher { return this.#delay == 0 || !ExecutionLogger.getExecution(this.#key, this.#input); } + get key() { + return this.#key; + } + get input() { return this.#input; } diff --git a/test/switcher-client.test.js b/test/switcher-client.test.js index 3fdfb65..c823e01 100644 --- a/test/switcher-client.test.js +++ b/test/switcher-client.test.js @@ -54,6 +54,23 @@ describe('E2E test - Switcher local:', function () { assert.isNotEmpty(Client.getLogger('FF2FOR2020')); }); + it('should get execution from logger', async function () { + await switcher + .checkValue('Japan') + .checkNetwork('10.0.0.3') + .prepare('FF2FOR2020'); + + await switcher.isItOn('FF2FOR2020'); + const log = Client.getExecution(switcher); + + assert.equal(log.key, 'FF2FOR2020'); + assert.sameDeepMembers(log.input, [ + [ 'VALUE_VALIDATION', 'Japan' ], + [ 'NETWORK_VALIDATION', '10.0.0.3' ]]); + assert.equal(log.response.reason, 'Success'); + assert.equal(log.response.result, true); + }); + it('should be valid - isItOn - with detail', async function () { const response = await switcher .detail() diff --git a/test/switcher-snapshot.test.js b/test/switcher-snapshot.test.js index 7e4b55a..138cf5f 100644 --- a/test/switcher-snapshot.test.js +++ b/test/switcher-snapshot.test.js @@ -87,6 +87,28 @@ describe('E2E test - Switcher local - Snapshot:', function () { Client.unloadSnapshot(); }); + it('should update snapshot during load - store file', async function () { + //given + fetchStub = stub(FetchFacade, 'fetch'); + + given(fetchStub, 0, { json: () => generateAuth('[auth_token]', 5), status: 200 }); + given(fetchStub, 1, { json: () => generateStatus(false), status: 200 }); // Snapshot outdated + given(fetchStub, 2, { json: () => JSON.parse(dataJSON), status: 200 }); + + //test + Client.buildContext({ url, apiKey, domain, component, environment }, { + snapshotLocation: 'generated-snapshots/', + local: true, + regexSafe: false + }); + + await Client.loadSnapshot(true, true); + assert.isTrue(existsSync(`generated-snapshots/${environment}.json`)); + + //restore state to avoid process leakage + Client.unloadSnapshot(); + }); + it('should NOT update snapshot', async function () { //given fetchStub = stub(FetchFacade, 'fetch');