-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Playwright with Automate support (#355)
* Playwright with Automate support * Adding specs * Adding coverage * Adding coverage * Updating cache changes * Adding readme for POA
- Loading branch information
1 parent
1922eaa
commit d8543d5
Showing
12 changed files
with
865 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
extends: standard | ||
parser: babel-eslint | ||
rules: | ||
prefer-const: off | ||
no-unused-expressions: off | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class Cache { | ||
static CACHE = {}; | ||
static CACHE_TIMEOUT = 5 * 60; // 300 seconds | ||
static TIMEOUT_KEY = 'last_access_time'; | ||
|
||
// Caching Keys | ||
static sessionDetails = 'sessionDetails'; | ||
|
||
static checkTypes(sessionId, property) { | ||
if (typeof sessionId !== 'string') { | ||
throw new TypeError('Argument sessionId should be a string'); | ||
} | ||
if (typeof property !== 'string') { | ||
throw new TypeError('Argument property should be a string'); | ||
} | ||
} | ||
|
||
static setCache(sessionId, property, value) { | ||
this.checkTypes(sessionId, property); | ||
let session = this.CACHE[sessionId] || {}; | ||
session[this.TIMEOUT_KEY] = Math.floor(Date.now() / 1000); | ||
session[property] = value; | ||
this.CACHE[sessionId] = session; | ||
} | ||
|
||
static getCache(sessionId, property) { | ||
this.cleanupCache(); | ||
this.checkTypes(sessionId, property); | ||
/* Below line is covered even then nyc is not able to consider it as coverage */ | ||
/* istanbul ignore next */ | ||
let session = this.CACHE[sessionId] || {}; | ||
return session[property] || null; | ||
} | ||
|
||
static cleanupCache() { | ||
let now = Math.floor(Date.now() / 1000); | ||
for (let sessionId in this.CACHE) { | ||
let session = this.CACHE[sessionId]; | ||
let timestamp = session[this.TIMEOUT_KEY]; | ||
if (now - timestamp >= this.CACHE_TIMEOUT) { | ||
this.CACHE[sessionId] = { | ||
[this.sessionDetails]: session[this.sessionDetails] | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
Cache | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { test, expect } from '@playwright/test'; | ||
import { Cache } from '../cache.js'; | ||
import sinon from 'sinon'; | ||
|
||
test.describe('Cache', () => { | ||
test.afterEach(() => { | ||
sinon.restore(); | ||
Cache.cleanupCache(); | ||
}); | ||
|
||
test.describe('setCache', () => { | ||
test('should set cache correctly', () => { | ||
const sessionId = 'mockSessionId'; | ||
const property = 'mockProperty'; | ||
const value = 'mockValue'; | ||
const now = 1234567890; | ||
|
||
// Stub Date.now() to return a fixed value | ||
sinon.stub(Date, 'now').returns(now * 1000); | ||
|
||
Cache.setCache(sessionId, property, value); | ||
|
||
expect(Cache.CACHE[sessionId]).toEqual({ | ||
[Cache.TIMEOUT_KEY]: now, | ||
[property]: value | ||
}); | ||
}); | ||
|
||
test('should initialize session to empty object if not found in CACHE', () => { | ||
const sessionId = 'nonExistentSessionId'; | ||
const property = 'mockProperty'; | ||
sinon.stub(Date, 'now').returns(1234567890 * 1000); | ||
|
||
Cache.setCache(sessionId, property, 'mockValue'); | ||
|
||
expect(Cache.CACHE[sessionId]).toEqual({ [Cache.TIMEOUT_KEY]: 1234567890, [property]: 'mockValue' }); | ||
}); | ||
}); | ||
|
||
test.describe('getCache', () => { | ||
test('should return null if cache entry does not exist', () => { | ||
const sessionId = 'nonExistentSessionId'; | ||
const property = 'mockProperty'; | ||
const value = Cache.getCache(sessionId, property); | ||
|
||
expect(value).toBeNull(); | ||
}); | ||
|
||
test('should return cached value if cache entry exists', () => { | ||
const sessionId = 'existingSessionId'; | ||
const property = 'mockProperty'; | ||
const cachedValue = 'cachedValue'; | ||
const now = 1234567890; | ||
sinon.stub(Cache, 'cleanupCache'); | ||
|
||
Cache.CACHE[sessionId] = { | ||
[Cache.TIMEOUT_KEY]: now, | ||
[property]: cachedValue | ||
}; | ||
|
||
const value = Cache.getCache(sessionId, property); | ||
|
||
expect(value).toEqual(cachedValue); | ||
}); | ||
}); | ||
|
||
test.describe('cleanupCache', () => { | ||
test('should remove expired cache entries', () => { | ||
const sessionId1 = 'expiredSessionId'; | ||
const sessionId2 = 'validSessionId'; | ||
const property = 'mockProperty'; | ||
const now = 1234567890; | ||
const expiredTime = now - (Cache.CACHE_TIMEOUT + 1); | ||
const validTime = now - (Cache.CACHE_TIMEOUT - 1); | ||
|
||
Cache.CACHE[sessionId1] = { | ||
[Cache.TIMEOUT_KEY]: expiredTime, | ||
[property]: 'expiredValue' | ||
}; | ||
Cache.CACHE[sessionId2] = { | ||
[Cache.TIMEOUT_KEY]: validTime, | ||
[property]: 'validValue' | ||
}; | ||
|
||
sinon.stub(Date, 'now').returns(now * 1000); | ||
|
||
Cache.cleanupCache(); | ||
|
||
expect(Cache.CACHE[sessionId1]).toEqual({ sessionDetails: undefined }); | ||
expect(Cache.CACHE[sessionId2]).toBeDefined(); | ||
}); | ||
}); | ||
|
||
test.describe('checkTypes', () => { | ||
test('should throw TypeError if sessionId is not a string', () => { | ||
expect(() => Cache.checkTypes(123, 'property')).toThrowError(TypeError, 'Argument sessionId should be a string'); | ||
}); | ||
|
||
test('should throw TypeError if property is not a string', () => { | ||
expect(() => Cache.checkTypes('sessionId', 123)).toThrowError(TypeError, 'Argument property should be a string'); | ||
}); | ||
|
||
test('should not throw any error if sessionId and property are strings', () => { | ||
expect(() => Cache.checkTypes('sessionId', 'property')).not.toThrowError(TypeError); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.