aboutsummaryrefslogtreecommitdiff
path: root/website/scripts/docs-preview.integration.test.ts
blob: f850689c900426ba6d02343e2b446830f77e3309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { afterEach, describe, expect, test } from 'bun:test';
import { execFileSync } from 'node:child_process';
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { tmpdir } from 'node:os';

const script = resolve(import.meta.dir, 'docs-preview.mjs');
const temporaryDirectories: string[] = [];

afterEach(async () => {
  await Promise.all(
    temporaryDirectories.splice(0).map((path) => rm(path, { recursive: true, force: true })),
  );
});

describe('preview documentation snapshots', () => {
  test('snapshots and validates the selected commit exactly', async () => {
    const root = await fixture();
    const commit = git(root, ['rev-parse', 'HEAD']).trim();
    await write(root, 'docs/preview/website/stale.mdx', 'stale\n');
    await writeManifest(root, commit);

    runScript(root, ['snapshot', commit]);
    runScript(root, ['check']);

    expect(await read(root, 'docs/preview/website/src/content/docs/index.mdx')).toBe(
      'selected preview\n',
    );
    await expect(read(root, 'docs/preview/website/stale.mdx')).rejects.toThrow();
  });

  test('rejects snapshot drift', async () => {
    const root = await fixture();
    const commit = git(root, ['rev-parse', 'HEAD']).trim();
    await writeManifest(root, commit);
    runScript(root, ['snapshot', commit]);
    await write(root, 'docs/preview/website/src/content/docs/index.mdx', 'changed\n');

    expect(() => runScript(root, ['check'])).toThrow();
  });
});

async function fixture() {
  const root = await mkdtemp(join(tmpdir(), 'herdr-preview-docs-'));
  temporaryDirectories.push(root);
  await write(root, 'docs/next/website/src/content/docs/index.mdx', 'selected preview\n');
  await write(root, 'docs/next/website/src/data/config-reference.json', '{"preview":true}\n');
  git(root, ['init', '-q']);
  git(root, ['config', 'user.email', 'test@example.com']);
  git(root, ['config', 'user.name', 'Test']);
  git(root, ['add', '.']);
  git(root, ['commit', '-qm', 'preview fixture']);
  return root;
}

async function writeManifest(root: string, commit: string) {
  await write(
    root,
    'website/preview.json',
    `${JSON.stringify({
      schema_version: 1,
      channel: 'preview',
      build_id: 'test-build',
      commit,
      builds: { 'test-build': { commit, tag: 'preview-test-build' } },
    })}\n`,
  );
}

async function write(root: string, path: string, content: string) {
  const destination = resolve(root, path);
  await mkdir(resolve(destination, '..'), { recursive: true });
  await writeFile(destination, content, 'utf8');
}

async function read(root: string, path: string) {
  return readFile(resolve(root, path), 'utf8');
}

function git(root: string, args: string[]) {
  return execFileSync('git', args, { cwd: root, encoding: 'utf8', stdio: 'pipe' });
}

function runScript(root: string, args: string[]) {
  execFileSync('node', [script, ...args], {
    cwd: root,
    env: { ...process.env, HERDR_DOCS_REPO_ROOT: root },
    stdio: 'pipe',
  });
}