Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

26.4 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { AsyncTaskModel } from '@/database/models/asyncTask';
import { FileService } from '@/server/services/file';
import { AsyncTaskStatus } from '@/types/asyncTask';

// ---- hoisted mocks (available inside vi.mock factories) ----

const {
  mockCreateVideo,
  mockFillVideoPricingDefaults,
  mockFindPreviousGeneration,
  mockFindUserById,
  mockGenerationTopicFindById,
  mockGetVideoAvgLatencies,
  mockIsLobeHubModelAvailable,
  mockProcessBackgroundVideoPolling,
  mockResolveBusinessModelMapping,
  mockAfter,
  mockAppEnv,
  mockServerDB,
  mockTransaction,
} = vi.hoisted(() => {
  const mockTransaction = vi.fn();
  const mockServerDB = {
    query: {
      generationBatches: {
        findFirst: vi.fn(),
      },
      generations: {
        findMany: vi.fn(),
      },
    },
    transaction: mockTransaction,
  };
  const mockCreateVideo = vi.fn();
  const mockAfter = vi.fn(function (cb: () => void) {
    return cb();
  });
  const mockFillVideoPricingDefaults = vi.fn(async ({ params }: { params: unknown }) => params);
  const mockAppEnv = {
    APP_URL: 'https://app.example.com',
    VIDEO_GENERATION_PREFER_WEBHOOK: false,
    WEBHOOK_PROXY_URL: undefined as string | undefined,
  };
  const mockFindPreviousGeneration = vi.fn();
  const mockFindUserById = vi.fn();
  const mockGenerationTopicFindById = vi.fn();
  const mockGetVideoAvgLatencies = vi.fn();
  const mockIsLobeHubModelAvailable = vi.fn();
  const mockProcessBackgroundVideoPolling = vi.fn().mockResolvedValue(undefined);
  const mockResolveBusinessModelMapping = vi.fn();
  return {
    mockCreateVideo,
    mockFillVideoPricingDefaults,
    mockFindPreviousGeneration,
    mockFindUserById,
    mockGenerationTopicFindById,
    mockGetVideoAvgLatencies,
    mockIsLobeHubModelAvailable,
    mockProcessBackgroundVideoPolling,
    mockResolveBusinessModelMapping,
    mockAfter,
    mockAppEnv,
    mockServerDB,
    mockTransaction,
  };
});

// ---- module-level mocks ----

vi.mock('@/database/models/asyncTask');
vi.mock('@/database/models/generation', () => ({
  GenerationModel: vi.fn(function () {
    return { findById: mockFindPreviousGeneration };
  }),
}));
vi.mock('@/database/models/generationTopic', () => ({
  GenerationTopicModel: vi.fn(function () {
    return {
      findById: mockGenerationTopicFindById,
    };
  }),
}));
vi.mock('@/server/services/file');
vi.mock('@/database/models/user', () => ({
  UserModel: {
    findById: mockFindUserById,
  },
}));

vi.mock('@/database/core/db-adaptor', () => ({
  getServerDB: vi.fn().mockResolvedValue(mockServerDB),
}));
vi.mock('@/database/server', () => ({
  getServerDB: vi.fn().mockResolvedValue(mockServerDB),
}));
vi.mock('@/server/modules/ModelRuntime', () => ({
  initModelRuntimeFromDB: vi.fn().mockResolvedValue({ createVideo: mockCreateVideo }),
}));
vi.mock('@/business/server/video-generation/chargeBeforeGenerate', () => ({
  chargeBeforeGenerate: vi.fn().mockResolvedValue({ errorBatch: null, prechargeResult: null }),
}));
vi.mock('@/business/server/video-generation/chargeAfterGenerate', () => ({
  chargeAfterGenerate: vi.fn().mockResolvedValue(undefined),
}));
vi.mock('@lobechat/business-model-runtime', async (importOriginal) => ({
  ...((await importOriginal()) as any),
  resolveBusinessModelMapping: (...args: [string, string]) =>
    mockResolveBusinessModelMapping(...args),
}));
vi.mock('@lobechat/business-model-bank/model-config', () => ({
  isLobeHubModelAvailable: (
    ...args: [
      string,
      string,
      { getUserEmail?: () => Promise<string | null | undefined>; userEmail?: string | null }?,
    ]
  ) => mockIsLobeHubModelAvailable(...args),
}));
vi.mock('@/server/services/generation/videoPricingDefaults', () => ({
  fillVideoPricingDefaults: mockFillVideoPricingDefaults,
}));
vi.mock('@/server/utils/scheduleAfterResponse', () => ({
  after: (cb: () => void) => mockAfter(cb),
}));
vi.mock('@/server/services/generation/videoBackgroundPolling', () => ({
  processBackgroundVideoPolling: mockProcessBackgroundVideoPolling,
}));
vi.mock('@/server/services/generation/latency', () => ({
  getVideoAvgLatencies: mockGetVideoAvgLatencies,
  getVideoLatencyKey: ({ model, provider }: { model: string; provider: string }) =>
    `${provider}\0${model}`,
}));
vi.mock('@/envs/app', () => ({
  appEnv: mockAppEnv,
}));
vi.mock('debug', () => ({
  default: vi.fn(function () {
    return vi.fn();
  }),
}));

