aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/documentation.spec.ts
blob: 16f0c431cb2a830172de7d9cd9b9b1fc8b143628 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import fs from 'fs-extra';
import { glob } from 'glob';
import { getOptions } from '../lib/config/options';
import { regEx } from '../lib/util/regex';

const options = getOptions();
const markdownGlob = '{docs,lib}/**/*.md';

describe('documentation', () => {
  it('has no invalid links', async () => {
    const markdownFiles = await glob(markdownGlob);

    await Promise.all(
      markdownFiles.map(async (markdownFile) => {
        const markdownText = await fs.readFile(markdownFile, 'utf8');
        expect(markdownText).not.toMatch(regEx(/\.md\/#/));
      }),
    );
  });

  describe('website-documentation', () => {
    function getConfigOptionSubHeaders(
      file: string,
      configOption: string,
    ): string[] {
      const subHeadings = [];
      const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
      const reg = regEx(`##\\s${configOption}[\\s\\S]+?\n##\\s`);
      const match = reg.exec(content);
      const subHeadersMatch = match?.[0]?.matchAll(/\n###\s(?<child>\w+)\n/g);
      if (subHeadersMatch) {
        for (const subHeaderStr of subHeadersMatch) {
          if (subHeaderStr?.groups?.child) {
            subHeadings.push(subHeaderStr.groups.child);
          }
        }
      }
      return subHeadings;
    }

    describe('docs/usage/configuration-options.md', () => {
      function getConfigHeaders(file: string): string[] {
        const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
        const matches = content.match(/\n## (.*?)\n/g) ?? [];
        return matches.map((match) => match.substring(4, match.length - 1));
      }

      function getRequiredConfigOptions(): string[] {
        return options
          .filter((option) => !option.globalOnly)
          .filter((option) => !option.parents)
          .filter((option) => !option.autogenerated)
          .map((option) => option.name)
          .sort();
      }

      it('has doc headers sorted alphabetically', () => {
        expect(getConfigHeaders('configuration-options.md')).toEqual(
          getConfigHeaders('configuration-options.md').sort(),
        );
      });

      it('has headers for every required option', () => {
        expect(getConfigHeaders('configuration-options.md')).toEqual(
          getRequiredConfigOptions(),
        );
      });

      function getConfigSubHeaders(file: string): string[] {
        const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
        const matches = content.match(/\n### (.*?)\n/g) ?? [];
        return matches
          .map((match) => match.substring(5, match.length - 1))
          .sort();
      }

      function getRequiredConfigSubOptions(): string[] {
        return options
          .filter((option) => option.stage !== 'global')
          .filter((option) => !option.globalOnly)
          .filter((option) => option.parents)
          .map((option) => option.name)
          .sort();
      }

      function getParentNames(): Set<string> {
        const childrens = options
          .filter((option) => option.stage !== 'global')
          .filter((option) => !option.globalOnly)
          .filter((option) => option.parents);

        const parentNames = new Set<string>();
        for (const children of childrens) {
          const parents = children.parents ?? [];
          for (const parent of parents) {
            parentNames.add(parent);
          }
        }

        return parentNames;
      }

      it('has headers for every required sub-option', () => {
        expect(getConfigSubHeaders('configuration-options.md')).toEqual(
          getRequiredConfigSubOptions(),
        );
      });

      it.each([...getParentNames()])(
        '%s has sub-headers sorted alphabetically',
        (parentName: string) => {
          expect(
            getConfigOptionSubHeaders('configuration-options.md', parentName),
          ).toEqual(
            getConfigOptionSubHeaders(
              'configuration-options.md',
              parentName,
            ).sort(),
          );
        },
      );
    });

    describe('docs/usage/self-hosted-configuration.md', () => {
      function getSelfHostedHeaders(file: string): string[] {
        const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
        const matches = content.match(/\n## (.*?)\n/g) ?? [];
        return matches.map((match) => match.substring(4, match.length - 1));
      }

      function getRequiredSelfHostedOptions(): string[] {
        return options
          .filter((option) => option.globalOnly)
          .map((option) => option.name)
          .sort();
      }

      it('has headers sorted alphabetically', () => {
        expect(getSelfHostedHeaders('self-hosted-configuration.md')).toEqual(
          getSelfHostedHeaders('self-hosted-configuration.md').sort(),
        );
      });

      it('has headers for every required option', () => {
        expect(getSelfHostedHeaders('self-hosted-configuration.md')).toEqual(
          getRequiredSelfHostedOptions(),
        );
      });
    });

    describe('docs/usage/self-hosted-experimental.md', () => {
      function getSelfHostedExperimentalConfigHeaders(file: string): string[] {
        const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
        const matches = content.match(/\n## (.*?)\n/g) ?? [];
        return matches.map((match) => match.substring(4, match.length - 1));
      }

      it('has headers sorted alphabetically', () => {
        expect(
          getSelfHostedExperimentalConfigHeaders('self-hosted-experimental.md'),
        ).toEqual(
          getSelfHostedExperimentalConfigHeaders(
            'self-hosted-experimental.md',
          ).sort(),
        );
      });
    });
  });
});