/**
 * Service generator — null / empty operation robustness.
 *
 * Surfaced 2026-06-03 by the JobHunter service-rehoming (engines 6.97.x):
 * the ORM-precedence rule rehomes suppressed classes (scrapers/coordinators)
 * as services, and some of their analyse-recovered operations parse as a
 * YAML `opName:` with no body → `operation === null`. The generator iterated
 * `Object.entries(service.operations)` and read `operation.parameters`,
 * throwing "Cannot read properties of null (reading 'parameters')" — realize
 * logged "Service: Failed to render template" and dropped the operation.
 *
 * Fix: `generateOperation` defaults a null/empty operation to `{}` (a
 * schema-valid empty operation must render, not crash). Same guard added to
 * the mongo + pg service generators (R16c — fix the axis).
 */
import { describe, it, expect } from 'vitest';
import generatePrismaService from '../service-generator.js';

describe('prisma service-generator — null/empty operation', () => {
  it('renders a service whose operations include a null body without throwing', () => {
    const context: any = {
      spec: { models: {} },
      service: {
        name: 'OrderPageHarvester',
        operations: {
          harvest: { steps: ['Fetch the page', 'Parse rows'], ensures: ['Returns rows'] },
          // analyse-recovered operation with no declared body → YAML null.
          paginate: null,
          // empty-object body (the other shape that reaches the generator).
          reset: {},
        },
      },
    };

    let out = '';
    expect(() => { out = generatePrismaService(context); }).not.toThrow();
    // All three operations emit a method — none is silently dropped.
    expect(out).toContain('async harvest(');
    expect(out).toContain('async paginate(');
    expect(out).toContain('async reset(');
  });
});
