Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename recording verification to verification #183

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/codegen/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ describe('Code generation', () => {
it('should generate checks', () => {
const rules: TestRule[] = [
{
type: 'recording-verification',
type: 'verification',
id: '1',
filter: { path: '' },
value: {
type: 'recordedValue',
},
},
]
const correlationStateMap: CorrelationStateMap = {}
Expand All @@ -221,7 +225,7 @@ describe('Code generation', () => {
params = { headers: {}, cookies: {} }
url = http.url\`http://test.k6.io/api/v1/foo\`
resp = http.request('POST', url, null, params)
check(resp,{'RecordingVerificationRule:statusmatchesrecording':(r)=>r.status===200,})
check(resp,{'statusmatches200':(r)=>r.status===200,})

`

Expand Down
6 changes: 2 additions & 4 deletions src/rules/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CorrelationStateMap, TestRule } from '@/types/rules'
import { exhaustive } from '../utils/typescript'
import { applyCustomCodeRule } from './customCode'
import { applyCorrelationRule } from './correlation'
import { applyRecordingVerificationRule } from './verification'
import { applyVerificationRule } from './verification'

export function applyRule(
requestSnippetSchema: RequestSnippetSchema,
Expand All @@ -24,9 +24,7 @@ export function applyRule(
)
case 'parameterization':
case 'verification':
return requestSnippetSchema
case 'recording-verification':
return applyRecordingVerificationRule(requestSnippetSchema)
return applyVerificationRule(requestSnippetSchema)
default:
return exhaustive(rule)
}
Expand Down
3 changes: 0 additions & 3 deletions src/rules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ export function matchFilter(
const { filter } = rule
return new RegExp(filter.path).test(request.url)
}
case 'recording-verification':
// NOTE: no filtering yet on recording verification
return true
default:
return exhaustive(rule)
}
Expand Down
4 changes: 2 additions & 2 deletions src/rules/verification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestSnippetSchema } from '@/types'

export function applyRecordingVerificationRule(
export function applyVerificationRule(
requestSnippetSchema: RequestSnippetSchema
): RequestSnippetSchema {
const response = requestSnippetSchema.data.response
Expand All @@ -11,7 +11,7 @@ export function applyRecordingVerificationRule(

const verificationSnippet = `
check(resp, {
'Recording Verification Rule: status matches recording': (r) => r.status === ${response.statusCode},
'status matches ${response.statusCode}': (r) => r.status === ${response.statusCode},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per suggested we are going to have it differentiated by status code

})
`
return {
Expand Down
7 changes: 1 addition & 6 deletions src/schemas/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const CorrelationRuleSchema = RuleBaseSchema.extend({
export const VerificationRuleSchema = RuleBaseSchema.extend({
type: z.literal('verification'),
filter: FilterSchema,
selector: VerificationRuleSelectorSchema,
selector: VerificationRuleSelectorSchema.optional(),
value: z.discriminatedUnion('type', [
VariableValueSchema,
ArrayValueSchema,
Expand All @@ -120,10 +120,6 @@ export const VerificationRuleSchema = RuleBaseSchema.extend({
]),
})

export const RecordingVerificationRuleSchema = RuleBaseSchema.extend({
type: z.literal('recording-verification'),
})

export const CustomCodeRuleSchema = RuleBaseSchema.extend({
type: z.literal('customCode'),
filter: FilterSchema,
Expand All @@ -136,5 +132,4 @@ export const TestRuleSchema = z.discriminatedUnion('type', [
CorrelationRuleSchema,
VerificationRuleSchema,
CustomCodeRuleSchema,
RecordingVerificationRuleSchema,
])
4 changes: 4 additions & 0 deletions src/store/generator/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ function selectLoadProfile({
return exhaustive(executor)
}
}

export function selectHasVerificationRule(state: GeneratorStore) {
return state.rules.some((rule) => rule.type === 'verification')
}
4 changes: 0 additions & 4 deletions src/types/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
JsonSelectorSchema,
ParameterizationRuleSchema,
RecordedValueSchema,
RecordingVerificationRuleSchema,
RegexSelectorSchema,
RuleBaseSchema,
SelectorSchema,
Expand Down Expand Up @@ -55,7 +54,4 @@ export type ParameterizationRule = z.infer<typeof ParameterizationRuleSchema>
export type CorrelationRule = z.infer<typeof CorrelationRuleSchema>
export type VerificationRule = z.infer<typeof VerificationRuleSchema>
export type CustomCodeRule = z.infer<typeof CustomCodeRuleSchema>
export type RecordingVerificationRule = z.infer<
typeof RecordingVerificationRuleSchema
>
export type TestRule = z.infer<typeof TestRuleSchema>
2 changes: 1 addition & 1 deletion src/utils/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function createNewGeneratorFile(recordingPath = ''): GeneratorFileData {
testData: {
variables: [],
},
rules: [createEmptyRule('recording-verification')],
rules: [createEmptyRule('verification')],
allowlist: [],
includeStaticAssets: false,
}
Expand Down
5 changes: 0 additions & 5 deletions src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,5 @@ export function createEmptyRule(type: TestRule['type']): TestRule {
type: 'recordedValue',
},
}
case 'recording-verification':
return {
type: 'recording-verification',
id: self.crypto.randomUUID(),
}
}
}
19 changes: 16 additions & 3 deletions src/views/Generator/NewRuleMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { PlusCircledIcon } from '@radix-ui/react-icons'
import { Button, DropdownMenu } from '@radix-ui/themes'

import { useGeneratorStore } from '@/store/generator'
import { selectHasVerificationRule, useGeneratorStore } from '@/store/generator'
import { TestRule } from '@/types/rules'
import { createEmptyRule } from '@/utils/rules'

export function NewRuleMenu() {
const addRule = useGeneratorStore((store) => store.addRule)
const hasVerificationRule = useGeneratorStore(selectHasVerificationRule)

const createRule = (type: TestRule['type']) => {
if (hasVerificationRule && type === 'verification') {
return
}

const newRule = createEmptyRule(type)
addRule(newRule)
}
Expand All @@ -27,14 +32,22 @@ export function NewRuleMenu() {
createRule('correlation')
}}
>
Correlation rule
Correlation
</DropdownMenu.Item>
<DropdownMenu.Item
onClick={() => {
createRule('customCode')
}}
>
Custom code rule
Custom code
</DropdownMenu.Item>
<DropdownMenu.Item
disabled={hasVerificationRule}
onClick={() => {
createRule('verification')
}}
>
Verification (limited)
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
Expand Down
13 changes: 11 additions & 2 deletions src/views/Generator/RuleEditor/RuleEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export function RuleEditorSwitch() {
case 'customCode':
return <CustomCodeEditor />
case 'parameterization':
case 'verification':
case 'recording-verification':
return (
<Callout.Root>
<Callout.Icon>
Expand All @@ -31,6 +29,17 @@ export function RuleEditorSwitch() {
<Callout.Text>Not implemented yet</Callout.Text>
</Callout.Root>
)
case 'verification':
return (
<Callout.Root>
<Callout.Icon>
<InfoCircledIcon />
</Callout.Icon>
<Callout.Text>
Verification rule configuration is coming soon
</Callout.Text>
</Callout.Root>
)
default:
return exhaustive(ruleType)
}
Expand Down
1 change: 0 additions & 1 deletion src/views/Generator/RulePreview/RulePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function RulePreview() {
case 'customCode':
case 'parameterization':
case 'verification':
case 'recording-verification':
return null

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CorrelationRule, CustomCodeRule, TestRule } from '@/types/rules'
import {
CorrelationRule,
CustomCodeRule,
TestRule,
VerificationRule,
} from '@/types/rules'
import { TestRuleFilter } from './TestRuleFilter'
import { Badge, Tooltip } from '@radix-ui/themes'
import { exhaustive } from '@/utils/typescript'
Expand All @@ -7,6 +12,7 @@ import {
BorderLeftIcon,
BorderRightIcon,
EyeOpenIcon,
DiscIcon,
} from '@radix-ui/react-icons'

interface TestRuleInlineContentProps {
Expand All @@ -20,15 +26,29 @@ export function TestRuleInlineContent({ rule }: TestRuleInlineContentProps) {
case 'customCode':
return <CustomCodeContent rule={rule} />
case 'parameterization':
case 'verification':
return null
case 'recording-verification':
return null
case 'verification':
return <VerificationContent rule={rule} />
default:
return exhaustive(rule)
}
}

function VerificationContent({ rule }: { rule: VerificationRule }) {
return (
<>
<TestRuleFilter filter={rule.filter} />{' '}
<Tooltip
content={`Checks will be added for status to be the same as the recording.`}
>
<Badge color="gray">
<DiscIcon /> Recording
</Badge>
</Tooltip>
</>
)
}

function CorrelationContent({ rule }: { rule: CorrelationRule }) {
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ function getLabel(rule: TestRule) {
return 'Parameterization'
case 'verification':
return 'Verification'
case 'recording-verification':
return 'Recording Verification'
default:
return exhaustive(rule)
}
Expand Down
Loading