294 lines
12 KiB
JavaScript
294 lines
12 KiB
JavaScript
import assert from 'node:assert/strict'
|
||
import { execFileSync } from 'node:child_process'
|
||
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
||
import { tmpdir } from 'node:os'
|
||
import { join } from 'node:path'
|
||
import test from 'node:test'
|
||
import {
|
||
DEFAULT_EXCLUDES,
|
||
captureArtifactSnapshot,
|
||
captureArtifactSnapshotSync,
|
||
deliveryPolicyOf,
|
||
summarizeSnapshot,
|
||
verifyArtifactSnapshot,
|
||
verifyArtifactSnapshotSync,
|
||
} from '../src/artifact.js'
|
||
|
||
const GIT_AVAILABLE = (() => {
|
||
try {
|
||
execFileSync('git', ['--version'], { stdio: 'ignore' })
|
||
return true
|
||
} catch {
|
||
return false
|
||
}
|
||
})()
|
||
|
||
function git(cwd, args) {
|
||
return execFileSync('git', args, {
|
||
cwd,
|
||
encoding: 'utf8',
|
||
stdio: ['ignore', 'pipe', 'pipe'],
|
||
})
|
||
}
|
||
|
||
function projectFor(workDir, delivery = {}) {
|
||
return { id: 'prj-artifact-test', workDir, delivery }
|
||
}
|
||
|
||
async function tempDir(label) {
|
||
return mkdtemp(join(tmpdir(), `sansheng-artifact-${label}-`))
|
||
}
|
||
|
||
async function cleanup(dir) {
|
||
await rm(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 })
|
||
}
|
||
|
||
test('DEFAULT_EXCLUDES 覆盖默认目录与敏感文件模式', () => {
|
||
assert.ok(Array.isArray(DEFAULT_EXCLUDES))
|
||
for (const name of [
|
||
'.git', 'node_modules', '.venv', 'venv', '__pycache__', 'dist', 'build',
|
||
'coverage', '.next', '.cache', '.turbo', 'target', 'out',
|
||
]) {
|
||
assert.ok(DEFAULT_EXCLUDES.includes(name), `缺少目录排除 ${name}`)
|
||
}
|
||
for (const pattern of [
|
||
'*.log', '*.tmp', '.env*', '*.pem', '*.key', '*.p12', 'id_rsa*',
|
||
'credentials*', '.DS_Store', 'Thumbs.db',
|
||
]) {
|
||
assert.ok(DEFAULT_EXCLUDES.includes(pattern), `缺少文件排除 ${pattern}`)
|
||
}
|
||
assert.ok(DEFAULT_EXCLUDES.dirs.includes('node_modules'))
|
||
assert.ok(DEFAULT_EXCLUDES.files.includes('.env*'))
|
||
})
|
||
|
||
test('deliveryPolicyOf 合并默认值与项目 delivery', () => {
|
||
const policy = deliveryPolicyOf({
|
||
delivery: {
|
||
include: ['src', './src', 'docs/'],
|
||
exclude: ['secret.txt', '*.bak', 'secret.txt'],
|
||
maxFileBytes: 1024,
|
||
maxFiles: 7,
|
||
},
|
||
})
|
||
assert.deepEqual(policy.include, ['src', 'docs'])
|
||
assert.ok(policy.exclude.includes('secret.txt'))
|
||
assert.ok(policy.exclude.includes('*.bak'))
|
||
assert.ok(policy.exclude.includes('.git'))
|
||
assert.ok(policy.exclude.includes('*.log'))
|
||
assert.equal(policy.maxFileBytes, 1024)
|
||
assert.equal(policy.maxFiles, 7)
|
||
|
||
const defaults = deliveryPolicyOf({})
|
||
assert.deepEqual(defaults.include, [])
|
||
assert.equal(defaults.maxFileBytes, 4 * 1024 * 1024)
|
||
assert.equal(defaults.maxFiles, 5000)
|
||
assert.ok(defaults.exclude.includes('node_modules'))
|
||
})
|
||
|
||
test('非 Git 目录:快照稳定,内容/交付文件变化会改变摘要', async () => {
|
||
const dir = await tempDir('plain')
|
||
try {
|
||
await writeFile(join(dir, 'a.txt'), 'alpha', 'utf8')
|
||
await writeFile(join(dir, 'b.txt'), 'beta', 'utf8')
|
||
const project = projectFor(dir)
|
||
const goal = { id: 'goal-plain' }
|
||
|
||
const first = await captureArtifactSnapshot({ project, goal, stageIndex: 1, now: 1000 })
|
||
const again = await captureArtifactSnapshot({ project, goal, stageIndex: 1, now: 2000 })
|
||
assert.equal(first.kind, 'files')
|
||
assert.equal(first.exists, true)
|
||
assert.equal(first.fileCount, 2)
|
||
assert.equal(first.totalBytes, 9)
|
||
assert.deepEqual(first.files.map(file => file.path), ['a.txt', 'b.txt'])
|
||
assert.equal(first.digest, again.digest)
|
||
assert.notEqual(first.id, again.id)
|
||
|
||
// 默认排除的目录与敏感文件不影响快照。
|
||
await mkdir(join(dir, 'node_modules', 'pkg'), { recursive: true })
|
||
await writeFile(join(dir, 'node_modules', 'pkg', 'index.js'), 'console.log(1)', 'utf8')
|
||
await writeFile(join(dir, '.env'), 'SECRET=1', 'utf8')
|
||
await writeFile(join(dir, '.env.local'), 'SECRET=2', 'utf8')
|
||
const ignored = await captureArtifactSnapshot({ project, goal, stageIndex: 1 })
|
||
assert.equal(ignored.digest, first.digest)
|
||
assert.equal(ignored.fileCount, 2)
|
||
assert.ok(!ignored.files.some(file => file.path === '.env'))
|
||
assert.ok(!ignored.files.some(file => file.path.startsWith('node_modules/')))
|
||
|
||
// 修改已交付文件 → 摘要变化。
|
||
await writeFile(join(dir, 'a.txt'), 'alpha changed', 'utf8')
|
||
const modified = await captureArtifactSnapshot({ project, goal, stageIndex: 1 })
|
||
assert.notEqual(modified.digest, first.digest)
|
||
const before = first.files.find(file => file.path === 'a.txt')
|
||
const after = modified.files.find(file => file.path === 'a.txt')
|
||
assert.notEqual(after.sha256, before.sha256)
|
||
|
||
// 新增交付文件 → 摘要变化。
|
||
await writeFile(join(dir, 'c.txt'), 'gamma', 'utf8')
|
||
const added = await captureArtifactSnapshot({ project, goal, stageIndex: 1 })
|
||
assert.notEqual(added.digest, modified.digest)
|
||
assert.ok(added.files.some(file => file.path === 'c.txt'))
|
||
|
||
// sync 与 async 对同一目录必须给出同一摘要。
|
||
const sync = captureArtifactSnapshotSync({ project, goal, stageIndex: 1 })
|
||
assert.equal(sync.digest, added.digest)
|
||
assert.deepEqual(sync.files, added.files)
|
||
assert.equal(sync.kind, added.kind)
|
||
|
||
const verify = await verifyArtifactSnapshot({ snapshot: added, project })
|
||
assert.equal(verify.ok, true)
|
||
assert.equal(verifyArtifactSnapshotSync({ snapshot: added, project }).ok, true)
|
||
|
||
await writeFile(join(dir, 'c.txt'), 'gamma2', 'utf8')
|
||
const stale = await verifyArtifactSnapshot({ snapshot: added, project })
|
||
assert.equal(stale.ok, false)
|
||
assert.match(stale.reason, /成果已变化/)
|
||
assert.equal(verifyArtifactSnapshotSync({ snapshot: added, project }).ok, false)
|
||
} finally {
|
||
await cleanup(dir)
|
||
}
|
||
})
|
||
|
||
test('include 前缀限制交付范围,maxFiles 超限标记 truncated', async () => {
|
||
const dir = await tempDir('include')
|
||
try {
|
||
await mkdir(join(dir, 'src', 'nested'), { recursive: true })
|
||
await mkdir(join(dir, 'docs'), { recursive: true })
|
||
await writeFile(join(dir, 'src', 'a.js'), 'a', 'utf8')
|
||
await writeFile(join(dir, 'src', 'nested', 'b.js'), 'b', 'utf8')
|
||
await writeFile(join(dir, 'docs', 'readme.md'), 'doc', 'utf8')
|
||
await writeFile(join(dir, 'root.txt'), 'root', 'utf8')
|
||
|
||
const project = projectFor(dir, { include: ['src'] })
|
||
const snapshot = await captureArtifactSnapshot({ project, goal: { id: 'goal-include' }, stageIndex: 1 })
|
||
assert.deepEqual(snapshot.files.map(file => file.path), ['src/a.js', 'src/nested/b.js'])
|
||
assert.ok(!snapshot.files.some(file => file.path.startsWith('docs/')))
|
||
assert.ok(!snapshot.files.some(file => file.path === 'root.txt'))
|
||
|
||
const limited = await captureArtifactSnapshot({
|
||
project,
|
||
goal: { id: 'goal-include' },
|
||
stageIndex: 1,
|
||
policy: { ...deliveryPolicyOf(project), maxFiles: 1 },
|
||
})
|
||
assert.equal(limited.fileCount, 1)
|
||
assert.equal(limited.truncated, true)
|
||
assert.equal(limited.digest.length, 64)
|
||
} finally {
|
||
await cleanup(dir)
|
||
}
|
||
})
|
||
|
||
test('Git 目录:记录 baseCommit,tracked/untracked 变化会改变摘要', { skip: GIT_AVAILABLE ? false : 'git 不可用' }, async () => {
|
||
const dir = await tempDir('git')
|
||
try {
|
||
git(dir, ['init'])
|
||
git(dir, ['config', 'user.name', 'Artifact Test'])
|
||
git(dir, ['config', 'user.email', 'artifact@example.com'])
|
||
await writeFile(join(dir, 'tracked.txt'), 'one\n', 'utf8')
|
||
git(dir, ['add', '.'])
|
||
git(dir, ['commit', '-m', 'initial'])
|
||
|
||
const project = projectFor(dir)
|
||
const goal = { id: 'goal-git' }
|
||
const head = git(dir, ['rev-parse', 'HEAD']).trim()
|
||
|
||
const first = await captureArtifactSnapshot({ project, goal, stageIndex: 1 })
|
||
const second = await captureArtifactSnapshot({ project, goal, stageIndex: 1 })
|
||
assert.equal(first.kind, 'git')
|
||
assert.equal(first.exists, true)
|
||
assert.equal(first.baseCommit, head)
|
||
assert.equal(first.fileCount, 0)
|
||
assert.equal(first.digest, second.digest)
|
||
|
||
const sync = captureArtifactSnapshotSync({ project, goal, stageIndex: 1 })
|
||
assert.equal(sync.digest, first.digest)
|
||
|
||
// 修改 tracked 文件 → diffDigest 与总摘要变化。
|
||
await writeFile(join(dir, 'tracked.txt'), 'two\n', 'utf8')
|
||
const modified = await captureArtifactSnapshot({ project, goal, stageIndex: 1 })
|
||
assert.notEqual(modified.digest, first.digest)
|
||
assert.equal(modified.baseCommit, first.baseCommit)
|
||
assert.notEqual(modified.diffDigest, first.diffDigest)
|
||
assert.notEqual(modified.statusDigest, first.statusDigest)
|
||
|
||
// 新增未跟踪交付文件 → files 记录内容摘要,总摘要变化。
|
||
await writeFile(join(dir, 'untracked.txt'), 'new\n', 'utf8')
|
||
const added = await captureArtifactSnapshot({ project, goal, stageIndex: 1 })
|
||
assert.notEqual(added.digest, modified.digest)
|
||
assert.ok(added.files.some(file => file.path === 'untracked.txt'))
|
||
const untracked = added.files.find(file => file.path === 'untracked.txt')
|
||
assert.equal(untracked.sha256.length, 64)
|
||
|
||
assert.equal((await verifyArtifactSnapshot({ snapshot: added, project })).ok, true)
|
||
await writeFile(join(dir, 'untracked.txt'), 'changed\n', 'utf8')
|
||
const stale = await verifyArtifactSnapshot({ snapshot: added, project })
|
||
assert.equal(stale.ok, false)
|
||
assert.match(stale.reason, /成果已变化/)
|
||
} finally {
|
||
await cleanup(dir)
|
||
}
|
||
})
|
||
|
||
test('workDir 为空/不存在:unscoped 与 missing 同类通过、跨类失败', async () => {
|
||
const dir = await tempDir('unscoped')
|
||
try {
|
||
const projectId = 'prj-state-test'
|
||
const unscopedProject = { id: projectId, workDir: '' }
|
||
const missingProject = { id: projectId, workDir: join(dir, 'not-here') }
|
||
const goal = { id: 'goal-state' }
|
||
|
||
const unscoped = await captureArtifactSnapshot({ project: unscopedProject, goal, stageIndex: 1 })
|
||
const unscopedAgain = await captureArtifactSnapshot({ project: unscopedProject, goal, stageIndex: 1 })
|
||
assert.equal(unscoped.kind, 'unscoped')
|
||
assert.equal(unscoped.exists, false)
|
||
assert.equal(unscoped.fileCount, 0)
|
||
assert.equal(unscoped.digest, unscopedAgain.digest)
|
||
assert.equal(captureArtifactSnapshotSync({ project: unscopedProject, goal, stageIndex: 1 }).digest, unscoped.digest)
|
||
|
||
const missing = await captureArtifactSnapshot({ project: missingProject, goal, stageIndex: 1 })
|
||
assert.equal(missing.kind, 'missing')
|
||
assert.equal(missing.exists, false)
|
||
assert.equal(missing.fileCount, 0)
|
||
assert.equal(captureArtifactSnapshotSync({ project: missingProject, goal, stageIndex: 1 }).digest, missing.digest)
|
||
|
||
assert.equal((await verifyArtifactSnapshot({ snapshot: unscoped, project: unscopedProject })).ok, true)
|
||
assert.equal((await verifyArtifactSnapshot({ snapshot: missing, project: missingProject })).ok, true)
|
||
assert.equal(verifyArtifactSnapshotSync({ snapshot: missing, project: missingProject }).ok, true)
|
||
|
||
const missingAgainstUnscoped = await verifyArtifactSnapshot({ snapshot: missing, project: unscopedProject })
|
||
assert.equal(missingAgainstUnscoped.ok, false)
|
||
assert.match(missingAgainstUnscoped.reason, /成果来源已变化/)
|
||
const unscopedAgainstMissing = await verifyArtifactSnapshot({ snapshot: unscoped, project: missingProject })
|
||
assert.equal(unscopedAgainstMissing.ok, false)
|
||
assert.match(unscopedAgainstMissing.reason, /成果来源已变化/)
|
||
} finally {
|
||
await cleanup(dir)
|
||
}
|
||
})
|
||
|
||
test('summarizeSnapshot 返回固定字段集', async () => {
|
||
const dir = await tempDir('summary')
|
||
try {
|
||
await writeFile(join(dir, 'a.txt'), 'alpha', 'utf8')
|
||
const snapshot = await captureArtifactSnapshot({ project: projectFor(dir), goal: { id: 'goal-summary' }, stageIndex: 1 })
|
||
const summary = summarizeSnapshot(snapshot)
|
||
assert.deepEqual(Object.keys(summary).sort(), [
|
||
'baseCommit', 'diffDigest', 'digest', 'exists', 'fileCount', 'kind', 'totalBytes', 'truncated',
|
||
])
|
||
assert.deepEqual(summary, {
|
||
digest: snapshot.digest,
|
||
kind: snapshot.kind,
|
||
fileCount: snapshot.fileCount,
|
||
totalBytes: snapshot.totalBytes,
|
||
truncated: snapshot.truncated,
|
||
baseCommit: snapshot.baseCommit,
|
||
diffDigest: snapshot.diffDigest,
|
||
exists: snapshot.exists,
|
||
})
|
||
assert.equal(summary.fileCount, 1)
|
||
assert.equal(summary.kind, 'files')
|
||
} finally {
|
||
await cleanup(dir)
|
||
}
|
||
})
|