import { describe, it, expect } from 'vitest';
import { extractDormancy } from './extract-node-dormancy.mjs';

describe('extractDormancy', () => {
  it('returns false for a live row (dormant=false, no DEFERRED in boundaries)', () => {
    const md = [
      '#### #10 Laravel Boost',
      '',
      '**Атрибуты:**',
      '',
      '| id | name | kind | phase | subcategory | triggers | boundaries | dormant | last-touched |',
      '|---|---|---|---|---|---|---|---|---|',
      '| #10 | Laravel Boost | composer-dep | 1 | — | «SQL, Eloquent» | replaces #1 PG MCP | false | 2026-05-19 |',
    ].join('\n');
    expect(extractDormancy(md)).toEqual({ '#10': false });
  });

  it('returns true when Tooling marks dormant=true', () => {
    const md = '| #17 | pg_partman | binary-dep | 1 | — | «partition mgmt» | none | true | 2026-05-19 |';
    expect(extractDormancy(md)).toEqual({ '#17': true });
  });

  it('returns true when boundaries contains DEFERRED (even if dormant=false)', () => {
    const md = '| #44 | Figma MCP | mcp | off-phase | design-tooling | «figma extract» | DEFERRED — нет Figma-аккаунта | false | 2026-05-19 |';
    expect(extractDormancy(md)).toEqual({ '#44': true });
  });

  it('handles multiple nodes in one pass (mixed signals)', () => {
    const md = [
      '| #44 | Figma MCP | mcp | off-phase | design-tooling | «figma extract» | DEFERRED — нет Figma | false | 2026-05-17 |',
      '| #45 | Universal Icons MCP | mcp | off-phase | design-tooling | «svg search» | non-Lucide | false | 2026-05-17 |',
    ].join('\n');
    expect(extractDormancy(md)).toEqual({ '#44': true, '#45': false });
  });

  it('ignores header/separator rows', () => {
    const md = [
      '| id | name | kind | phase | subcategory | triggers | boundaries | dormant | last-touched |',
      '|---|---|---|---|---|---|---|---|---|',
    ].join('\n');
    expect(extractDormancy(md)).toEqual({});
  });

  it('ignores non-numeric ids (template placeholders)', () => {
    const md = '| #NN | <name> | <kind> | <phase> | <subcat or —> | «<triggers>» | <ADR-NNN or none> | false | 2026-05-19 |';
    expect(extractDormancy(md)).toEqual({});
  });

  it('does NOT match the word DEFERRED inside a longer token (boundary check)', () => {
    const md = '| #99 | fake | mcp | off | tooling | «t» | NODEFERREDX prefix | false | 2026-05-19 |';
    expect(extractDormancy(md)).toEqual({ '#99': false });
  });
});
