|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import assert from 'node:assert/strict' |
| 4 | +import { execFileSync } from 'node:child_process' |
| 5 | +import fs from 'node:fs' |
| 6 | +import os from 'node:os' |
| 7 | +import path from 'node:path' |
| 8 | +import test from 'node:test' |
| 9 | +import { fileURLToPath } from 'node:url' |
| 10 | + |
| 11 | +const scriptDir = path.dirname(fileURLToPath(import.meta.url)) |
| 12 | +const repoRoot = path.resolve(scriptDir, '..') |
| 13 | +const sourceSkillDir = path.join(repoRoot, 'skills', 'sealos-deploy') |
| 14 | +const repoUrl = 'https://github.com/usememos/memos' |
| 15 | + |
| 16 | +const templateYaml = `apiVersion: app.sealos.io/v1 |
| 17 | +kind: Template |
| 18 | +metadata: |
| 19 | + name: memos |
| 20 | +spec: |
| 21 | + title: Memos |
| 22 | + url: https://github.com/usememos/memos |
| 23 | + defaults: |
| 24 | + app_name: |
| 25 | + type: string |
| 26 | + value: memos |
| 27 | + inputs: {} |
| 28 | + appConfig: |
| 29 | + data: |
| 30 | + url: https://github.com/usememos/memos |
| 31 | +` |
| 32 | + |
| 33 | +test('/sealos-deploy materializes a configured GitHub template fast path', () => { |
| 34 | + const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'sealos-template-fast-path-')) |
| 35 | + const skillDir = path.join(tmpRoot, 'sealos-deploy') |
| 36 | + const workDir = path.join(tmpRoot, 'work') |
| 37 | + |
| 38 | + try { |
| 39 | + fs.cpSync(sourceSkillDir, skillDir, { recursive: true }) |
| 40 | + fs.mkdirSync(workDir, { recursive: true }) |
| 41 | + |
| 42 | + const configPath = path.join(skillDir, 'config.json') |
| 43 | + const config = JSON.parse(fs.readFileSync(configPath, 'utf8')) |
| 44 | + config.template_fast_path = { |
| 45 | + enabled: true, |
| 46 | + templates: [ |
| 47 | + { |
| 48 | + name: 'memos', |
| 49 | + title: 'Memos', |
| 50 | + source_repos: [repoUrl], |
| 51 | + template_yaml: templateYaml, |
| 52 | + args: { |
| 53 | + default_app_name: 'memos', |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + } |
| 58 | + fs.writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`) |
| 59 | + |
| 60 | + execFileSync(process.execPath, [ |
| 61 | + path.join(skillDir, 'scripts', 'detect-template.mjs'), |
| 62 | + '--github-url', |
| 63 | + repoUrl, |
| 64 | + '--work-dir', |
| 65 | + workDir, |
| 66 | + '--skill-dir', |
| 67 | + skillDir, |
| 68 | + ], { encoding: 'utf8' }) |
| 69 | + |
| 70 | + const matchPath = path.join(workDir, '.sealos', 'template-match.json') |
| 71 | + const templatePath = path.join(workDir, '.sealos', 'template', 'index.yaml') |
| 72 | + |
| 73 | + execFileSync(process.execPath, [ |
| 74 | + path.join(skillDir, 'scripts', 'validate-artifacts.mjs'), |
| 75 | + 'template-match', |
| 76 | + matchPath, |
| 77 | + ], { encoding: 'utf8' }) |
| 78 | + |
| 79 | + const match = JSON.parse(fs.readFileSync(matchPath, 'utf8')) |
| 80 | + assert.equal(match.matched, true) |
| 81 | + assert.equal(match.materialized, true) |
| 82 | + assert.equal(match.repo.reference, 'usememos/memos') |
| 83 | + assert.equal(match.repo.github_url, repoUrl) |
| 84 | + assert.equal(match.template.name, 'memos') |
| 85 | + assert.equal(match.source, 'config') |
| 86 | + assert.equal(match.template_path, '.sealos/template/index.yaml') |
| 87 | + assert.deepEqual(match.args, { default_app_name: 'memos' }) |
| 88 | + |
| 89 | + const materializedTemplate = fs.readFileSync(templatePath, 'utf8') |
| 90 | + assert.match(materializedTemplate, /^apiVersion:\s*app\.sealos\.io\/v1$/m) |
| 91 | + assert.match(materializedTemplate, /^kind:\s*Template$/m) |
| 92 | + |
| 93 | + assert.equal(fs.existsSync(path.join(workDir, '.sealos', 'analysis.json')), false) |
| 94 | + assert.equal(fs.existsSync(path.join(workDir, '.sealos', 'build')), false) |
| 95 | + |
| 96 | + const pipeline = fs.readFileSync(path.join(sourceSkillDir, 'modules', 'pipeline.md'), 'utf8') |
| 97 | + assert.match( |
| 98 | + pipeline, |
| 99 | + /`matched=true` and `materialized=true` → skip Phase 1 through Phase 5/, |
| 100 | + ) |
| 101 | + } finally { |
| 102 | + fs.rmSync(tmpRoot, { recursive: true, force: true }) |
| 103 | + } |
| 104 | +}) |
0 commit comments