← back to Apartmentwallpaper

afternic-unlock.test.mjs

55 lines

// Unit tests for afternic-unlock.mjs pure logic.
// These test the code we authored — they make NO network calls and perform NO
// registrar writes. Run: node --test
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { DOMAINS, findCreds, buildRequests, parseMode } from './afternic-unlock.mjs';

test('DOMAINS holds exactly the 38 Afternic-listed domains', () => {
  assert.equal(DOMAINS.length, 38);
});

test('DOMAINS are unique and well-formed', () => {
  assert.equal(new Set(DOMAINS).size, DOMAINS.length, 'no duplicates');
  for (const d of DOMAINS) assert.match(d, /^[a-z0-9.-]+\.[a-z]{2,}$/, `${d} looks like a domain`);
});

test('buildRequests emits GET state, PUT unlock, PUT nameservers in order', () => {
  const reqs = buildRequests('example.com');
  assert.equal(reqs.length, 3);
  assert.deepEqual(reqs[0], { method: 'GET', path: '/v1/domains/example.com', body: null });
  assert.deepEqual(reqs[1], { method: 'PUT', path: '/v1/domains/example.com', body: { locked: false } });
  assert.equal(reqs[2].method, 'PUT');
  assert.equal(reqs[2].body.nameServers.length, 2);
});

test('findCreds reads GoDaddy key/secret from process.env first', () => {
  const c = findCreds({ GODADDY_API_KEY: 'k123', GODADDY_API_SECRET: 's456' }, () => null);
  assert.deepEqual(c, { key: 'k123', secret: 's456', src: 'process.env' });
});

test('findCreds digs key/secret out of a domain-suite MCP env block', () => {
  const fakeCfg = { mcpServers: { 'domain-suite': { env: { GODADDY_API_KEY: 'abc', GODADDY_API_SECRET: 'xyz' } } } };
  const c = findCreds({}, () => fakeCfg);
  assert.equal(c.src, '.claude.json');
  assert.equal(c.key, 'abc');
  assert.equal(c.secret, 'xyz');
});

test('findCreds returns null when nothing is configured', () => {
  assert.equal(findCreds({}, () => null), null);
});

test('parseMode defaults to a single-domain dry test', () => {
  const { mode, targets } = parseMode(undefined);
  assert.equal(mode, 'test');
  assert.equal(targets.length, 1);
  assert.equal(targets[0], DOMAINS[0]);
});

test('parseMode "all" targets every domain', () => {
  const { mode, targets } = parseMode('all');
  assert.equal(mode, 'all');
  assert.equal(targets.length, 38);
});