Skip to content

Commit

Permalink
Added Client.getExecution(switcher) (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored May 11, 2024
1 parent 1ece832 commit 14f1042
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions src/switcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class Switcher {
*/
checkPayload(input: string): Switcher;

/**
* Return switcher key
*/
get key(): string;

/**
* Return switcher current strategy input
*/
Expand Down
4 changes: 4 additions & 0 deletions src/switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions test/switcher-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions test/switcher-snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 14f1042

Please sign in to comment.