wip: game schema

This commit is contained in:
Justin Lin committed 2024-10-10 16:22:20 +08:00
1 parent e66598f1a7
commit 19c44fbe52
7 files changed
+70 -2

No files matched your search

+3 -1
View File
@@ -6,5 +6,7 @@
"scripts": {
"build": "tsc -b"
},
"dependencies": {}
"dependencies": {
"zod": "^3.23.8"
}
}
Whitespace-only changes.
+54
View File
@@ -0,0 +1,54 @@
import { z } from 'zod'
export const requirementsSchema = z.object({
requirements: z.array(z.string()),
})
export type IRequirements = z.infer<typeof requirementsSchema>
export const fileInputSchema = z.object({
path: z.string(),
type: z.enum(['file', 'folder']),
})
export type IFileInput = z.infer<typeof fileInputSchema>
export const fileExistenceInputSchema = fileInputSchema.extend({
exists: z.boolean(),
})
export type IFileExistenceInput = z.infer<typeof fileExistenceInputSchema>
const baseConditionSchema = z.object({
command: z.string().optional(),
output: z.string().optional(),
error: z.string().optional(),
pwd: z.string().optional(),
files: z.array(fileExistenceInputSchema).optional(),
})
export type ICondition = z.infer<typeof baseConditionSchema> & {
or?: ICondition[]
not?: ICondition
}
export const conditionSchema: z.ZodType<ICondition> =
baseConditionSchema.extend({
or: z.lazy(() => z.array(conditionSchema)).optional(),
not: z.lazy(() => conditionSchema).optional(),
})
export const responseSchema = z.object({
type: z.enum(['narrative', 'dialogue']),
content: z.string(),
})
export const stageSchema = z.object({
id: z.string().regex(/^[a-zA-Z0-9_-]+$/),
name: z.string().optional(),
requirements: z.array(z.string()),
condition: conditionSchema,
response: responseSchema,
})
export type IStage = z.infer<typeof stageSchema>
Whitespace-only changes.
View File
Whitespace-only changes.
+12
View File
@@ -4,6 +4,7 @@ import { OutputMatcher } from '../src/condition/OutputMatcher'
import { PwdMatcher } from '../src/condition/PwdMatcher'
import { CommandMatcher } from '../src/condition/CommandMatcher'
import { ErrorMatcher } from '../src/condition/ErrorMatcher.js'
import { conditionSchema } from '../src/schema'
test('command match', () => {
const matcher = new CommandMatcher('^echo start$')
@@ -80,3 +81,14 @@ test('condition with NOT', () => {
expect(condition.match({ command: 'echo start' })).toBe(false)
expect(condition.match({ command: 'echo hello' })).toBe(true)
})
it('should parse condition', () => {
const condition = {
command: 'echo start',
output: 'start',
not: {
pwd: '/home/user',
},
}
expect(conditionSchema.safeParse(condition).error).toBeUndefined()
})