import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');

void test('package.json: конфиг electron-builder (mac/win/linux)', () => {
  const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')) as {
    build: {
      appId: string;
      asar: boolean;
      npmRebuild: boolean;
      asarUnpack: string[];
      extraResources: { from: string; to: string }[];
      mac: { target: unknown; artifactName?: string };
      linux: { target: unknown };
      appImage?: { artifactName?: string };
      nsis?: { artifactName?: string };
      dmg?: { artifactName?: string };
      files: string[];
      toolsets?: { appimage?: string };
    };
  };
  assert.ok(pkg.build);
  assert.equal(pkg.build.appId, 'com.ttrpgplayer.app');
  assert.equal(pkg.build.asar, true, 'релизный артефакт: app.asar без «голого» дерева dist в .app/.exe');
  assert.equal(
    pkg.build.npmRebuild,
    false,
    'N-API prebuilds (classic-level, sharp @img) — без node-gyp / Visual Studio на pack',
  );
  assert.ok(Array.isArray(pkg.build.asarUnpack));
  assert.ok(pkg.build.asarUnpack.some((p) => p.includes('preload')));
  assert.ok(Array.isArray(pkg.build.extraResources));
  assert.ok(
    pkg.build.extraResources.some(
      (e: unknown) =>
        typeof e === 'object' &&
        e !== null &&
        'to' in e &&
        typeof (e as { to: unknown }).to === 'string' &&
        (e as { to: string }).to.includes('branding'),
    ),
  );
  assert.ok(Array.isArray(pkg.build.mac.target));
  assert.equal(pkg.build.mac.artifactName, '${productName}-${arch}.${ext}');
  assert.ok(!pkg.build.mac.artifactName?.includes('version'));
  assert.equal(pkg.build.toolsets?.appimage, '1.0.2');
  assert.ok(Array.isArray(pkg.build.linux.target));
  const linuxTargets = pkg.build.linux.target as { target: string; arch: string[] }[];
  assert.ok(linuxTargets.some((t) => t.target === 'AppImage'));
  assert.ok(linuxTargets.some((t) => t.arch.includes('x64') && t.arch.includes('arm64')));
  assert.equal(pkg.build.appImage?.artifactName, '${productName}-${arch}.${ext}');
  assert.equal(pkg.build.nsis?.artifactName, '${productName}-Setup.${ext}');
  assert.equal(pkg.build.dmg?.artifactName, '${productName}-${arch}.${ext}');
  assert.ok(!pkg.build.appImage?.artifactName?.includes('version'));
  assert.ok(!pkg.build.nsis?.artifactName?.includes('version'));
  assert.ok(pkg.build.files.includes('dist/**/*'));
  assert.ok(
    pkg.build.asarUnpack.some((p) => p.includes('@img')),
    'sharp native binaries live under node_modules/@img',
  );
  assert.ok(
    pkg.build.asarUnpack.some((p) => p.includes('classic-level')),
    'Foundry DB: classic-level stays unpacked with its prebuilds',
  );
});

void test('package.json: pack:mac runs release-mac-prep before electron-builder', () => {
  const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')) as {
    scripts: { 'pack:mac': string };
  };
  assert.match(pkg.scripts['pack:mac'], /release-mac-prep\.mjs/);
});