// ---- helpers ----

const defaultInput = {
  generationTopicId: 'topic-1',
  model: 'test-model',
  params: { prompt: 'a cat dancing' },
  provider: 'volcengine',
};

const txResult = {
  asyncTaskCreatedAt: new Date('2026-01-01'),
  asyncTaskId: 'async-1',
  batch: { id: 'batch-1' },
  generation: { id: 'gen-1' },
};

// Minimal drizzle-like chain mocks
function createInsertChain() {
  return vi.fn().mockReturnValue({
    values: vi.fn().mockReturnValue({
      returning: vi
        .fn()
        .mockResolvedValueOnce([txResult.batch])
        .mockResolvedValueOnce([txResult.generation])
        .mockResolvedValueOnce([
          { id: txResult.asyncTaskId, createdAt: txResult.asyncTaskCreatedAt },
        ]),
    }),
  });
}

const mockDbUpdate = vi.fn().mockReturnValue({
  set: vi.fn().mockReturnValue({
    where: vi.fn().mockResolvedValue(undefined),
  }),
});

function setupMocks() {
  const mockUpdate = vi.fn().mockResolvedValue(undefined);
  const mockGetFullFileUrl = vi.fn().mockResolvedValue(null);
  const mockGetKeyFromFullUrl = vi.fn().mockResolvedValue(null);

  vi.mocked(AsyncTaskModel).mockImplementation(function () {
    return { update: mockUpdate } as any;
  });
  vi.mocked(FileService).mockImplementation(function () {
    return {
      getFullFileUrl: mockGetFullFileUrl,
      getKeyFromFullUrl: mockGetKeyFromFullUrl,
    } as any;
  });

  const mockInsert = createInsertChain();
  mockTransaction.mockImplementation(async (cb: any) =>
    cb({ insert: mockInsert, update: mockDbUpdate }),
  );

  return { mockGetFullFileUrl, mockGetKeyFromFullUrl, mockInsert, mockUpdate };
}

// ---- import router AFTER mocks are set up ----

const { videoRouter } = await import('../video');

const compatibleVideoEditInput = (): Parameters<
  ReturnType<typeof videoRouter.createCaller>['createVideo']
>[0] => ({
  ...defaultInput,
  model: 'gemini-omni-1.1-flash',
  previousGenerationId: 'source-generation',
  provider: 'google',
});

// ---- tests ----

