Extract @ipp/core npm workspace package
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { KeyType } from '@ipp/core'
|
||||
import { getShareByKey, utcBucketKey } from '../src/immich'
|
||||
import { KeyType } from '../src/types'
|
||||
|
||||
// 3.0-shaped responses: album `/shared-links/me` returns an empty assets[],
|
||||
// and the album's assets are enumerated from the timeline API. This exercises
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { Asset, AssetType, KeyType, loadConfig, SharedLink } from '@ipp/core'
|
||||
import { buildAssetMetadata } from '../src/gallery/metadata'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { Asset, AssetType, KeyType, SharedLink } from '../src/types'
|
||||
|
||||
// buildAssetMetadata is the lazy-flow counterpart to the gallery builder's
|
||||
// per-item baking; it must apply the same showMetadata kill-switch and
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { Asset, AssetType, KeyType, loadConfig } from '@ipp/core'
|
||||
import { Writable } from 'stream'
|
||||
import type { Request, Response } from 'express-serve-static-core'
|
||||
import { assetBuffer } from '../src/stream/asset'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { Asset, AssetType, ImageSize, IncomingShareRequest, KeyType } from '../src/types'
|
||||
import { ImageSize, IncomingShareRequest } from '../src/types'
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { Asset, AssetType, KeyType } from '@ipp/core'
|
||||
import { dateSortComparator } from '../src/gallery/builder'
|
||||
import { Asset, AssetType, KeyType } from '../src/types'
|
||||
|
||||
/*
|
||||
The date-grouping sort must follow the album's own order while
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { Asset, AssetType, KeyType, loadConfig, SharedLink } from '@ipp/core'
|
||||
import { Writable } from 'stream'
|
||||
import { once } from 'events'
|
||||
import type { Response } from 'express-serve-static-core'
|
||||
import { downloadAssets } from '../src/stream/download'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { Asset, AssetType, KeyType, SharedLink } from '../src/types'
|
||||
|
||||
/*
|
||||
"Download all" streams each asset from Immich straight into the zip on the
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { AssetType, ImageSize, KeyType } from '../src/types'
|
||||
import type { Asset } from '../src/types'
|
||||
import { AssetType, KeyType } from '@ipp/core'
|
||||
import type { Asset } from '@ipp/core'
|
||||
import { ImageSize } from '../src/types'
|
||||
import { getFilename } from '../src/gallery/filename'
|
||||
|
||||
/*
|
||||
getFilename reads `ipp.downloadedFilename` via getConfigOption; mock the
|
||||
config-access module so tests exercise the default (original filename) mode
|
||||
getFilename reads `ipp.downloadedFilename` via getConfigOption; mock it in
|
||||
@ipp/core so tests exercise the default (original filename) mode
|
||||
without loading a real config file.
|
||||
*/
|
||||
vi.mock('../src/config/access', () => ({
|
||||
vi.mock('@ipp/core', async (importOriginal) => ({
|
||||
...await importOriginal<typeof import('@ipp/core')>(),
|
||||
getConfigOption: (_path: string, fallback?: unknown) => fallback
|
||||
}))
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
|
||||
import express from 'express'
|
||||
import type { Server } from 'http'
|
||||
import type { AddressInfo } from 'net'
|
||||
import { asyncHandler, errorHandler } from '../src/http'
|
||||
|
||||
/*
|
||||
Regression tests for the per-request crash class: under Express 4 a rejected
|
||||
promise in an async route became an unhandledRejection, and index.ts used to
|
||||
respond to that with process.exit(1) - one bad request took down the whole
|
||||
container. asyncHandler + errorHandler must instead turn any thrown/rejected
|
||||
route error into a plain 404 (the privacy policy: no upstream detail) while
|
||||
the server keeps serving.
|
||||
*/
|
||||
|
||||
describe('asyncHandler + errorHandler', () => {
|
||||
let server: Server
|
||||
let base: string
|
||||
|
||||
beforeAll(async () => {
|
||||
// Silence errorHandler's expected server-side logging during these tests
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
|
||||
const app = express()
|
||||
app.get('/ok', asyncHandler(async (_req, res) => { res.send('ok') }))
|
||||
app.get('/boom', asyncHandler(async () => {
|
||||
throw new TypeError("Cannot read properties of undefined (reading 'headers')")
|
||||
}))
|
||||
app.get('/mid-stream', asyncHandler(async (_req, res) => {
|
||||
res.status(200)
|
||||
res.write('partial')
|
||||
throw new Error('upstream died mid-response')
|
||||
}))
|
||||
app.use(errorHandler)
|
||||
|
||||
await new Promise<void>(resolve => {
|
||||
server = app.listen(0, resolve)
|
||||
})
|
||||
base = 'http://127.0.0.1:' + (server.address() as AddressInfo).port
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
server?.close()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('turns a rejected route promise into a 404 with an empty body', async () => {
|
||||
const res = await fetch(base + '/boom')
|
||||
expect(res.status).toBe(404)
|
||||
expect(await res.text()).toBe('')
|
||||
})
|
||||
|
||||
it('keeps serving other requests after a route has thrown', async () => {
|
||||
await fetch(base + '/boom')
|
||||
const res = await fetch(base + '/ok')
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.text()).toBe('ok')
|
||||
})
|
||||
|
||||
it('does not leak error detail to the client', async () => {
|
||||
const res = await fetch(base + '/boom')
|
||||
const body = await res.text()
|
||||
expect(body).not.toContain('TypeError')
|
||||
expect(body).not.toContain('headers')
|
||||
})
|
||||
|
||||
it('ends the response when the error hits after headers were sent', async () => {
|
||||
const res = await fetch(base + '/mid-stream')
|
||||
// Status was already committed before the throw; the handler can only
|
||||
// end the stream, not rewrite it to a 404.
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.text()).toBe('partial')
|
||||
// And the server is still alive afterwards
|
||||
const ok = await fetch(base + '/ok')
|
||||
expect(ok.status).toBe(200)
|
||||
})
|
||||
})
|
||||
@@ -1,9 +1,8 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { KeyType, loadConfig, SharedLink } from '@ipp/core'
|
||||
import dayjs from 'dayjs'
|
||||
import 'dayjs/locale/de' // load German so the localised-format assertions are deterministic
|
||||
import { expiryDate } from '../src/share'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { KeyType, SharedLink } from '../src/types'
|
||||
|
||||
// expiryDate formats the share's Immich expiry for the gallery subtitle. It is
|
||||
// gated behind ipp.gallery.showExpiryDate (default off) and formatted with
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { renderPage } from '@ipp/core'
|
||||
import { h } from 'preact'
|
||||
import { Gallery, GalleryProps, GalleryItem } from '../src/view/gallery'
|
||||
import { renderPage } from '../src/view/render'
|
||||
|
||||
/*
|
||||
Regression test for the init-JSON XSS: asset strings (originalFileName, EXIF
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { getImmichVersion, isImmichVersionSupported, MIN_IMMICH_VERSION } from '../src/immich'
|
||||
|
||||
// The startup guard that refuses to run against an Immich server older than
|
||||
// IPP supports. The floor is 2.0.0 - the columnar timeline API IPP relies on
|
||||
// to enumerate album shares landed in Immich 2.0.
|
||||
|
||||
function jsonResponse (body: unknown, status = 200) {
|
||||
return {
|
||||
ok: status >= 200 && status < 300,
|
||||
status,
|
||||
headers: { get: (h: string) => (h.toLowerCase() === 'content-type' ? 'application/json' : null) },
|
||||
json: async () => body,
|
||||
text: async () => JSON.stringify(body)
|
||||
}
|
||||
}
|
||||
|
||||
describe('isImmichVersionSupported', () => {
|
||||
it('accepts exactly the minimum version', () => {
|
||||
expect(isImmichVersionSupported(MIN_IMMICH_VERSION)).toBe(true)
|
||||
})
|
||||
|
||||
it('accepts newer major/minor/patch across the 2.x and 3.x lines', () => {
|
||||
expect(isImmichVersionSupported({ major: 2, minor: 7, patch: 5 })).toBe(true)
|
||||
expect(isImmichVersionSupported({ major: 3, minor: 0, patch: 2 })).toBe(true)
|
||||
expect(isImmichVersionSupported({ major: 4, minor: 0, patch: 0 })).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects 1.x, which predates the columnar timeline API', () => {
|
||||
expect(isImmichVersionSupported({ major: 1, minor: 137, patch: 3 })).toBe(false)
|
||||
expect(isImmichVersionSupported({ major: 1, minor: 125, patch: 7 })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getImmichVersion', () => {
|
||||
beforeEach(() => {
|
||||
process.env.IMMICH_URL = 'http://immich.test'
|
||||
})
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('parses the /server/version response', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async (url: string) => {
|
||||
if (url.includes('/server/version')) return jsonResponse({ major: 3, minor: 0, patch: 2, prerelease: null })
|
||||
throw new Error('Unexpected fetch to ' + url)
|
||||
}))
|
||||
expect(await getImmichVersion()).toEqual({ major: 3, minor: 0, patch: 2 })
|
||||
})
|
||||
|
||||
it('returns null when Immich is unreachable', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => { throw new Error('ECONNREFUSED') }))
|
||||
expect(await getImmichVersion()).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null on an unexpected response shape', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ version: 'v3.0.2' })))
|
||||
expect(await getImmichVersion()).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { AssetType, KeyType } from '../src/types'
|
||||
import type { Asset, SharedLink } from '../src/types'
|
||||
import { AssetType, KeyType } from '@ipp/core'
|
||||
import type { Asset, SharedLink } from '@ipp/core'
|
||||
import type { Response } from 'express-serve-static-core'
|
||||
import { findMotionPhotoStill } from '../src/share'
|
||||
import { gallery } from '../src/gallery/builder'
|
||||
@@ -11,7 +11,8 @@ import { gallery } from '../src/gallery/builder'
|
||||
is on. Config is mocked per test rather than loaded from a file.
|
||||
*/
|
||||
const cfg: Record<string, unknown> = {}
|
||||
vi.mock('../src/config/access', () => ({
|
||||
vi.mock('@ipp/core', async (importOriginal) => ({
|
||||
...await importOriginal<typeof import('@ipp/core')>(),
|
||||
getConfigOption: (path: string, fallback?: unknown) => (path in cfg ? cfg[path] : fallback),
|
||||
getNumericConfigOption: (path: string, fallback: number) => (path in cfg ? Number(cfg[path]) : fallback)
|
||||
}))
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
import { describe, it, expect, afterEach } from 'vitest'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { getNumericConfigOption } from '../src/config/access'
|
||||
import { createLimiter } from '../src/utils/limiter'
|
||||
|
||||
/*
|
||||
Regression tests for the non-numeric concurrency bug: a non-numeric
|
||||
`ipp.downloadFromImmichConcurrencyLimit` used to reach createLimiter as NaN,
|
||||
where `active >= NaN` is always false - so the limiter never throttled and
|
||||
every asset in a zip download hit Immich at once.
|
||||
*/
|
||||
|
||||
function loadConfigFrom (config: Record<string, unknown>) {
|
||||
process.env.CONFIG = JSON.stringify(config)
|
||||
loadConfig()
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
delete process.env.CONFIG
|
||||
loadConfig()
|
||||
})
|
||||
|
||||
describe('getNumericConfigOption', () => {
|
||||
it('returns a configured number', () => {
|
||||
loadConfigFrom({ ipp: { downloadFromImmichConcurrencyLimit: 5 } })
|
||||
expect(getNumericConfigOption('ipp.downloadFromImmichConcurrencyLimit', 20)).toBe(5)
|
||||
})
|
||||
|
||||
it('coerces a numeric string (docker-compose env values arrive as strings)', () => {
|
||||
loadConfigFrom({ ipp: { downloadFromImmichConcurrencyLimit: '5' } })
|
||||
expect(getNumericConfigOption('ipp.downloadFromImmichConcurrencyLimit', 20)).toBe(5)
|
||||
})
|
||||
|
||||
it('falls back to the default for a non-numeric value instead of NaN', () => {
|
||||
loadConfigFrom({ ipp: { downloadFromImmichConcurrencyLimit: 'lots' } })
|
||||
expect(getNumericConfigOption('ipp.downloadFromImmichConcurrencyLimit', 20)).toBe(20)
|
||||
})
|
||||
|
||||
it('falls back to the default for non-scalar values', () => {
|
||||
loadConfigFrom({ ipp: { downloadFromImmichConcurrencyLimit: { max: 5 } } })
|
||||
expect(getNumericConfigOption('ipp.downloadFromImmichConcurrencyLimit', 20)).toBe(20)
|
||||
})
|
||||
|
||||
it('falls back to the default when the option is unset', () => {
|
||||
loadConfigFrom({})
|
||||
expect(getNumericConfigOption('ipp.downloadFromImmichConcurrencyLimit', 20)).toBe(20)
|
||||
})
|
||||
})
|
||||
|
||||
describe('createLimiter with guarded config', () => {
|
||||
/** Run `total` tasks through the limiter and report the concurrency peak */
|
||||
async function peakConcurrency (limit: number, total: number): Promise<number> {
|
||||
const run = createLimiter(limit)
|
||||
let active = 0
|
||||
let peak = 0
|
||||
await Promise.all(Array.from({ length: total }, () => run(async () => {
|
||||
active++
|
||||
peak = Math.max(peak, active)
|
||||
// Yield so other queued tasks get a chance to start while this one is "active"
|
||||
await new Promise(resolve => setTimeout(resolve, 1))
|
||||
active--
|
||||
})))
|
||||
return peak
|
||||
}
|
||||
|
||||
it('a NaN limit disables throttling (the failure mode being guarded against)', async () => {
|
||||
expect(await peakConcurrency(NaN, 10)).toBe(10)
|
||||
})
|
||||
|
||||
it('the guarded value throttles as configured', async () => {
|
||||
loadConfigFrom({ ipp: { downloadFromImmichConcurrencyLimit: 'lots' } })
|
||||
const limit = Math.max(1, getNumericConfigOption('ipp.downloadFromImmichConcurrencyLimit', 3))
|
||||
expect(await peakConcurrency(limit, 10)).toBe(3)
|
||||
})
|
||||
})
|
||||
@@ -1,16 +1,18 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { AssetType, ImageSize } from '../src/types'
|
||||
import type { Asset } from '../src/types'
|
||||
import { AssetType } from '@ipp/core'
|
||||
import type { Asset } from '@ipp/core'
|
||||
import { ImageSize } from '../src/types'
|
||||
import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint, requiresOriginal } from '../src/gallery/sizing'
|
||||
|
||||
/*
|
||||
resolveImageEndpoint reads `ipp.maxDownloadQuality` and `ipp.maxZoomQuality`
|
||||
via getConfigOption, so we mock the config-access module and set the tiers per
|
||||
via getConfigOption, so we mock it in @ipp/core and set the tiers per
|
||||
test rather than loading a real config file. vitest hoists vi.mock above the
|
||||
imports, so the mock is in place by the time sizing.ts resolves config/access.
|
||||
imports, so the mock is in place by the time sizing.ts resolves @ipp/core.
|
||||
*/
|
||||
const cfg: Record<string, unknown> = {}
|
||||
vi.mock('../src/config/access', () => ({
|
||||
vi.mock('@ipp/core', async (importOriginal) => ({
|
||||
...await importOriginal<typeof import('@ipp/core')>(),
|
||||
getConfigOption: (path: string, fallback?: unknown) =>
|
||||
path in cfg ? cfg[path] : fallback
|
||||
}))
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { escapeHtml, jsonForInlineScript } from '../src/utils/text'
|
||||
|
||||
describe('escapeHtml', () => {
|
||||
it('escapes the five HTML-significant characters', () => {
|
||||
expect(escapeHtml('<script>alert("xss")</script>')).toBe(
|
||||
'<script>alert("xss")</script>'
|
||||
)
|
||||
})
|
||||
|
||||
it('escapes ampersand first so other escapes are not re-escaped', () => {
|
||||
expect(escapeHtml('&')).toBe('&amp;')
|
||||
})
|
||||
|
||||
it('escapes single quotes', () => {
|
||||
expect(escapeHtml("it's")).toBe('it's')
|
||||
})
|
||||
|
||||
it('returns empty string unchanged', () => {
|
||||
expect(escapeHtml('')).toBe('')
|
||||
})
|
||||
|
||||
it('passes through strings with no special characters', () => {
|
||||
expect(escapeHtml('plain text 123')).toBe('plain text 123')
|
||||
})
|
||||
})
|
||||
|
||||
describe('jsonForInlineScript', () => {
|
||||
it('escapes < so </script> cannot terminate an inline script block', () => {
|
||||
const out = jsonForInlineScript({ name: '</script><script>alert(1)</script>' })
|
||||
expect(out).not.toContain('<')
|
||||
expect(out).toContain('\\u003c/script>')
|
||||
})
|
||||
|
||||
it('round-trips through JSON.parse unchanged', () => {
|
||||
const value = {
|
||||
name: '</script> & <!-- tricky --> \u2028\u2029',
|
||||
n: 3,
|
||||
nested: { arr: [1, 'two'] }
|
||||
}
|
||||
expect(JSON.parse(jsonForInlineScript(value))).toEqual(value)
|
||||
})
|
||||
|
||||
it('escapes the JS line separators U+2028 and U+2029', () => {
|
||||
const out = jsonForInlineScript({ s: '\u2028\u2029' })
|
||||
expect(out).toBe('{"s":"\\u2028\\u2029"}')
|
||||
})
|
||||
|
||||
it('matches JSON.stringify for benign values', () => {
|
||||
const value = { a: 1, b: 'plain text', c: null }
|
||||
expect(jsonForInlineScript(value)).toBe(JSON.stringify(value))
|
||||
})
|
||||
})
|
||||
@@ -1,110 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { TtlLruCache } from '../src/utils/ttlLruCache'
|
||||
|
||||
/*
|
||||
All TTL-based assertions rely on Vitest's fake-timer system mocking
|
||||
`Date.now()`. `vi.advanceTimersByTime` advances both the timer queue
|
||||
and the system clock the cache reads from.
|
||||
*/
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
describe('TtlLruCache', () => {
|
||||
it('returns undefined for an unknown key', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 1000, max: 10 })
|
||||
expect(cache.get('missing')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns the value after set', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 1000, max: 10 })
|
||||
cache.set('a', 'apple')
|
||||
expect(cache.get('a')).toBe('apple')
|
||||
})
|
||||
|
||||
it('returns undefined exactly at the TTL boundary', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 1000, max: 10 })
|
||||
cache.set('a', 'apple')
|
||||
vi.advanceTimersByTime(999)
|
||||
expect(cache.get('a')).toBe('apple')
|
||||
vi.advanceTimersByTime(1)
|
||||
expect(cache.get('a')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('drops expired entries on access so they do not count toward the size cap', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 100, max: 2 })
|
||||
cache.set('a', '1')
|
||||
vi.advanceTimersByTime(200)
|
||||
// Read once so the expired entry is dropped from the internal map.
|
||||
expect(cache.get('a')).toBeUndefined()
|
||||
// The cache should now have capacity for two fresh entries with no
|
||||
// collateral eviction of either.
|
||||
cache.set('b', '2')
|
||||
cache.set('c', '3')
|
||||
expect(cache.get('b')).toBe('2')
|
||||
expect(cache.get('c')).toBe('3')
|
||||
})
|
||||
})
|
||||
|
||||
describe('TtlLruCache delete', () => {
|
||||
it('removes an existing entry', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 1000, max: 10 })
|
||||
cache.set('a', '1')
|
||||
cache.delete('a')
|
||||
expect(cache.get('a')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('is a no-op for a missing key', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 1000, max: 10 })
|
||||
expect(() => cache.delete('missing')).not.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('TtlLruCache LRU eviction', () => {
|
||||
it('evicts the oldest entry when set causes overflow', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 10_000, max: 2 })
|
||||
cache.set('a', '1')
|
||||
cache.set('b', '2')
|
||||
cache.set('c', '3') // overflow - 'a' is the oldest, gets evicted
|
||||
expect(cache.get('a')).toBeUndefined()
|
||||
expect(cache.get('b')).toBe('2')
|
||||
expect(cache.get('c')).toBe('3')
|
||||
})
|
||||
|
||||
it('get refreshes LRU position so a recently-read entry survives eviction', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 10_000, max: 2 })
|
||||
cache.set('a', '1')
|
||||
cache.set('b', '2')
|
||||
// Touch 'a' so it becomes the most-recently-used. 'b' is now oldest.
|
||||
cache.get('a')
|
||||
cache.set('c', '3') // overflow - 'b' should be evicted, not 'a'
|
||||
expect(cache.get('a')).toBe('1')
|
||||
expect(cache.get('b')).toBeUndefined()
|
||||
expect(cache.get('c')).toBe('3')
|
||||
})
|
||||
})
|
||||
|
||||
describe('TtlLruCache re-set', () => {
|
||||
it('replaces the value when set is called twice for the same key', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 1000, max: 10 })
|
||||
cache.set('a', '1')
|
||||
cache.set('a', '2')
|
||||
expect(cache.get('a')).toBe('2')
|
||||
})
|
||||
|
||||
it('resets the TTL on re-set', () => {
|
||||
const cache = new TtlLruCache<string>({ ttlMs: 1000, max: 10 })
|
||||
cache.set('a', '1')
|
||||
vi.advanceTimersByTime(800)
|
||||
cache.set('a', '2') // bumps expiry to 1000ms from now
|
||||
vi.advanceTimersByTime(800)
|
||||
// 800 + 800 = 1600ms past the original set, but only 800ms past the
|
||||
// re-set, so the entry should still be live.
|
||||
expect(cache.get('a')).toBe('2')
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { Asset, AssetType, KeyType } from '@ipp/core'
|
||||
import { getVideoContentType } from '../src/immich'
|
||||
import { Asset, AssetType, KeyType } from '../src/types'
|
||||
|
||||
const video: Asset = {
|
||||
id: 'v1',
|
||||
|
||||
Reference in New Issue
Block a user