mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: parse all quest
This commit is contained in:
1 parent
ed4d04b658
commit
61d1871e95
9 files changed
+77
-18
No files matched your search
@@ -1,11 +1,12 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { fileConditionSchema } from './file-condition.js'
|
import { fileConditionSchema } from './file-condition.js'
|
||||||
|
import { toArray } from '../utils.js'
|
||||||
|
|
||||||
export const conditionSchema = z.object({
|
export const conditionSchema = z.object({
|
||||||
command: z.array(z.string()).optional(),
|
command: z.preprocess(toArray, z.array(z.string()).optional()),
|
||||||
output: z.array(z.string()).optional(),
|
output: z.preprocess(toArray, z.array(z.string()).optional()),
|
||||||
error: z.array(z.string()).optional(),
|
error: z.preprocess(toArray, z.array(z.string()).optional()),
|
||||||
pwd: z.array(z.string()).optional(),
|
pwd: z.preprocess(toArray, z.array(z.string()).optional()),
|
||||||
files: z.array(fileConditionSchema).optional(),
|
files: z.array(fileConditionSchema).optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import { toArray } from '../utils.js'
|
||||||
|
|
||||||
export const responseSchema = z.object({
|
export const responseSchema = z.object({
|
||||||
type: z.enum(['narrative', 'dialogue']).default('narrative'),
|
type: z.enum(['narrative', 'dialogue']).default('narrative'),
|
||||||
content: z.string(),
|
content: z.preprocess(toArray, z.array(z.string())),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type Response = z.infer<typeof responseSchema>
|
export type Response = z.infer<typeof responseSchema>
|
||||||
@@ -8,7 +8,7 @@ import { requirementSchema } from './requirement.js'
|
|||||||
export const stageSchema = z.object({
|
export const stageSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
task: z.string(),
|
task: z.string(),
|
||||||
exceptions: z.array(stageExceptionSchema).default([]),
|
exceptions: stageExceptionSchema.array().default([]),
|
||||||
requirements: requirementSchema,
|
requirements: requirementSchema,
|
||||||
condition: conditionSchema,
|
condition: conditionSchema,
|
||||||
responses: z.array(responseSchema),
|
responses: z.array(responseSchema),
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
export const toArray = (val: unknown) => (typeof val === 'string' ? [val] : val)
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { describe, it, expect, beforeAll } from '@jest/globals'
|
||||||
|
import fs from 'fs/promises'
|
||||||
|
import path from 'path'
|
||||||
|
import yaml from 'yaml'
|
||||||
|
import { questSchema } from '../src/quest/quest.js'
|
||||||
|
|
||||||
|
describe('Parse all quests', () => {
|
||||||
|
let quests: any[] = []
|
||||||
|
|
||||||
|
test('should parse all quests', async () => {
|
||||||
|
const questDirectory = path.join(process.cwd(), 'quests')
|
||||||
|
|
||||||
|
const loadQuests = async () => {
|
||||||
|
const questNames = await fs.readdir(questDirectory, {
|
||||||
|
withFileTypes: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
return Promise.all(
|
||||||
|
questNames
|
||||||
|
.filter(
|
||||||
|
(dirent) => dirent.isDirectory() && !dirent.name.startsWith('.')
|
||||||
|
)
|
||||||
|
.map(async (dirent) => {
|
||||||
|
const id = dirent.name
|
||||||
|
const questPath = path.join(questDirectory, id)
|
||||||
|
const files = await fs.readdir(questPath)
|
||||||
|
if (!files.includes('game.yml')) {
|
||||||
|
throw new Error(`Missing game.yml in quest ${id}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await fs.readFile(
|
||||||
|
path.join(questPath, 'game.yml'),
|
||||||
|
'utf-8'
|
||||||
|
)
|
||||||
|
const questData = yaml.parse(body, { merge: true })
|
||||||
|
const image = files.includes('Dockerfile') ? id : 'base'
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
image,
|
||||||
|
...questData,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
quests = await loadQuests()
|
||||||
|
quests.forEach((quest) => {
|
||||||
|
console.log(`Quest ID: ${quest.id}`)
|
||||||
|
expect(questSchema.safeParse(quest).error).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -179,9 +179,9 @@ stages:
|
|||||||
你看到了終端機輸出 `You are lucky!` 這個字串。
|
你看到了終端機輸出 `You are lucky!` 這個字串。
|
||||||
恭喜你,你已經完成了這個課程!
|
恭喜你,你已經完成了這個課程!
|
||||||
exceptions:
|
exceptions:
|
||||||
condition:
|
- condition:
|
||||||
command: ^echo help$
|
command: ^echo help$
|
||||||
responses:
|
responses:
|
||||||
- content: |
|
- content: |
|
||||||
你需要先移動到 `commands` 資料夾,再使用相對路徑執行 `lucky` 檔案。
|
你需要先移動到 `commands` 資料夾,再使用相對路徑執行 `lucky` 檔案。
|
||||||
請先輸入 `cd commands` 來移動到 `commands` 資料夾,再使用相對路徑 `./lucky` 來執行 `lucky` 檔案。
|
請先輸入 `cd commands` 來移動到 `commands` 資料夾,再使用相對路徑 `./lucky` 來執行 `lucky` 檔案。
|
||||||
@@ -68,3 +68,6 @@ stages:
|
|||||||
condition:
|
condition:
|
||||||
command:
|
command:
|
||||||
- ^echo finish$
|
- ^echo finish$
|
||||||
|
responses:
|
||||||
|
- content: |
|
||||||
|
恭喜你,你已經完成了這個關卡!
|
||||||
@@ -69,9 +69,10 @@ stages:
|
|||||||
not:
|
not:
|
||||||
pwd:
|
pwd:
|
||||||
- ^/home/commander$
|
- ^/home/commander$
|
||||||
content: |
|
responses:
|
||||||
請留在 `/home/commander` 資料夾中。
|
- content: |
|
||||||
請輸入 `cd ~` 回到 `/home/commander` 資料夾。
|
請留在 `/home/commander` 資料夾中。
|
||||||
|
請輸入 `cd ~` 回到 `/home/commander` 資料夾。
|
||||||
responses:
|
responses:
|
||||||
- content: |
|
- content: |
|
||||||
你現在知道 `folder` 資料夾的內容了。
|
你現在知道 `folder` 資料夾的內容了。
|
||||||
|
|||||||
@@ -74,15 +74,15 @@ class QuestManager {
|
|||||||
|
|
||||||
const image = files.includes('Dockerfile') ? id : 'base'
|
const image = files.includes('Dockerfile') ? id : 'base'
|
||||||
const quest = questSchema.parse({
|
const quest = questSchema.parse({
|
||||||
_id: id,
|
id,
|
||||||
image,
|
image,
|
||||||
...questData,
|
...questData,
|
||||||
})
|
})
|
||||||
quest.exceptions.push(...exceptions)
|
quest.exceptions.push(...exceptions)
|
||||||
this.quests.set(id, quest)
|
this.quests.set(id, quest)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error parsing quest`, { id, error })
|
logger.error(`Error parsing quest ${id}`, { error })
|
||||||
throw new QuestValidationError(`Error parsing quest`, id, error)
|
throw new QuestValidationError(`Error parsing quest ${id}`, id, error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user