describe('videoRouter', () => {
  const mockCtx = { userId: 'test-user' };

  beforeEach(() => {
    vi.clearAllMocks();
    mockResolveBusinessModelMapping.mockImplementation(
      async (_provider: string, model: string) => ({
        resolvedModelId: model,
      }),
    );
    mockFindUserById.mockResolvedValue({ email: '[email protected]' });
    mockFindPreviousGeneration.mockResolvedValue(undefined);
    mockGenerationTopicFindById.mockResolvedValue({ id: 'topic-1' });
    mockGetVideoAvgLatencies.mockResolvedValue(new Map());
    mockIsLobeHubModelAvailable.mockResolvedValue(true);
    mockServerDB.query.generationBatches.findFirst.mockResolvedValue(undefined);
    mockAppEnv.VIDEO_GENERATION_PREFER_WEBHOOK = false;
    mockAppEnv.WEBHOOK_PROXY_URL = undefined;
  });

  describe('createVideo - async strategy routing', () => {
    it('should use the webhook path when selected by the runtime', async () => {
      const { mockUpdate } = setupMocks();
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'inf-1',
      });

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo(defaultInput);

      expect(result.success).toBe(true);
      expect(mockCreateVideo).toHaveBeenCalledWith(
        expect.objectContaining({
          callbackUrl: expect.stringMatching(/^https:\/\/app\.example\.com\/api\/webhooks\/video/),
        }),
        expect.objectContaining({ preferredCompletionMode: 'polling' }),
      );
      expect(mockUpdate).toHaveBeenCalledWith('async-1', {
        inferenceId: 'inf-1',
        metadata: {
          completionMode: 'webhook',
          webhookToken: expect.any(String),
        },
        status: AsyncTaskStatus.Processing,
      });
      // Webhook: should NOT trigger background polling
      expect(mockAfter).not.toHaveBeenCalled();
    });

    it('should pass the webhook preference and typed proxy callback URL to the runtime', async () => {
      setupMocks();
      mockAppEnv.VIDEO_GENERATION_PREFER_WEBHOOK = true;
      mockAppEnv.WEBHOOK_PROXY_URL = 'https://local-tunnel.example.com';
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'inf-proxy',
      });

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo(defaultInput);

      expect(mockCreateVideo).toHaveBeenCalledWith(
        expect.objectContaining({
          callbackUrl: expect.stringMatching(
            /^https:\/\/local-tunnel\.example\.com\/api\/webhooks\/video/,
          ),
        }),
        expect.objectContaining({ preferredCompletionMode: 'webhook' }),
      );
    });

    it('should keep a path prefix from the configured callback base URL', async () => {
      setupMocks();
      mockAppEnv.WEBHOOK_PROXY_URL = 'https://proxy.example.com/lobehub/';

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo(defaultInput);

      expect(mockCreateVideo).toHaveBeenCalledWith(
        expect.objectContaining({
          callbackUrl: expect.stringMatching(
            /^https:\/\/proxy\.example\.com\/lobehub\/api\/webhooks\/video\/[^?]+\?/,
          ),
        }),
        expect.any(Object),
      );
    });

    it('should preserve route metadata without polling for a webhook-based interaction', async () => {
      const { mockUpdate } = setupMocks();
      mockCreateVideo.mockImplementation(async (_payload, options) => {
        options.metadata.routeAttempt = {
          apiType: 'google',
          channelId: 'google-channel-2',
          routerId: 'google-router',
        };

        return {
          completionMode: 'webhook',
          inferenceId: 'interactions/omni-1',
        };
      });

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo(defaultInput);

      expect(mockAfter).not.toHaveBeenCalled();
      expect(mockProcessBackgroundVideoPolling).not.toHaveBeenCalled();
      expect(mockUpdate).toHaveBeenCalledWith(
        'async-1',
        expect.objectContaining({
          metadata: expect.objectContaining({
            route: {
              apiType: 'google',
              channelId: 'google-channel-2',
              routerId: 'google-router',
            },
          }),
        }),
      );
    });

    it('should preserve reference image URLs that are not storage files', async () => {
      const { mockGetKeyFromFullUrl, mockInsert } = setupMocks();
      const imageUrls = [
        'https://images.example.com/reference.png',
        'data:image/png;base64,reference',
      ];
      mockGetKeyFromFullUrl.mockResolvedValue(null);
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'inf-reference',
      });

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo({
        ...defaultInput,
        params: { ...defaultInput.params, imageUrls },
      });

      const values = mockInsert.mock.results[0].value.values;
      expect(values).toHaveBeenNthCalledWith(
        1,
        expect.objectContaining({
          config: expect.objectContaining({ imageUrls }),
        }),
      );
    });

    it('should pass the previous interaction when editing a compatible video', async () => {
      const { mockInsert } = setupMocks();
      mockFindPreviousGeneration.mockResolvedValue({
        asset: { interactionId: 'interactions/source-1', type: 'video' },
        generationBatchId: 'source-batch',
        id: 'source-generation',
      });
      mockServerDB.query.generationBatches.findFirst.mockResolvedValue({
        generationTopicId: 'topic-1',
        id: 'source-batch',
        model: 'gemini-omni-1.1-flash',
        provider: 'google',
      });
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'interactions/edit-1',
      });

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo(compatibleVideoEditInput());

      expect(mockCreateVideo).toHaveBeenCalledWith(
        expect.objectContaining({
          params: expect.objectContaining({ task: 'edit' }),
          previousInteractionId: 'interactions/source-1',
        }),
        expect.any(Object),
      );

      const values = mockInsert.mock.results[0].value.values;
      expect(values.mock.calls[0][0].config).not.toHaveProperty('previousGenerationId');
      expect(values).toHaveBeenNthCalledWith(
        3,
        expect.objectContaining({
          metadata: expect.objectContaining({ previousGenerationId: 'source-generation' }),
        }),
      );
    });

    it('should pin the edit to the route that created the source video', async () => {
      const { mockUpdate } = setupMocks();
      const sourceRoute = { apiType: 'google', channelId: 'google-channel-2', routerId: 'r-1' };
      const mockFindTask = vi.fn().mockResolvedValue({ metadata: { route: sourceRoute } });
      vi.mocked(AsyncTaskModel).mockImplementation(function () {
        return { findById: mockFindTask, update: mockUpdate } as any;
      });
      mockFindPreviousGeneration.mockResolvedValue({
        asset: { interactionId: 'interactions/source-1', type: 'video' },
        asyncTaskId: 'source-task',
        generationBatchId: 'source-batch',
        id: 'source-generation',
      });
      mockServerDB.query.generationBatches.findFirst.mockResolvedValue({
        generationTopicId: 'topic-1',
        id: 'source-batch',
        model: 'gemini-omni-1.1-flash',
        provider: 'google',
      });
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'interactions/edit-1',
      });

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo(compatibleVideoEditInput());

      expect(mockFindTask).toHaveBeenCalledWith('source-task');
      expect(mockCreateVideo).toHaveBeenCalledWith(
        expect.objectContaining({ previousInteractionId: 'interactions/source-1' }),
        expect.objectContaining({ route: sourceRoute }),
      );
    });

    it('should reject editing with a model that has no conversational video support', async () => {
      setupMocks();

      const caller = videoRouter.createCaller(mockCtx);

      await expect(
        caller.createVideo({
          ...defaultInput,
          previousGenerationId: 'source-generation',
        }),
      ).rejects.toMatchObject({
        code: 'BAD_REQUEST',
        message: 'The selected model does not support conversational video editing',
      });
      expect(mockFindPreviousGeneration).not.toHaveBeenCalled();
      expect(mockTransaction).not.toHaveBeenCalled();
    });

    it('should reject editing across models', async () => {
      setupMocks();
      mockFindPreviousGeneration.mockResolvedValue({
        asset: { interactionId: 'interactions/source-1', type: 'video' },
        generationBatchId: 'source-batch',
        id: 'source-generation',
      });
      mockServerDB.query.generationBatches.findFirst.mockResolvedValue({
        generationTopicId: 'topic-1',
        id: 'source-batch',
        model: 'different-model',
        provider: 'volcengine',
      });

      const caller = videoRouter.createCaller(mockCtx);

      await expect(caller.createVideo(compatibleVideoEditInput())).rejects.toMatchObject({
        code: 'BAD_REQUEST',
        message: 'Previous video generation cannot be edited with the selected model',
      });
      expect(mockTransaction).not.toHaveBeenCalled();
      expect(mockCreateVideo).not.toHaveBeenCalled();
    });

    it('should validate mapped model id before rejecting deprecated lobehub video models', async () => {
      setupMocks();
      mockResolveBusinessModelMapping.mockResolvedValue({
        requestedModelId: 'onboarding-video',
        resolvedModelId: 'dreamina-seedance-2-0-260128',
      });
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'inf-mapped',
      });

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo({
        ...defaultInput,
        model: 'onboarding-video',
        provider: 'lobehub',
      });

      expect(result.success).toBe(true);
      expect(mockResolveBusinessModelMapping).toHaveBeenCalledWith('lobehub', 'onboarding-video');
      expect(mockIsLobeHubModelAvailable).toHaveBeenCalledWith(
        'dreamina-seedance-2-0-260128',
        'video',
        { getUserEmail: expect.any(Function) },
      );
      const availabilityOptions = mockIsLobeHubModelAvailable.mock.calls.at(-1)?.[2];
      expect(mockFindUserById).not.toHaveBeenCalled();
      await expect(availabilityOptions!.getUserEmail!()).resolves.toBe('[email protected]');
      expect(mockFindUserById).toHaveBeenCalledWith(mockServerDB, mockCtx.userId);
      expect(mockCreateVideo).toHaveBeenCalledWith(
        expect.objectContaining({
          callbackUrl: expect.stringContaining('model=dreamina-seedance-2-0-260128'),
          model: 'dreamina-seedance-2-0-260128',
        }),
        expect.any(Object),
      );
    });

    it('should reject unavailable lobehub video models before creating async tasks', async () => {
      setupMocks();
      mockIsLobeHubModelAvailable.mockResolvedValue(false);

      const caller = videoRouter.createCaller(mockCtx);

      await expect(
        caller.createVideo({
          ...defaultInput,
          model: 'restricted-video-model',
          provider: 'lobehub',
        }),
      ).rejects.toMatchObject({
        code: 'BAD_REQUEST',
        message: 'LobeHubModelDeprecated',
      });

      expect(mockTransaction).not.toHaveBeenCalled();
      expect(mockCreateVideo).not.toHaveBeenCalled();
    });

    it('should reject inaccessible generation topic before charging or creating records', async () => {
      setupMocks();
      mockGenerationTopicFindById.mockResolvedValue(undefined);

      const caller = videoRouter.createCaller({ userId: 'test-user', workspaceId: 'workspace-1' });

      await expect(caller.createVideo(defaultInput)).rejects.toMatchObject({
        code: 'FORBIDDEN',
        message: 'Invalid generation topic',
      });

      expect(mockTransaction).not.toHaveBeenCalled();
      expect(mockCreateVideo).not.toHaveBeenCalled();
    });

    it('should use the polling path when selected by the runtime', async () => {
      const { mockUpdate } = setupMocks();
      mockCreateVideo.mockResolvedValue({
        completionMode: 'polling',
        inferenceId: 'inf-2',
      });

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo(defaultInput);

      expect(result.success).toBe(true);
      expect(mockUpdate).toHaveBeenCalledWith('async-1', {
        inferenceId: 'inf-2',
        metadata: {
          completionMode: 'polling',
          webhookToken: expect.any(String),
        },
        status: AsyncTaskStatus.Processing,
      });
      // Polling: should trigger background polling after the response.
      expect(mockAfter).toHaveBeenCalled();
      expect(mockProcessBackgroundVideoPolling).toHaveBeenCalled();
    });

    it('should use polling path when response contains videoUrl (no special handling)', async () => {
      const { mockUpdate } = setupMocks();
      mockCreateVideo.mockResolvedValue({
        completionMode: 'polling',
        inferenceId: 'inf-3',
        videoUrl: 'https://cdn.example.com/video.mp4',
      });

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo(defaultInput);

      expect(result.success).toBe(true);
      expect(mockUpdate).toHaveBeenCalledWith('async-1', {
        inferenceId: 'inf-3',
        metadata: {
          completionMode: 'polling',
          webhookToken: expect.any(String),
        },
        status: AsyncTaskStatus.Processing,
      });
      // No special videoUrl branch — falls through to polling
      expect(mockAfter).toHaveBeenCalled();
      expect(mockProcessBackgroundVideoPolling).toHaveBeenCalled();
    });

    it('should not start polling for a webhook task', async () => {
      setupMocks();
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'inf-4',
      });

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo(defaultInput);

      expect(mockAfter).not.toHaveBeenCalled();
      expect(mockProcessBackgroundVideoPolling).not.toHaveBeenCalled();
    });
  });

  describe('createVideo - error handling', () => {
    it('should set error status when createVideo throws', async () => {
      const { mockUpdate } = setupMocks();
      mockCreateVideo.mockRejectedValue(new Error('API timeout'));

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo(defaultInput);

      // Batch was already created, so still returns success structure
      expect(result.success).toBe(true);
      expect(mockUpdate).toHaveBeenCalledWith(
        'async-1',
        expect.objectContaining({ status: AsyncTaskStatus.Error }),
      );
    });
  });

  describe('createVideo - pre-charge', () => {
    it('forwards the request spend origin to pre-charge', async () => {
      setupMocks();
      const { chargeBeforeGenerate } =
        await import('@/business/server/video-generation/chargeBeforeGenerate');
      const spendOrigin = {
        agentShare: {
          agentId: 'agent-share',
          shareId: 'share-1',
          visitorUserId: 'visitor-1',
        },
        trigger: 'agent_share',
      };

      const caller = videoRouter.createCaller({ ...mockCtx, spendOrigin });
      await caller.createVideo(defaultInput);

      expect(chargeBeforeGenerate).toHaveBeenCalledWith(expect.objectContaining({ spendOrigin }));
    });

    it('prices and generates with the model defaults the request omitted', async () => {
      setupMocks();
      const { chargeBeforeGenerate } =
        await import('@/business/server/video-generation/chargeBeforeGenerate');
      mockFillVideoPricingDefaults.mockImplementationOnce(async ({ params }) => ({
        ...(params as object),
        duration: 5,
        resolution: '768P',
      }));

      const caller = videoRouter.createCaller(mockCtx);
      await caller.createVideo(defaultInput);

      expect(mockFillVideoPricingDefaults).toHaveBeenCalledWith(
        expect.objectContaining({ params: defaultInput.params }),
      );
      expect(chargeBeforeGenerate).toHaveBeenCalledWith(
        expect.objectContaining({
          params: expect.objectContaining({ duration: 5, resolution: '768P' }),
          providerParams: expect.objectContaining({ duration: 5, resolution: '768P' }),
        }),
      );
      expect(mockCreateVideo).toHaveBeenCalledWith(
        expect.objectContaining({
          params: expect.objectContaining({ duration: 5, resolution: '768P' }),
        }),
        expect.anything(),
      );
    });

    it('should return error batch when pre-charge fails', async () => {
      setupMocks();
      const { chargeBeforeGenerate } =
        await import('@/business/server/video-generation/chargeBeforeGenerate');
      vi.mocked(chargeBeforeGenerate).mockResolvedValueOnce({
        errorBatch: { error: 'insufficient_balance' } as any,
        prechargeResult: undefined,
      });

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo(defaultInput);

      expect(result).toEqual({ error: 'insufficient_balance' });
      // Should not proceed to createVideo
      expect(mockCreateVideo).not.toHaveBeenCalled();
    });
    it('should link error batch generations to their errored async task', async () => {
      setupMocks();
      const { chargeBeforeGenerate } =
        await import('@/business/server/video-generation/chargeBeforeGenerate');
      // The billing layer links the generation to its errored async task after
      // inserting it, so the row it returns carries no asyncTaskId.
      vi.mocked(chargeBeforeGenerate).mockResolvedValueOnce({
        errorBatch: {
          data: {
            batch: { id: 'batch-err' } as any,
            generations: [{ asyncTaskId: null, id: 'gen-err' } as any],
          },
          success: true,
        },
      });
      mockServerDB.query.generations.findMany.mockResolvedValueOnce([
        { asyncTaskId: 'async-err', id: 'gen-err' },
      ]);

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo(defaultInput);

      expect(result.data?.generations?.[0]).toEqual({ asyncTaskId: 'async-err', id: 'gen-err' });
      expect(mockCreateVideo).not.toHaveBeenCalled();
    });
  });

  describe('createVideo - return value', () => {
    it('should return batch and generation data', async () => {
      setupMocks();
      mockCreateVideo.mockResolvedValue({
        completionMode: 'webhook',
        inferenceId: 'inf-5',
      });

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.createVideo(defaultInput);

      expect(result).toEqual({
        data: {
          batch: txResult.batch,
          generations: [{ ...txResult.generation, asyncTaskId: 'async-1' }],
        },
        success: true,
      });
    });
  });

  describe('getModelLatencies', () => {
    it('returns provider-scoped latency once per model pair from a single batched lookup', async () => {
      mockGetVideoAvgLatencies.mockResolvedValue(
        new Map([
          ['provider-1\0model-1', 76_000],
          ['provider-2\0model-1', null],
        ]),
      );
      const models = [
        { model: 'model-1', provider: 'provider-1' },
        { model: 'model-1', provider: 'provider-1' },
        { model: 'model-1', provider: 'provider-2' },
      ];

      const caller = videoRouter.createCaller(mockCtx);
      const result = await caller.getModelLatencies({ models });

      expect(result).toEqual([
        { avgLatencyMs: 76_000, model: 'model-1', provider: 'provider-1' },
        { avgLatencyMs: null, model: 'model-1', provider: 'provider-2' },
      ]);
      expect(mockGetVideoAvgLatencies).toHaveBeenCalledOnce();
      expect(mockGetVideoAvgLatencies).toHaveBeenCalledWith(models);
    });
  });

  describe('getVideoFreeQuota', () => {
    it('reports no free quota for released clients that still query it', async () => {
      const caller = videoRouter.createCaller(mockCtx);

      await expect(caller.getVideoFreeQuota({ model: 'model-1' })).resolves.toBeNull();
    });
  });
});