← back to Desktop Dotbar

test/realm.test.js

48 lines

'use strict';
// realm.js — DW / Non-DW folder classification (TK-12234). Zero deps.
const test = require('node:test');
const assert = require('node:assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const realm = require('../realm');

const idx = new Map([
  ['TK-1', 'dw-commerce Fix Kravet MAP rows'],
  ['TK-2', 'kamatera certbot fleet-wide ACME'],
  ['TK-3', 'Designer-Wallcoverings theme fix'],
]);

test('ticket project/title decides first', async () => {
  assert.equal(await realm.realmOf({ ticket: 'TK-1', doing: '', pid: 0 }, { index: idx, cwd: '' }), 'dw');
  assert.equal(await realm.realmOf({ ticket: 'TK-3', doing: '', pid: 0 }, { index: idx, cwd: '' }), 'dw');
  assert.equal(await realm.realmOf({ ticket: 'TK-2', doing: 'renewing certs', pid: 0 }, { index: idx, cwd: '/Users/x' }), 'other');
});

test('falls back to label, then cwd project', async () => {
  assert.equal(await realm.realmOf({ ticket: '', doing: 'shopify metafield backfill', pid: 0 }, { index: idx, cwd: '' }), 'dw');
  assert.equal(await realm.realmOf({ ticket: '', doing: 'working', pid: 0 }, { index: idx, cwd: '/Users/x/Projects/dw-kravet-hires' }), 'dw');
  assert.equal(await realm.realmOf({ ticket: '', doing: 'working', pid: 0 }, { index: idx, cwd: '/Users/x/Projects/homesonspec' }), 'other');
});

test('negative: "dw" inside other words and the home path never read as DW', async () => {
  for (const t of ['crowdwork', 'bandwidth test', 'shadow dom', 'windows'])
    assert.equal(realm.isDwText(t), false, t);
  // a home dir containing letters like "dw" must not flip the session to DW
  assert.equal(await realm.realmOf({ ticket: '', doing: '', pid: 0 }, { index: idx, cwd: '/Users/dwayne' }), 'other');
  // unknown ticket, no signals -> other (fail to Non-DW, never a false DW)
  assert.equal(await realm.realmOf({ ticket: 'TK-999', doing: '', pid: 0 }, { index: idx, cwd: '' }), 'other');
});

test('indexTickets reads the log incrementally and keeps partial lines', () => {
  const f = path.join(os.tmpdir(), `realm-ev-${process.pid}.jsonl`);
  realm._resetIndex();
  fs.writeFileSync(f, JSON.stringify({ type: 'create', id: 'TK-10-a', project: 'dw', title: 'x' }) + '\n{"type":"cre');
  let m = realm.indexTickets(f);
  assert.equal(m.get('TK-10'), 'dw x');
  fs.appendFileSync(f, 'ate","id":"TK-11-b","project":"ops","title":"y"}\n');
  m = realm.indexTickets(f);
  assert.equal(m.get('TK-11'), 'ops y');
  fs.unlinkSync(f); realm._resetIndex();
});