File size: 2,151 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export type ConfigFor<P> = {
  [K in keyof P]: P[K] extends string
    ? string[]
    : P[K] extends number
      ? number[]
      : P[K] extends boolean
        ? boolean[] | boolean
        : never
}

export interface Scenario {
  name: string
  config: Record<string, (string | number | boolean)[]>
  only: boolean
  fn: (props: Record<string, string | number | boolean>) => Promise<void>
}

export interface ScenarioVariant {
  scenario: Scenario
  props: Record<string, string | number | boolean>
}

export interface CurrentScenario {
  scenario: ScenarioVariant
  iface: FullInterface

  measurements: Map<
    string,
    {
      value: number
      unit: string
    }
  >
}

export type Interface = Partial<FullInterface>

export interface FullInterface {
  filterScenarios(scenarios: Scenario[]): Promise<Scenario[]>
  filterScenarioVariants(
    scenarioVariants: ScenarioVariant[]
  ): Promise<ScenarioVariant[]>

  start(
    scenario: string,
    props: Record<string, string | number | boolean | null>
  ): Promise<void>
  measurement(
    scenario: string,
    props: Record<string, string | number | boolean | null>,
    name: string,
    value: number,
    unit: string,
    relativeTo?: string
  ): Promise<void>
  end(
    scenario: string,
    props: Record<string, string | number | boolean | null>
  ): Promise<void>
  error(
    scenario: string,
    props: Record<string, string | number | boolean | null>,
    error: unknown
  ): Promise<void>

  finish(): Promise<void>
}

export function intoFullInterface(iface: Interface): FullInterface {
  return {
    filterScenarios: iface.filterScenarios ?? (async (scenarios) => scenarios),
    filterScenarioVariants:
      iface.filterScenarioVariants ??
      (async (scenarioVariants) => scenarioVariants),
    start: iface.start ?? (async () => {}),
    measurement: iface.measurement ?? (async () => {}),
    end: iface.end ?? (async () => {}),
    error: iface.error ?? (async () => {}),
    finish: iface.finish ?? (async () => {}),
  }
}

export {
  describe,
  measureTime,
  reportMeasurement,
  PREVIOUS,
} from './describe.js'
export { runScenarios } from './runner.js'