Skip to content

Commit 885b0dc

Browse files
committed
fix(host): release the pricing file source with the fiber, not at apply time
ctx.effect(() => fileSource.dispose(), ...) calls dispose while the fiber is being built and registers undefined as the disposer: the file source is dead before its first read, so pricing.json is never applied and the change watcher never fires. Return a disposer instead. Test mocks now follow cordis semantics (run setup, keep the returned disposer, release it in after()) and a /pricing route regression test covers the override path - it fails against the old form.
1 parent dacc1f5 commit 885b0dc

4 files changed

Lines changed: 62 additions & 10 deletions

File tree

‎src/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ export function apply(ctx: Context, config: Config = {}): void {
364364
const pricingFile = config.pricingFile?.trim() || defaultPricingFile()
365365
const fileSource = filePricingSource(pricingFile)
366366
const resolver = createPricingResolver(fileSource)
367-
ctx.effect(() => fileSource.dispose(), 'dsh-usage-chart: pricing file watcher')
367+
// 注意返回的是「返回 disposer 的函数」:写成 () => fileSource.dispose() 会在注册时立刻销毁文件源。
368+
ctx.effect(() => () => fileSource.dispose(), 'dsh-usage-chart: pricing file watcher')
368369

369370
ctx.effect(() => ctx.webServer.register({
370371
kind: 'exact',

‎tests/core.test.mjs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'node:assert/strict'
2-
import test from 'node:test'
2+
import test, { after } from 'node:test'
33

44
import {
55
DEFAULT_CNY_PER_USD,
@@ -16,6 +16,10 @@ import {
1616
normalizeCurrency,
1717
} from '../lib/index.js'
1818

19+
/** cordis 语义:effect(setup) 立即执行 setup,把返回的 disposer 留到 fiber 销毁时再调用。 */
20+
const disposers = []
21+
after(() => { for (const dispose of disposers) if (typeof dispose === 'function') dispose() })
22+
1923
function responseRecorder() {
2024
return {
2125
headers: {},
@@ -30,7 +34,7 @@ function responseRecorder() {
3034
function mountedRoutes(events = [], config = {}) {
3135
const routes = new Map()
3236
apply({
33-
effect(setup) { setup() },
37+
effect(setup) { disposers.push(setup()) },
3438
get() { return undefined },
3539
webServer: {
3640
register(route) { routes.set(route.path, route); return () => {} },
@@ -126,7 +130,7 @@ test('mounted Host routes enforce method and browser-origin guards', async () =>
126130
async function balanceResponse(extraCtx = {}, config = {}) {
127131
const routes = new Map()
128132
apply({
129-
effect(setup) { setup() },
133+
effect(setup) { disposers.push(setup()) },
130134
get(name) { return name === 'credentials' ? extraCtx.credentials : undefined },
131135
webServer: { register(route) { routes.set(route.path, route); return () => {} } },
132136
sessions: { get() { return undefined } },

‎tests/pricing.test.mjs‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
22
import { mkdtemp, writeFile, rm } from 'node:fs/promises'
33
import { tmpdir } from 'node:os'
44
import { join } from 'node:path'
5-
import test from 'node:test'
5+
import test, { after } from 'node:test'
66

77
import {
88
BUILTIN_VERIFIED_AT,
@@ -19,6 +19,10 @@ import {
1919
tierAt,
2020
} from '../lib/index.js'
2121

22+
/** cordis 语义:effect(setup) 立即执行 setup,把返回的 disposer 留到 fiber 销毁时再调用。 */
23+
const disposers = []
24+
after(() => { for (const dispose of disposers) if (typeof dispose === 'function') dispose() })
25+
2226
/** 2026-09-10 内置刊例价(双币种 /1M,高峰/空闲)。 */
2327
const FLASH_OFF_PEAK = {
2428
cny: { cacheMissInput: 1, cacheHitInput: 0.02, output: 4 },
@@ -264,7 +268,7 @@ function responseRecorder() {
264268
test('/pricing route exposes builtin + fallback + models snapshot', async () => {
265269
const routes = new Map()
266270
apply({
267-
effect(setup) { setup() },
271+
effect(setup) { disposers.push(setup()) },
268272
get() { return undefined },
269273
webServer: { register(route) { routes.set(route.path, route); return () => {} } },
270274
sessions: { get() { return undefined } },
@@ -286,3 +290,41 @@ test('/pricing route exposes builtin + fallback + models snapshot', async () =>
286290
assert.ok(models.includes('deepseek-v4-pro'))
287291
assert.ok(models.includes('deepseek-v4-flash-vision-exp'))
288292
})
293+
294+
test('/pricing route serves the user pricing.json override (file source survives startup)', async () => {
295+
const dir = await mkdtemp(join(tmpdir(), 'duc-pricing-'))
296+
const pricingFile = join(dir, 'pricing.json')
297+
await writeFile(pricingFile, JSON.stringify({
298+
models: {
299+
'deepseek-flash': {
300+
peak: { cny: { cacheMissInput: 19.99, cacheHitInput: 0.04, output: 8 }, usd: { cacheMissInput: 2.99, cacheHitInput: 0.006, output: 1.2 } },
301+
offPeak: { cny: { cacheMissInput: 9.99, cacheHitInput: 0.02, output: 4 }, usd: { cacheMissInput: 1.49, cacheHitInput: 0.003, output: 0.6 } },
302+
},
303+
},
304+
}))
305+
const routes = new Map()
306+
const disposers = []
307+
try {
308+
apply({
309+
// cordis 语义:effect(setup) 立即执行 setup,把返回的 disposer 留到 fiber 销毁时才调用。
310+
// 写成 () => source.dispose() 会在这里当场销毁文件源 → 覆盖文件永不生效(本测试即为此回归)。
311+
effect(setup) { disposers.push(setup()) },
312+
get() { return undefined },
313+
webServer: { register(route) { routes.set(route.path, route); return () => {} } },
314+
sessions: { get() { return undefined } },
315+
}, { pricingFile })
316+
await new Promise((resolve) => setTimeout(resolve, 50))
317+
318+
const recorder = responseRecorder()
319+
await routes.get('/dsh-usage-chart/pricing').handler(
320+
{ method: 'GET', url: '/dsh-usage-chart/pricing', headers: { host: 'localhost:3000' } },
321+
recorder,
322+
)
323+
const flash = JSON.parse(recorder.body).models.find((m) => m.model === 'deepseek-flash')
324+
assert.equal(flash.source, 'file')
325+
assert.equal(flash.pricing.offPeak.cny.cacheMissInput, 9.99)
326+
} finally {
327+
for (const dispose of disposers) if (typeof dispose === 'function') dispose()
328+
await rm(dir, { recursive: true, force: true })
329+
}
330+
})

‎tests/rounds.test.mjs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import assert from 'node:assert/strict'
2-
import test from 'node:test'
2+
import test, { after } from 'node:test'
33

44
import { apply, foldRounds, foldTurnUsage, tierAt } from '../lib/index.js'
55

6+
/** cordis 语义:effect(setup) 立即执行 setup,把返回的 disposer 留到 fiber 销毁时再调用。 */
7+
const disposers = []
8+
after(() => { for (const dispose of disposers) if (typeof dispose === 'function') dispose() })
9+
10+
611
/** 2026-09-10 内置刊例价(双币种 /1M):flash 高峰/空闲。 */
712
const FLASH_PEAK = {
813
cny: { cacheMissInput: 2, cacheHitInput: 0.04, output: 8 },
@@ -206,7 +211,7 @@ test('/usage route returns rounds with cost and timing', async () => {
206211
]
207212
const routes = new Map()
208213
apply({
209-
effect(setup) { setup() },
214+
effect(setup) { disposers.push(setup()) },
210215
get() { return undefined },
211216
webServer: { register(route) { routes.set(route.path, route); return () => {} } },
212217
sessions: { get(id) { return id === 'session-test' ? { id, events } : undefined } },
@@ -247,7 +252,7 @@ test('/usage route prefers snapshotEvents() when the new API is present', async
247252
]
248253
const routes = new Map()
249254
apply({
250-
effect(setup) { setup() },
255+
effect(setup) { disposers.push(setup()) },
251256
get() { return undefined },
252257
webServer: { register(route) { routes.set(route.path, route); return () => {} } },
253258
// 有 snapshotEvents、无 events —— 必须走 snapshotEvents() 分支,且绝不触碰 events 属性。
@@ -273,7 +278,7 @@ test('/usage route prefers snapshotEvents() when the new API is present', async
273278
const both = { id: 'session-test', events: [], snapshotEvents: () => events }
274279
const bothRoutes = new Map()
275280
apply({
276-
effect(setup) { setup() },
281+
effect(setup) { disposers.push(setup()) },
277282
get() { return undefined },
278283
webServer: { register(route) { bothRoutes.set(route.path, route); return () => {} } },
279284
sessions: { get(id) { return id === 'session-test' ? both : undefined } },

0 commit comments

Comments
 (0)