247 lines
9.3 KiB
TypeScript
247 lines
9.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import zlib from 'node:zlib'
|
|
import {
|
|
MISSILE_METAS,
|
|
HOWL_META,
|
|
SHOUT_META,
|
|
BATTLECRY_META,
|
|
BATTLEORDERS_META,
|
|
BATTLECOMMAND_META,
|
|
WARCRY_META,
|
|
GRIMWARDSMALLSTART_META,
|
|
GRIMWARDSMALL_META,
|
|
GRIMWARDSMALLSTOP_META,
|
|
GRIMWARDMEDIUMSTART_META,
|
|
GRIMWARDMEDIUM_META,
|
|
GRIMWARDMEDIUMSTOP_META,
|
|
GRIMWARDLARGESTART_META,
|
|
GRIMWARDLARGE_META,
|
|
GRIMWARDLARGESTOP_META,
|
|
} from '../../../src/render/missiles-meta.ts'
|
|
import {
|
|
OVERLAY_METAS,
|
|
SHOUTSTART_OVERLAY_META,
|
|
SHOUT_OVERLAY_META,
|
|
BATTLECRY_OVERLAY_META,
|
|
BATTLEORDERSCAST_OVERLAY_META,
|
|
BATTLEORDERS_OVERLAY_META,
|
|
BATTLECOMMANDCAST_OVERLAY_META,
|
|
BATTLECOMMAND_OVERLAY_META,
|
|
BERSERKFRONT_OVERLAY_META,
|
|
FRENZY_OVERLAY_META,
|
|
WARCRY_OVERLAY_META,
|
|
} from '../../../src/render/overlays-meta.ts'
|
|
import { BATCH1_SKILLS } from '../../../src/game/skills.ts'
|
|
|
|
function decodeRgbaPng(buf: Buffer) {
|
|
const width = buf.readUInt32BE(16)
|
|
const height = buf.readUInt32BE(20)
|
|
const bitDepth = buf[24]!
|
|
const colorType = buf[25]!
|
|
|
|
const idatParts: Buffer[] = []
|
|
let offset = 8
|
|
while (offset < buf.length) {
|
|
const len = buf.readUInt32BE(offset)
|
|
const type = buf.subarray(offset + 4, offset + 8).toString('ascii')
|
|
if (type === 'IDAT') {
|
|
idatParts.push(buf.subarray(offset + 8, offset + 8 + len))
|
|
}
|
|
offset += 12 + len
|
|
}
|
|
const idat = Buffer.concat(idatParts)
|
|
const raw = zlib.inflateSync(idat)
|
|
|
|
const bpp = 4
|
|
const rowBytes = width * bpp
|
|
const pixels = new Uint8Array(width * height * 4)
|
|
let srcAt = 0
|
|
let dstAt = 0
|
|
|
|
for (let y = 0; y < height; y++) {
|
|
const filter = raw[srcAt++]
|
|
const row = raw.subarray(srcAt, srcAt + rowBytes)
|
|
srcAt += rowBytes
|
|
|
|
const prevRow = y > 0 ? pixels.subarray((y - 1) * rowBytes, y * rowBytes) : null
|
|
const curRow = pixels.subarray(dstAt, dstAt + rowBytes)
|
|
|
|
for (let x = 0; x < rowBytes; x++) {
|
|
const left = x >= bpp ? curRow[x - bpp]! : 0
|
|
const up = prevRow ? prevRow[x]! : 0
|
|
const upLeft = prevRow && x >= bpp ? prevRow[x - bpp]! : 0
|
|
|
|
let val = row[x]!
|
|
if (filter === 0) {
|
|
// None
|
|
} else if (filter === 1) {
|
|
val = (val + left) & 0xff
|
|
} else if (filter === 2) {
|
|
val = (val + up) & 0xff
|
|
} else if (filter === 3) {
|
|
val = (val + Math.floor((left + up) / 2)) & 0xff
|
|
} else if (filter === 4) {
|
|
const p = left + up - upLeft
|
|
const pa = Math.abs(p - left)
|
|
const pb = Math.abs(p - up)
|
|
const pc = Math.abs(p - upLeft)
|
|
let pr = upLeft
|
|
if (pa <= pb && pa <= pc) pr = left
|
|
else if (pb <= pc) pr = up
|
|
val = (val + pr) & 0xff
|
|
}
|
|
curRow[x] = val
|
|
}
|
|
dstAt += rowBytes
|
|
}
|
|
|
|
return { width, height, bitDepth, colorType, pixels }
|
|
}
|
|
|
|
describe('Adversarial Stress Test: Barbarian Asset Pipeline & Invariants (Milestone M16)', () => {
|
|
const publicMissilesDir = path.resolve(__dirname, '../../../public/missiles')
|
|
const packMissilesDir = path.resolve(__dirname, '../../../samples/d2-packs/missiles')
|
|
const publicOverlaysDir = path.resolve(__dirname, '../../../public/overlays')
|
|
const packOverlaysDir = path.resolve(__dirname, '../../../samples/d2-packs/overlays')
|
|
|
|
const BARBARIAN_MISSILES = [
|
|
{ name: 'howl', celFile: 'BAYellShockWave01', meta: HOWL_META },
|
|
{ name: 'shout', celFile: 'BAYellShockWave01', meta: SHOUT_META },
|
|
{ name: 'battlecry', celFile: 'BAYellShockWave01', meta: BATTLECRY_META },
|
|
{ name: 'battleorders', celFile: 'BAYellShockWave01', meta: BATTLEORDERS_META },
|
|
{ name: 'battlecommand', celFile: 'BAYellShockWave01', meta: BATTLECOMMAND_META },
|
|
{ name: 'warcry', celFile: 'WarcryShockwave', meta: WARCRY_META },
|
|
{ name: 'grimwardsmallstart', celFile: 'GrimWardSmallStart', meta: GRIMWARDSMALLSTART_META },
|
|
{ name: 'grimwardsmall', celFile: 'GrimWardSmall', meta: GRIMWARDSMALL_META },
|
|
{ name: 'grimwardsmallstop', celFile: 'GrimWardSmallStop', meta: GRIMWARDSMALLSTOP_META },
|
|
{ name: 'grimwardmediumstart', celFile: 'GrimWardMediumStart', meta: GRIMWARDMEDIUMSTART_META },
|
|
{ name: 'grimwardmedium', celFile: 'GrimWardMedium', meta: GRIMWARDMEDIUM_META },
|
|
{ name: 'grimwardmediumstop', celFile: 'GrimWardMediumStop', meta: GRIMWARDMEDIUMSTOP_META },
|
|
{ name: 'grimwardlargestart', celFile: 'GrimWardLargeStart', meta: GRIMWARDLARGESTART_META },
|
|
{ name: 'grimwardlarge', celFile: 'GrimWardLarge', meta: GRIMWARDLARGE_META },
|
|
{ name: 'grimwardlargestop', celFile: 'GrimWardLargeStop', meta: GRIMWARDLARGESTOP_META },
|
|
] as const
|
|
|
|
const BARBARIAN_OVERLAYS = [
|
|
{ name: 'shoutstart', celFile: 'ShoutStart', meta: SHOUTSTART_OVERLAY_META },
|
|
{ name: 'shout', celFile: 'Shout', meta: SHOUT_OVERLAY_META },
|
|
{ name: 'battlecry', celFile: 'BattleCry', meta: BATTLECRY_OVERLAY_META },
|
|
{ name: 'battleorderscast', celFile: 'BattleOrdersStart', meta: BATTLEORDERSCAST_OVERLAY_META },
|
|
{ name: 'battleorders', celFile: 'BattleOrders', meta: BATTLEORDERS_OVERLAY_META },
|
|
{ name: 'battlecommandcast', celFile: 'BattleCommandStart', meta: BATTLECOMMANDCAST_OVERLAY_META },
|
|
{ name: 'battlecommand', celFile: 'BattleCommand', meta: BATTLECOMMAND_OVERLAY_META },
|
|
{ name: 'berserkfront', celFile: 'BerserkOverlay', meta: BERSERKFRONT_OVERLAY_META },
|
|
{ name: 'frenzy', celFile: 'Frenzy', meta: FRENZY_OVERLAY_META },
|
|
{ name: 'warcry', celFile: 'WarcryHit', meta: WARCRY_OVERLAY_META },
|
|
] as const
|
|
|
|
it('A1: Stress audits frame coordinates within atlas boundaries for all 15 Barbarian missiles', () => {
|
|
for (const m of BARBARIAN_MISSILES) {
|
|
const meta = m.meta
|
|
expect(meta.width).toBeGreaterThan(0)
|
|
expect(meta.height).toBeGreaterThan(0)
|
|
expect(meta.directions).toBeGreaterThan(0)
|
|
expect(meta.groups.length).toBe(meta.directions)
|
|
|
|
for (let d = 0; d < meta.directions; d++) {
|
|
const frames = meta.groups[d]!
|
|
expect(frames.length).toBe(meta.framesPerDirection)
|
|
for (let f = 0; f < frames.length; f++) {
|
|
const [x, y, w, h, ax, ay] = frames[f]!
|
|
expect(x).toBeGreaterThanOrEqual(0)
|
|
expect(y).toBeGreaterThanOrEqual(0)
|
|
expect(w).toBeGreaterThan(0)
|
|
expect(h).toBeGreaterThan(0)
|
|
expect(x + w).toBeLessThanOrEqual(meta.width)
|
|
expect(y + h).toBeLessThanOrEqual(meta.height)
|
|
expect(Number.isFinite(ax)).toBe(true)
|
|
expect(Number.isFinite(ay)).toBe(true)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
it('A2: Stress audits overlay frame bounding boxes and positive dimensional envelopes', () => {
|
|
for (const o of BARBARIAN_OVERLAYS) {
|
|
const meta = o.meta
|
|
expect(meta.width).toBeGreaterThan(0)
|
|
expect(meta.height).toBeGreaterThan(0)
|
|
expect(meta.frames).toBeGreaterThan(0)
|
|
expect(meta.box).toBeDefined()
|
|
expect(meta.box.width).toBeGreaterThan(0)
|
|
expect(meta.box.height).toBeGreaterThan(0)
|
|
|
|
for (const dirGroup of meta.groups) {
|
|
for (const frame of dirGroup) {
|
|
const [x, y, w, h] = frame
|
|
expect(x).toBeGreaterThanOrEqual(0)
|
|
expect(y).toBeGreaterThanOrEqual(0)
|
|
expect(w).toBeGreaterThan(0)
|
|
expect(h).toBeGreaterThan(0)
|
|
expect(x + w).toBeLessThanOrEqual(meta.width)
|
|
expect(y + h).toBeLessThanOrEqual(meta.height)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
it('A3: Adversarial pixel audit - No black edge artifacts or rogue alpha channels on dark backgrounds for additive warcries & overlays', () => {
|
|
const ADDITIVE_WARCRIES = ['howl', 'shout', 'battlecry', 'battleorders', 'battlecommand', 'warcry']
|
|
for (const name of ADDITIVE_WARCRIES) {
|
|
const filePath = path.join(publicMissilesDir, `${name}.png`)
|
|
const buf = fs.readFileSync(filePath)
|
|
const { pixels } = decodeRgbaPng(buf)
|
|
|
|
let blackOpaqueCount = 0
|
|
for (let i = 0; i < pixels.length; i += 4) {
|
|
const r = pixels[i]!
|
|
const g = pixels[i + 1]!
|
|
const b = pixels[i + 2]!
|
|
const a = pixels[i + 3]!
|
|
|
|
// For additive Warcries, semi-black/fringe (max(r,g,b) <= 4) MUST be transparent (a === 0)
|
|
if (Math.max(r, g, b) <= 4 && a > 0) {
|
|
blackOpaqueCount++
|
|
}
|
|
}
|
|
expect(blackOpaqueCount, `Missile ${name} has dark border fringe`).toBe(0)
|
|
}
|
|
|
|
for (const o of BARBARIAN_OVERLAYS) {
|
|
const filePath = path.join(publicOverlaysDir, `${o.name}.png`)
|
|
const buf = fs.readFileSync(filePath)
|
|
const { pixels } = decodeRgbaPng(buf)
|
|
|
|
let blackOpaqueCount = 0
|
|
for (let i = 0; i < pixels.length; i += 4) {
|
|
const r = pixels[i]!
|
|
const g = pixels[i + 1]!
|
|
const b = pixels[i + 2]!
|
|
const a = pixels[i + 3]!
|
|
|
|
if (Math.max(r, g, b) <= 4 && a > 0) {
|
|
blackOpaqueCount++
|
|
}
|
|
}
|
|
expect(blackOpaqueCount, `Overlay ${o.name} has dark border fringe`).toBe(0)
|
|
}
|
|
})
|
|
|
|
it('A4: Parity Invariant - BATCH1_SKILLS length remains strictly 38', () => {
|
|
expect(Object.keys(BATCH1_SKILLS).length).toBe(38)
|
|
})
|
|
|
|
it('A5: Zero Runtime MPQ Invariant - No runtime MPQ / DLL dependencies needed for Barbarian rendering', () => {
|
|
for (const m of BARBARIAN_MISSILES) {
|
|
expect(MISSILE_METAS[m.name]).toBeDefined()
|
|
expect(MISSILE_METAS[m.name]!.groups.length).toBeGreaterThan(0)
|
|
}
|
|
for (const o of BARBARIAN_OVERLAYS) {
|
|
expect(OVERLAY_METAS[o.name]).toBeDefined()
|
|
expect(OVERLAY_METAS[o.name]!.groups.length).toBeGreaterThan(0)
|
|
}
|
|
})
|
|
})
|