This commit is contained in:
Meghdad committed 2026-07-14 12:51:36 +03:30
commit f035fcef4f
134 files changed
+18678

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
import { mkdtemp, mkdir, stat, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { writeCacheMetadata } from '@easy-stream/media';
import { describe, expect, it } from 'vitest';
import { sweepCache } from '../src/cache-lifecycle.js';
import { readRegistry, updateRegistry } from '../src/registry.js';
describe('worker cache lifecycle', () => {
it('evicts to the low watermark, removes registry rows, and prunes font orphans', async () => {
const base = await mkdtemp(path.join(os.tmpdir(), 'easy-stream-cache-sweep-'));
const cacheRoot = path.join(base, 'cache');
const generations = path.join(cacheRoot, 'generations');
const registryPath = path.join(base, 'registry.json');
const oldGeneration = '11111111-1111-4111-8111-111111111111';
const protectedGeneration = '22222222-2222-4222-8222-222222222222';
for (const generation of [oldGeneration, protectedGeneration]) {
const directory = path.join(generations, generation);
await mkdir(directory, { recursive: true });
await writeFile(path.join(directory, 'segment.m4s'), Buffer.alloc(60));
await writeCacheMetadata(generations, directory, {
key: generation,
bytes: 60,
lastAccessedAt: '2020-01-01T00:00:00.000Z',
active: false,
building: false,
pinned: false,
});
await updateRegistry(registryPath, generation, {
mediaItemId: generation,
generationId: generation,
state: 'READY',
playable: true,
manifestPath: `/media/generations/${generation}/master.m3u8`,
});
}
const fontStore = path.join(cacheRoot, 'assets', 'fonts', 'sha256');
const orphanFont = path.join(fontStore, `${'a'.repeat(64)}.ttf`);
await mkdir(fontStore, { recursive: true });
await writeFile(orphanFont, Buffer.alloc(10));
const result = await sweepCache({
cacheRoot,
registryPath,
capacityBytes: 100,
highWatermark: 0.85,
lowWatermark: 0.75,
playbackTtlMs: 0,
protectedKeys: new Set([protectedGeneration]),
fontOrphanMinimumAgeMs: 0,
now: new Date('2999-01-01T00:00:00.000Z'),
});
expect(result.evicted).toEqual([oldGeneration]);
expect(result.registryEntriesRemoved).toEqual([oldGeneration]);
expect(result.orphanFontsRemoved).toEqual([path.basename(orphanFont)]);
expect((await readRegistry(registryPath)).packages).toEqual([
expect.objectContaining({ generationId: protectedGeneration }),
]);
await expect(stat(path.join(generations, oldGeneration))).rejects.toMatchObject({ code: 'ENOENT' });
await expect(stat(path.join(generations, protectedGeneration))).resolves.toBeDefined();
});
});
+23
View File
@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest';
import { deterministicUuid, parseMediaIdentity, shouldInspectSubtitleContent, slugify } from '../src/catalog.js';
describe('catalog filename parsing', () => {
it('groups the supplied season folder and episode filename', () => {
expect(parseMediaIdentity('Mushoku Tensei S3/Mushoku Tensei S3 - 01.[SS][720p][MixFlixTop].mkv'))
.toEqual({ kind: 'SERIES', title: 'Mushoku Tensei', seasonNumber: 3, episodeNumber: 1, episodeTitle: 'Episode 1' });
});
it('creates stable UUIDs and Unicode-safe slugs', () => {
expect(deterministicUuid('same')).toBe(deterministicUuid('same'));
expect(deterministicUuid('same')).toMatch(/^[0-9a-f-]{36}$/u);
expect(slugify('فیلم نمونه')).toBe('فیلم-نمونه');
});
it('samples only unknown textual subtitle languages during archive scans', () => {
expect(shouldInspectSubtitleContent({ index: 2, codec_type: 'subtitle', codec_name: 'ass', tags: { language: 'qad' } })).toBe(true);
expect(shouldInspectSubtitleContent({ index: 3, codec_type: 'subtitle', codec_name: 'ass', tags: { language: 'und' } })).toBe(true);
expect(shouldInspectSubtitleContent({ index: 4, codec_type: 'subtitle', codec_name: 'ass', tags: { language: 'per' } })).toBe(false);
expect(shouldInspectSubtitleContent({ index: 5, codec_type: 'subtitle', codec_name: 'subrip', tags: { language: 'eng' } })).toBe(false);
expect(shouldInspectSubtitleContent({ index: 6, codec_type: 'subtitle', codec_name: 'hdmv_pgs_subtitle' })).toBe(false);
});
});
+100
View File
@@ -0,0 +1,100 @@
import { mkdtemp, readFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { PackageRegistrySchema } from '@easy-stream/contracts';
import { Value } from '@sinclair/typebox/value';
import {
normalizeTrackLabel,
readRegistry,
removeGenerationFromRegistry,
updateRegistry,
} from '../src/registry.js';
describe('local generation registry', () => {
it('atomically records API-loadable per-media status', async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'easy-stream-registry-'));
const registryPath = path.join(directory, 'package-registry.json');
const mediaItemId = '11111111-1111-4111-8111-111111111111';
const generationId = '22222222-2222-4222-8222-222222222222';
await updateRegistry(registryPath, mediaItemId, {
mediaItemId, generationId, state: 'READY', playable: true,
manifestPath: `/media/generations/${generationId}/master.m3u8`,
});
const registry = await readRegistry(registryPath);
expect(registry.packages[0]).toMatchObject({ state: 'READY', playable: true });
expect(Value.Check(PackageRegistrySchema, registry)).toBe(true);
expect(JSON.parse(await readFile(registryPath, 'utf8'))).toMatchObject({ version: 1 });
await updateRegistry(registryPath, mediaItemId, {
mediaItemId, generationId, state: 'PREPARING', playable: false,
});
expect((await readRegistry(registryPath)).packages[0]).toMatchObject({
state: 'READY', playable: true, manifestPath: `/media/generations/${generationId}/master.m3u8`,
});
expect(await removeGenerationFromRegistry(registryPath, generationId)).toBe(true);
expect((await readRegistry(registryPath)).packages).toEqual([]);
});
it('normalizes oversized and control-character container labels for the API contract', async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'easy-stream-registry-label-'));
const registryPath = path.join(directory, 'package-registry.json');
const mediaItemId = '11111111-1111-4111-8111-111111111111';
const generationId = '22222222-2222-4222-8222-222222222222';
const rawMkvTitle = ` Persian\u0000\n\tAudio\u202e ${'x'.repeat(180)} `;
const label = normalizeTrackLabel(rawMkvTitle, 'fa');
expect(label.length).toBeLessThanOrEqual(100);
expect(label).toBe(`Persian Audio ${'x'.repeat(86)}`);
expect(label).not.toMatch(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u);
await updateRegistry(registryPath, mediaItemId, {
mediaItemId,
generationId,
state: 'READY',
playable: true,
manifestPath: `/media/generations/${generationId}/master.m3u8`,
audioTracks: [{
id: '33333333-3333-4333-8333-333333333333',
language: 'fa',
label,
default: true,
}],
});
const registry = await readRegistry(registryPath);
expect(Value.Check(PackageRegistrySchema, registry)).toBe(true);
expect(registry.packages[0]?.audioTracks?.[0]?.label).toBe(label);
});
it('rejects an invalid update before replacing a valid registry file', async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'easy-stream-registry-invalid-'));
const registryPath = path.join(directory, 'package-registry.json');
const mediaItemId = '11111111-1111-4111-8111-111111111111';
const generationId = '22222222-2222-4222-8222-222222222222';
await updateRegistry(registryPath, mediaItemId, {
mediaItemId,
generationId,
state: 'READY',
playable: true,
manifestPath: `/media/generations/${generationId}/master.m3u8`,
});
const before = await readFile(registryPath, 'utf8');
await expect(updateRegistry(registryPath, mediaItemId, {
mediaItemId,
generationId,
state: 'READY',
playable: true,
manifestPath: `/media/generations/${generationId}/master.m3u8`,
audioTracks: [{
id: '33333333-3333-4333-8333-333333333333',
language: 'en',
label: 'x'.repeat(101),
default: true,
}],
})).rejects.toThrow(/Refusing to publish invalid package registry/u);
expect(await readFile(registryPath, 'utf8')).toBe(before);
expect(Value.Check(PackageRegistrySchema, await readRegistry(registryPath))).toBe(true);
});
});
+13
View File
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest';
import { MEDIA_QUEUE_NAME, startMediaQueueWorker } from '../src/work.js';
describe('BullMQ media worker', () => {
it('uses the API queue name and rejects unsafe concurrency before connecting', () => {
expect(MEDIA_QUEUE_NAME).toBe('easy-stream-media');
expect(() => startMediaQueueWorker({
redisUrl: 'redis://127.0.0.1:1',
concurrency: 0,
async dispatch() {},
})).toThrow('concurrency');
});
});