๐Ÿ“ฆ Kong / insomnia

๐Ÿ“„ unit-test-suite.ts ยท 56 lines
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// @ts-expect-error the enquirer types are incomplete https://github.com/enquirer/enquirer/pull/307
import { AutoComplete } from 'enquirer';

import { logger } from '../../logger';
import type { Database } from '../index';
import { loadApiSpec } from './api-spec';
import type { UnitTestSuite } from './types';
import { ensureSingleOrNone, generateIdIsh, getDbChoice, matchIdIsh } from './util';
import { loadWorkspace } from './workspace';

export const loadUnitTestSuite = (db: Database, identifier: string): UnitTestSuite | null | undefined => {
  // Identifier is for one specific suite; find it
  logger.trace('Load unit test suite with identifier `%s` from data store', identifier);
  const items = db.UnitTestSuite.filter(suite => matchIdIsh(suite, identifier) || suite.name === identifier);
  logger.trace('Found %d unit test suite(s).', items.length);
  return ensureSingleOrNone(items, 'unit test suite');
};
export const loadTestSuites = (db: Database, identifier: string): UnitTestSuite[] => {
  const apiSpec = loadApiSpec(db, identifier);
  const workspace = loadWorkspace(db, apiSpec?.parentId || identifier); // if identifier is for an apiSpec or a workspace, return all suites for that workspace

  if (workspace) {
    return db.UnitTestSuite.filter(s => s.parentId === workspace._id).sort((a, b) => a.metaSortKey - b.metaSortKey);
  } // load particular suite

  const result = loadUnitTestSuite(db, identifier);
  return result ? [result] : [];
};
export const promptTestSuites = async (db: Database, ci: boolean): Promise<UnitTestSuite[]> => {
  if (ci) {
    return [];
  }

  const choices = db.ApiSpec.map(spec => [
    getDbChoice(generateIdIsh(spec), spec.fileName),
    ...db.UnitTestSuite.filter(suite => suite.parentId === spec.parentId).map(suite =>
      getDbChoice(generateIdIsh(suite), suite.name, {
        indent: 1,
      }),
    ),
  ]);

  if (!choices.length) {
    return [];
  }

  const prompt = new AutoComplete({
    name: 'testSuite',
    message: 'Select a document or unit test suite',
    choices: choices.flat(),
  });
  logger.trace('Prompt for document or test suite');
  const [idIsh] = (await prompt.run()).split(' - ').reverse();
  return loadTestSuites(db, idIsh);
};