Skip to content

Commit

Permalink
cleanup old tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Sep 20, 2023
1 parent 155952e commit 457c0aa
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 74 deletions.
16 changes: 8 additions & 8 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ export interface DurableCache {
export type StorageHandlerBackedCacheArgs = {
storageHandler: WrappedStorageHandler,
eventBus: EventBus,
domain: string,
cookieDomain: string,
}

export class StorageHandlerBackedCache implements DurableCache {
private handler
private domain
private cookieDomain
private eventBus

constructor (opts: StorageHandlerBackedCacheArgs) {
this.handler = opts.storageHandler
this.domain = opts.domain
this.cookieDomain = opts.cookieDomain
this.eventBus = opts.eventBus
}

private deleteCookie(key: string): void {
this.handler.setCookie(key, '', new Date(0), 'Lax', this.domain)
this.handler.setCookie(key, '', new Date(0), 'Lax', this.cookieDomain)
}

// layout: { w: writtenAt in millis, e? : expiresAt in millis }
Expand Down Expand Up @@ -161,8 +161,8 @@ export class StorageHandlerBackedCache implements DurableCache {
return cookieRecord
} else {
// ls record is newer. Update cookie record
this.handler.setCookie(key, lsRecord.data, lsRecord.meta.expiresAt, 'Lax', this.domain)
this.handler.setCookie(metaRecordKey, this.serializeMetaRecord(lsRecord.meta), lsRecord.meta.expiresAt, 'Lax', this.domain)
this.handler.setCookie(key, lsRecord.data, lsRecord.meta.expiresAt, 'Lax', this.cookieDomain)
this.handler.setCookie(metaRecordKey, this.serializeMetaRecord(lsRecord.meta), lsRecord.meta.expiresAt, 'Lax', this.cookieDomain)
return lsRecord
}
} else if (cookieRecord) {
Expand All @@ -172,8 +172,8 @@ export class StorageHandlerBackedCache implements DurableCache {
return cookieRecord
} else if (lsRecord) {
// only ls record exists. Write to cookie
this.handler.setCookie(key, lsRecord.data, lsRecord.meta.expiresAt, 'Lax', this.domain)
this.handler.setCookie(metaRecordKey, this.serializeMetaRecord(lsRecord.meta), lsRecord.meta.expiresAt, 'Lax', this.domain)
this.handler.setCookie(key, lsRecord.data, lsRecord.meta.expiresAt, 'Lax', this.cookieDomain)
this.handler.setCookie(metaRecordKey, this.serializeMetaRecord(lsRecord.meta), lsRecord.meta.expiresAt, 'Lax', this.cookieDomain)
return lsRecord
} else {
return null
Expand Down
14 changes: 7 additions & 7 deletions src/enrichers/decisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getQueryParameter, ParsedParam } from '../utils/url'
import { trim, isUUID, expiresInDays } from 'live-connect-common'
import { WrappedStorageHandler } from '../handlers/storage-handler'

type Input = { pageUrl?: string, domain: string }
type Input = { pageUrl?: string, cookieDomain: string }
type Output = { decisionIds: string[] }

const DEFAULT_DECISION_ID_COOKIE_EXPIRES = expiresInDays(30)
Expand All @@ -18,18 +18,18 @@ export function enrichDecisionIds(
eventBus: EventBus
): Enricher<Input, Output> {
return state => {
function _addDecisionId(key: string, cookieDomain?: string) {
function addDecisionId(key: string) {
if (key) {
storageHandler.setCookie(
`${DECISION_ID_COOKIE_NAMESPACE}${key}`,
key,
DEFAULT_DECISION_ID_COOKIE_EXPIRES,
'Lax',
cookieDomain)
state.cookieDomain)
}
}

function _orElseEmpty<A>(errorDescription: string, f: () => A[]): A[] {
function orElseEmpty<A>(errorDescription: string, f: () => A[]): A[] {
try {
return f()
} catch (e) {
Expand All @@ -38,7 +38,7 @@ export function enrichDecisionIds(
}
}

const freshDecisions = _orElseEmpty(
const freshDecisions = orElseEmpty(
'Error while extracting new decision ids',
() => {
const extractedFreshDecisions = ([] as ParsedParam[]).concat((state.pageUrl && getQueryParameter(state.pageUrl, DECISION_ID_QUERY_PARAM_NAME)) || [])
Expand All @@ -50,7 +50,7 @@ export function enrichDecisionIds(
}
)

const storedDecisions = _orElseEmpty(
const storedDecisions = orElseEmpty(
'Error while retrieving stored decision ids',
() => {
const extractedStoredDecisions = storageHandler.findSimilarCookies(DECISION_ID_COOKIE_NAMESPACE)
Expand All @@ -63,7 +63,7 @@ export function enrichDecisionIds(

freshDecisions.forEach(decision => {
try {
_addDecisionId(decision, state.domain)
addDecisionId(decision)
} catch (e) {
eventBus.emitErrorWithMessage('DecisionsResolve', 'Error while storing new decision id', e)
}
Expand Down
6 changes: 3 additions & 3 deletions src/enrichers/domain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { WrappedStorageHandler } from '../handlers/storage-handler'
import { Enricher } from '../types'
import { determineHighestAccessibleDomain } from '../utils/domain'
import { determineHighestWritableDomain } from '../utils/domain'

type Input = object
type Output = { domain: string }
type Output = { cookieDomain: string }

export function enrichDomain(storageHandler: WrappedStorageHandler): Enricher<Input, Output> {
return state => ({ ...state, domain: determineHighestAccessibleDomain(storageHandler) })
return state => ({ ...state, cookieDomain: determineHighestWritableDomain(storageHandler) })
}
4 changes: 2 additions & 2 deletions src/enrichers/live-connect-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DurableCache } from '../cache'

const NEXT_GEN_FP_NAME = '_lc2_fpi'

type Input = { domain: string }
type Input = { cookieDomain: string }
type Output = { liveConnectId?: string, peopleVerifiedId?: string }

export function enrichLiveConnectId(
Expand All @@ -28,7 +28,7 @@ export function enrichLiveConnectId(
cache.set(NEXT_GEN_FP_NAME, legacyValue)
liveConnectIdentifier = legacyValue
} else {
const newValue = `${domainHash(state.domain)}--${ulid()}`.toLocaleLowerCase()
const newValue = `${domainHash(state.cookieDomain)}--${ulid()}`.toLocaleLowerCase()
// will also set cookie. It will also later be extended by cookie bouncing in the cff.
cache.set(NEXT_GEN_FP_NAME, newValue)
// handle case when all storage backends are disabled
Expand Down
6 changes: 3 additions & 3 deletions src/idex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class IdentityResolver {
}
}

unsafeResolve(successCallback: (result: unknown, meta: ResolutionMetadata) => void, errorCallback: () => void, additionalParams: ResolutionParams): void {
unsafeResolve(successCallback: (result: unknown, meta: ResolutionMetadata) => void, errorCallback: (e: unknown) => void, additionalParams: ResolutionParams): void {
const cachedValue = this.getCached(additionalParams)
if (cachedValue) {
successCallback(...cachedValue)
Expand All @@ -149,13 +149,13 @@ export class IdentityResolver {
return `${this.url}/${this.source}/${this.publisherId}${params}`
}

resolve(successCallback: (result: unknown, meta: ResolutionMetadata) => void, errorCallback?: () => void, additionalParams?: ResolutionParams): void {
resolve(successCallback: (result: unknown, meta: ResolutionMetadata) => void, errorCallback?: (e: unknown) => void, additionalParams?: ResolutionParams): void {
try {
this.unsafeResolve(successCallback, errorCallback || (() => {}), additionalParams || {})
} catch (e) {
console.error('IdentityResolve', e)
if (errorCallback && isFunction(errorCallback)) {
errorCallback()
errorCallback(e)
}
if (this.eventBus) {
this.eventBus.emitError('IdentityResolve', e)
Expand Down
2 changes: 1 addition & 1 deletion src/standard-live-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function _standardInitialization(liveConnectConfig: LiveConnectConfig, externalS
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: stateWithDomain.domain
cookieDomain: stateWithDomain.cookieDomain
})

const enrichedState =
Expand Down
2 changes: 1 addition & 1 deletion src/utils/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { loadedDomain } from './page'

const TLD_CACHE_KEY = '_li_dcdm_c'

export function determineHighestAccessibleDomain(storageHandler: WrappedStorageHandler): string {
export function determineHighestWritableDomain(storageHandler: WrappedStorageHandler): string {
const cachedDomain = storageHandler.getCookie(TLD_CACHE_KEY)
if (cachedDomain) {
return cachedDomain
Expand Down
24 changes: 12 additions & 12 deletions test/unit/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(1))
Expand All @@ -65,7 +65,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(1))
Expand All @@ -83,7 +83,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(1))
Expand All @@ -100,7 +100,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(1))
Expand All @@ -115,7 +115,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key_any', 'value_any', expiresInDays(1))
Expand All @@ -138,7 +138,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(-1))
Expand All @@ -151,7 +151,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(-1))
Expand All @@ -164,7 +164,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

const expires = expiresInDays(5)
Expand All @@ -181,7 +181,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

const now = new Date()
Expand All @@ -198,7 +198,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(5))
Expand All @@ -219,7 +219,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(5))
Expand All @@ -236,7 +236,7 @@ describe('Cache', () => {
const cache = new StorageHandlerBackedCache({
storageHandler,
eventBus,
domain: 'example.com'
cookieDomain: 'example.com'
})

cache.set('key', 'value', expiresInDays(5))
Expand Down
6 changes: 3 additions & 3 deletions test/unit/enricher/domain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe('TLD checker', () => {

it('should determine correct tld', () => {
const resolved = enrichDomain(storageHandler)(input)
expect(resolved.domain).to.eq('.example.com')
expect(resolved.cookieDomain).to.eq('.example.com')
})

it('should reuse the cached correct tld', () => {
storageHandler.setCookie('_li_dcdm_c', '.example.com')
const resolved = enrichDomain(storageHandler)(input)
expect(resolved.domain).to.eq('.example.com')
expect(resolved.cookieDomain).to.eq('.example.com')
})
})

Expand All @@ -37,6 +37,6 @@ describe('TLD on sub-domain', () => {

it('should use the full domain', () => {
const resolved = enrichDomain(storageHandler)(input)
expect(resolved.domain).to.eq('.example.co.uk')
expect(resolved.cookieDomain).to.eq('.example.co.uk')
})
})
6 changes: 3 additions & 3 deletions test/unit/enricher/live-connect-id.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { expiresInHours } from 'live-connect-common'
use(dirtyChai)

function makeDeps(strategy: StorageStrategy = StorageStrategies.cookie) {
const domain = '.example.com'
const cookieDomain = '.example.com'
const eventBus = LocalEventBus()
const storage = new DefaultStorageHandler(eventBus)
const storageHandler = WrappedStorageHandler.make(strategy, storage, eventBus)
const cache = new StorageHandlerBackedCache({ storageHandler, domain, eventBus })
return { storageHandler, cache, domain }
const cache = new StorageHandlerBackedCache({ storageHandler, cookieDomain, eventBus })
return { storageHandler, cache, cookieDomain }
}

describe('LiveConnectIdEnricher', () => {
Expand Down
Loading

0 comments on commit 457c0aa

Please sign in to comment.