Files
aigen/tests/library-media-cache.test.mjs

39 lines
1.4 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync, readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
const root = fileURLToPath(new URL('../server/api/library/', import.meta.url))
function walk(dir) {
const files = []
for (const name of readdirSync(dir)) {
const path = join(dir, name)
if (statSync(path).isDirectory()) files.push(...walk(path))
else if (name.endsWith('.ts')) files.push(path)
}
return files
}
const media = walk(root).filter((path) => (
/[/\\](thumb|preview|video|audio|first-frame|last-frame)\.get\.ts$/.test(path)
|| /[/\\]stills[/\\]\[id\]\.get\.ts$/.test(path)
|| /[/\\]drafts[/\\]\[id\][/\\]still\.get\.ts$/.test(path)
))
test('library media routes are uncached and do not answer 304', () => {
const helper = readFileSync(new URL('../server/utils/library.ts', import.meta.url), 'utf8')
assert.match(helper, /Cache-Control', 'no-store, no-cache, must-revalidate'/)
assert.match(helper, /Pragma', 'no-cache'/)
assert.ok(media.length >= 8, media.join('\n'))
for (const path of media) {
const source = readFileSync(path, 'utf8')
assert.match(source, /setLibraryMediaHeaders/, path)
assert.doesNotMatch(source, /304/, path)
assert.doesNotMatch(source, /if-none-match/i, path)
assert.doesNotMatch(source, /max-age=\d+/, path)
assert.doesNotMatch(source, /public, max-age/, path)
}
})