repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
callouts-and-code.test.mjsJavaScript79 lines3.8 KiBraw download
import assert from 'node:assert/strict';
import test from 'node:test';
import { Window } from 'happy-dom';

test('callouts are typed from a marker or a leading emoji, and unlabelled code stays unlabelled', async () => {
  const window = new Window({ url: 'https://example.test/sheets/active-directory/certipy' });
  globalThis.window = window;
  globalThis.document = window.document;
  globalThis.localStorage = window.localStorage;
  // app.ts reads `NodeFilter` as a bare global, as a browser provides it.
  globalThis.NodeFilter = window.NodeFilter;

  document.body.innerHTML = `
    <article class="prose">
      <blockquote id="marker"><p>[!warning]+ Check the clock first.</p></blockquote>
      <blockquote id="emoji"><p>⚠️ <strong>Server 2022:</strong> stricter defaults.</p></blockquote>
      <blockquote id="tip"><p>💡 Use the RC4 hash.</p></blockquote>
      <blockquote id="custom"><p>[!opsec] Loud on the wire.</p></blockquote>
      <blockquote id="plain"><p>Hash format you will see.</p></blockquote>
      <pre class="astro-code" data-language="plaintext"><code>1. Gain a foothold</code></pre>
      <pre class="astro-code" data-language="bash"><code>id</code></pre>
    </article>
  `;

  await import('../src/scripts/app.ts');

  const callout = (id) => {
    const el = document.getElementById(id);
    return [el.dataset.callout ?? null, el.querySelector('.callout__label')?.textContent ?? null, el.querySelector('p').textContent];
  };
  assert.deepEqual(callout('marker'), ['warning', 'Warning', 'Check the clock first.']);
  assert.deepEqual(callout('emoji'), ['warning', 'Warning', 'Server 2022: stricter defaults.'], 'the emoji and its selector are dropped');
  assert.deepEqual(callout('tip'), ['tip', 'Tip', 'Use the RC4 hash.']);
  assert.deepEqual(callout('custom'), ['note', 'opsec', 'Loud on the wire.'], 'an unknown type keeps the author\'s word as its label');
  assert.deepEqual(callout('plain'), [null, null, 'Hash format you will see.']);

  const labels = [...document.querySelectorAll('.code-pane__lang')].map((el) => el.textContent);
  assert.deepEqual(labels, ['', 'bash']);
  assert.equal(document.querySelectorAll('.code-pane .code-copy').length, 2, 'the copy control is there either way');
});

test('pressing Copy copies the listing, confirms it on the block, and puts everything back', async () => {
  // Runs in the document the test above set up; app.ts is already bound.
  let clipboard = null;
  Object.defineProperty(globalThis, 'navigator', {
    value: { clipboard: { writeText: async (text) => { clipboard = text; } } },
    configurable: true,
  });
  const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

  const pane = [...document.querySelectorAll('.code-pane')].at(-1);
  const copy = pane.querySelector('.code-copy');
  const word = pane.querySelector('.code-copy__word');
  assert.equal(word.textContent, 'Copy');
  assert.ok(pane.querySelector('.code-pane__sweep'), 'the sweep line is in the pane, waiting');

  copy.click();
  await wait(20);
  assert.equal(clipboard, 'id');
  assert.equal(pane.classList.contains('is-copied'), true);
  assert.equal(copy.classList.contains('copied'), true);
  assert.equal(copy.getAttribute('aria-label'), 'Copied', 'the accessible name never passes through the scramble');
  assert.notEqual(word.textContent, 'Copied', 'the visible word is still settling');
  assert.equal(word.textContent.length, 'Copied'.length);

  await wait(420);
  assert.equal(word.textContent, 'Copied');

  // A second press mid-confirmation restarts the clock rather than cutting it short.
  copy.click();
  await wait(1500);
  assert.equal(pane.classList.contains('is-copied'), true);

  await wait(400);
  assert.equal(word.textContent, 'Copy');
  assert.equal(copy.getAttribute('aria-label'), 'Copy');
  assert.equal(pane.classList.contains('is-copied'), false);
  assert.equal(copy.classList.contains('copied'), false);
});