Files
bowenliang123 87a2893190 refactor(comments): clean up the test-suite comments to the guideline
Apply the AGENTS.md comments guideline to every .ts under tests/: remove
what the code already expresses, keep only what it cannot.

Removed: spec-header coverage inventories the describe/test tree already
names; comments restating the next line's assertion or its message; helper
JSDoc whose body already says it; within-file duplicates; five stale
claims (trendChart's "six aggregated turns" against agg.length === 8,
agentGraph's "six categories" against CATS' seven, fold-timing's "cheapest
call LAST" against a loop that feeds it first, fold-surface's unreachable
plain-JSON note, contextView's "two turns" for a one-turn fixture).

Kept and condensed: the projection-freeze class, strict wire/state-schema
hazards, retention and pricing windows, pairing rules, hostile-input
rationale, fixture and expectation-array ground truth, jsdom mechanics,
and issue-numbered regressions.

Comment lines 2604 -> 2014 (-22%); 85 module headers kept, 30 coverage
inventories dropped; every surviving comment line is within the 140-column
limit, with 36 wrapped lines joined onto fuller ones.

No logic, string, identifier, import or directive changed: a comment-strip
gate proves each file's comment-free code is byte-identical to HEAD, and a
line-level proof pairs all 845 changed lines with an unchanged code part.
pnpm run lint && pnpm run test (106 files / 2061 tests, per-file 100%
coverage) && pnpm run build all pass.
2026-10-10 00:50:55 +08:00

67 lines
2.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The official DeepSeek vision image-token calculator port (src/shared/imageTokens.ts). Reference values verified against the docs
// calculator itself (see the module header).
import assert from 'node:assert/strict'
import { describe, test } from 'vitest'
import { estimateImageTokens } from '../../src/shared/imageTokens'
describe('estimateImageTokens', () => {
// Verified against https://api-docs.deepseek.com/zh-cn/quick_start/token_usage.
test.each([
[2048, 1365, 313],
[800, 600, 341],
[2048, 2048, 349],
[512, 512, 201],
[100, 100, 117],
[1920, 1080, 369],
[400, 900, 249],
])('%i×%i → %i tokens (docs calculator reference)', (w, h, expected) => {
assert.equal(estimateImageTokens(w, h), expected)
})
test('every valid size lands inside the documented cap band', () => {
// The documented band is 117–384; aspect ratios at the 8:1 clamp price
// slightly under it (128×16 → 113) — same patch grid as the docs
// calculator, so pin the observed floor rather than the marketing one.
for (let w = 16; w <= 4096; w *= 2) {
for (let h = 16; h <= 4096; h *= 2) {
const tokens = estimateImageTokens(w, h)
assert.ok(tokens !== null && tokens >= 113 && tokens <= 384, `${w}×${h} → ${tokens}`)
}
}
})
test('extreme aspect ratios clamp to the 8:1 band before patching', () => {
assert.ok(estimateImageTokens(8000, 1000) !== null)
assert.ok(estimateImageTokens(1000, 8000) !== null)
})
test('a tall strip beyond the ~190:1 grid threshold solves on the collapsed-width arm', () => {
// The padded grid overflows the budget (860 tokens) and ratio 214 >
// 189.5, so the solve collapses width to one patch unit (gridW < 1 arm).
assert.equal(estimateImageTokens(50, 12000), 381)
})
test('a first-solve overflow walks the budget down until the grid fits', () => {
// Parity terms can push the solved grid a token or two over budget; the
// loop decrements until it fits (found by fuzz: 360×3243 exercises it).
assert.equal(estimateImageTokens(360, 3243), 281)
})
test('non-positive or non-finite dimensions return null', () => {
assert.equal(estimateImageTokens(0, 100), null)
assert.equal(estimateImageTokens(100, 0), null)
assert.equal(estimateImageTokens(-5, 100), null)
assert.equal(estimateImageTokens(100, -5), null)
assert.equal(estimateImageTokens(NaN, 100), null)
assert.equal(estimateImageTokens(100, Infinity), null)
})
test('astronomical (finite) dimensions fail to converge and return null instead of throwing', () => {
// w×h overflows to Infinity: the fixed-point iteration never stabilizes,
// so the 9-iteration cap narrows to null (untrusted-log defense).
assert.equal(estimateImageTokens(Number.MAX_VALUE, Number.MAX_VALUE), null)
assert.equal(estimateImageTokens(1e308, 1e308), null)
})
})