'use strict';

/**
 * DC-058 grid share-button wiring test.
 *
 * Validates that core/grid.js wires the "Share" button:
 *   1. the source contains a share-btn button emit
 *   2. it is gated on s.id !== 'internet' (same as options/delete)
 *   3. it calls window.openShareModal with the service object
 *   4. it surfaces a fallback error toast if the modal module is missing
 *   5. it does NOT touch the API directly (the modal owns the API calls)
 *
 * This is a static-source test (regex over the file) rather than a VM
 * sandbox because grid.js depends on many other globals (window.APPS,
 * SITE, el(), etc.) that would require a very large fake-DOM harness to
 * bootstrap. The static-source checks are sufficient regression guards
 * for the structural changes this DC-058 ticket introduces.
 *
 * Source path resolution: the standard location is `status/js/core/grid.js`.
 * The judge-artifact.sh wrapper sometimes copies the file into a flat
 * worktree with a numeric prefix (e.g. `1_grid.js`), so we fall back
 * to a directory scan.
 */

const fs = require('fs');
const path = require('path');
const test = require('node:test');
const assert = require('node:assert/strict');

function findTarget(name) {
  const candidates = [
    path.join(__dirname, '..', 'js', 'core', name),
    path.join(__dirname, 'core', name),
    path.join(__dirname, name),
  ];
  for (const p of candidates) {
    try { if (fs.statSync(p).isFile()) return p; } catch (_) { /* keep looking */ }
  }
  const dir = __dirname;
  let entries = [];
  try { entries = fs.readdirSync(dir); } catch (_) { return null; }
  const match = entries.find(e => e === name || e.endsWith('_' + name) || e.endsWith('-' + name));
  return match ? path.join(dir, match) : null;
}

const GRID_PATH = findTarget('grid.js');
if (!GRID_PATH) {
  throw new Error(
    'Cannot find core/grid.js. Searched standard paths + directory scan of ' +
    __dirname + '. If running under judge-artifact.sh, ensure the wrapper ' +
    'passed the file via --files.'
  );
}

const source = fs.readFileSync(GRID_PATH, 'utf8');

test('grid.js emits a share-btn button', () => {
  assert.match(source, /['"]share-btn['"]/,
    'grid.js must declare a share-btn button class');
  assert.match(source, /['"]🔗['"]/,
    'grid.js must use the link glyph for the share button');
});

test('grid.js share button is gated on s.id !== "internet"', () => {
  const shareMatch = source.match(/if \(s\.id !== ['"]internet['"]\) \{[\s\S]*?shareBtn[\s\S]*?\}/);
  assert.ok(shareMatch, 'share-btn block must be wrapped in s.id !== "internet" guard');
});

test('grid.js share button calls window.openShareModal(service)', () => {
  assert.match(source, /window\.openShareModal\(\s*s\s*\)/,
    'share-btn onclick must invoke window.openShareModal(s)');
});

test('grid.js share button has a fallback if the modal module is missing', () => {
  const shareOnclick = source.match(/shareBtn\.onclick[\s\S]*?\}/);
  assert.ok(shareOnclick, 'shareBtn must have an onclick handler');
  assert.match(
    shareOnclick[0],
    /showNotification|openShareModal|console\.(error|warn)/,
    'share-btn onclick must surface a visible error when the modal module is missing'
  );
});

test('grid.js share button does NOT call the API directly', () => {
  const shareOnclick = source.match(/shareBtn\.onclick[\s\S]*?\}/);
  assert.ok(shareOnclick, 'shareBtn must have an onclick handler');
  assert.doesNotMatch(shareOnclick[0], /fetch\s*\(/,
    'share-btn onclick must not call fetch directly — the modal owns API calls');
});
