Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

4.4 KB
Raw Blame
/**
 * StorageUsageScanJob — the nightly 04:15 cron that drives
 * StorageStatsService.scan(). Registration gating follows the
 * CronRegistrarService idiom pinned in tests/unit/nest/admin-jobs.test.ts;
 * the tick error-containment mirrors VersionCheckJob/DemoResetJob there.
 */
import type { CronRegistrarService } from '../../../../src/nest/scheduling/cron-registrar.service';
import type { StorageJobsService } from '../../../../src/nest/storage/storage-jobs.service';
import type { StorageStatsService } from '../../../../src/nest/storage/storage-stats.service';
import { StorageUsageScanJob } from '../../../../src/nest/storage/storage-usage-scan.job';
import { Logger } from '@nestjs/common';

import { describe, it, expect, vi, beforeEach } from 'vitest';

function registrarStub(enabled = true) {
  return {
    isEnabled: vi.fn(() => enabled),
    register: vi.fn(
      (
        _name: string,
        _expression: string,
        _onTick: () => void | Promise<void>,
        _opts?: { timezone?: 'app' | 'none' },
      ) => enabled,
    ),
    unregister: vi.fn(),
  };
}

function statsStub(): StorageStatsService {
  return { scan: vi.fn().mockResolvedValue(undefined) } as unknown as StorageStatsService;
}

/** No exclusive pass holds the slot: the scan goes at once. */
function jobsStub(idle: Promise<void> = Promise.resolve()): StorageJobsService {
  return { exclusiveIdle: vi.fn(() => idle) } as unknown as StorageJobsService;
}

beforeEach(() => vi.restoreAllMocks());

describe('StorageUsageScanJob', () => {
  it('SCAN-001 registers the nightly 04:15 cron when the registrar gate is open', async () => {
    const registrar = registrarStub();
    const job = new StorageUsageScanJob(registrar as unknown as CronRegistrarService, statsStub(), jobsStub());
    await job.onApplicationBootstrap();
    expect(registrar.register).toHaveBeenCalledWith('storage-usage-scan', '15 4 * * *', expect.any(Function));
  });

  it('SCAN-002 does not register under the test gate (registrar.isEnabled() false)', async () => {
    const registrar = registrarStub(false);
    const job = new StorageUsageScanJob(registrar as unknown as CronRegistrarService, statsStub(), jobsStub());
    await job.onApplicationBootstrap();
    expect(registrar.register).not.toHaveBeenCalled();
  });

  it('SCAN-003 the registered callback drives tick(), which calls stats.scan()', async () => {
    const registrar = registrarStub();
    const stats = statsStub();
    const job = new StorageUsageScanJob(registrar as unknown as CronRegistrarService, stats, jobsStub());
    await job.onApplicationBootstrap();
    const onTick = registrar.register.mock.calls[0]![2] as () => void;
    onTick();
    await vi.waitFor(() => expect(stats.scan).toHaveBeenCalledTimes(1));
  });

  it('SCAN-004 tick() resolves and contains an Error rejection to the usage scan log line', async () => {
    const errorSpy = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
    const stats = { scan: vi.fn().mockRejectedValue(new Error('disk full')) } as unknown as StorageStatsService;
    const job = new StorageUsageScanJob(registrarStub() as unknown as CronRegistrarService, stats, jobsStub());
    await expect(job.tick()).resolves.toBeUndefined();
    expect(errorSpy).toHaveBeenCalledWith('usage scan failed: disk full');
  });

  it('SCAN-005 tick() stringifies a non-Error rejection rather than crashing', async () => {
    const errorSpy = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
    const stats = { scan: vi.fn().mockRejectedValue('backend unreachable') } as unknown as StorageStatsService;
    const job = new StorageUsageScanJob(registrarStub() as unknown as CronRegistrarService, stats, jobsStub());
    await expect(job.tick()).resolves.toBeUndefined();
    expect(errorSpy).toHaveBeenCalledWith('usage scan failed: backend unreachable');
  });

  it('SCAN-006 tick() waits for a reclaim pass that still holds the slot before it scans', async () => {
    let release: () => void = () => undefined;
    const idle = new Promise<void>((resolve) => {
      release = resolve;
    });
    const stats = statsStub();
    const job = new StorageUsageScanJob(registrarStub() as unknown as CronRegistrarService, stats, jobsStub(idle));
    const ticked = job.tick();
    await Promise.resolve();
    expect(stats.scan).not.toHaveBeenCalled();
    release();
    await ticked;
    expect(stats.scan).toHaveBeenCalledTimes(1);
  });
});