Skip to content

Commit f57de29

Browse files
author
Token Goat Test
committed
test: stop the stored-chunk-cap test writing the worker-shared config
The test set `max_chunks_per_file = 6` by writing `configPath()`, which `tests/setup/isolate-home.ts` pins once per Vitest worker rather than per file. Every other file that worker runs reads that same config, so a cap of 6 was observable outside the test that set it -- including by the three suites whose `beforeAll` walks and embeds the whole `src` tree. It restored the previous contents in `afterEach`, so nothing had actually been observed failing because of it; a worker killed mid-test would have left the cap behind. The hazard is the same one recorded at the head of `tests/cache_session_commands.test.ts`, where mutating one unrelated field of the shared config broke a test further down that file. Redirected to a per-file temp path with the established `vi.mock('../src/constants.js')` plus `tempConfigPath()` pattern. Proven by pinning `TOKEN_GOAT_HOME` at an empty directory and running the file: no `config.toml` appears there now. The save/restore is gone with it, since `tempConfigPath` roots are removed when the process exits and no other file can observe the path.
1 parent a01d5b0 commit f57de29

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

‎tests/embed_stored_chunk_cap_token_recut.test.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import * as fs from 'node:fs'
3030
import * as os from 'node:os'
3131
import * as path from 'node:path'
3232

33-
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
33+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3434

3535
import { closeAllDbs, getDb } from '../src/db.js'
3636
import { buildEmbeddingBoundaries, indexFileEmbeddings, indexFileSync, maxChunksEmbedSha } from '../src/parser.js'
@@ -39,8 +39,17 @@ import { extractEmbeddableDocumentText, isEmbeddableDocument } from '../src/doc_
3939
import { modelFilesPresent } from '../src/embed_model.js'
4040
import { fingerprintFile } from '../src/fingerprint.js'
4141
import { getFileEntry } from '../src/index_reader.js'
42+
// Redirects configPath() to a per-test-FILE temp file. tests/setup/isolate-home.ts pins TOKEN_GOAT_HOME once per Vitest worker, so the real config.toml is shared by every test file that worker runs, and this file rewrites `max_chunks_per_file` in beforeEach: any file sharing the worker would read a cap of 6 mid-run, including the ones whose beforeAll walks and embeds the whole src tree. The same hazard is documented at the head of tests/cache_session_commands.test.ts, where mutating one unrelated field of the shared config broke a test further down that same file.
43+
vi.mock('../src/constants.js', async (importOriginal) => {
44+
const original = await importOriginal<Record<string, unknown>>()
45+
return { ...original, configPath: () => _testConfigPath }
46+
})
47+
48+
const _testConfigPath = tempConfigPath('tg-embed-stored-chunk-cap-config-test.toml')
49+
4250
import { configPath } from '../src/constants.js'
4351
import { loadConfig } from '../src/config.js'
52+
import { tempConfigPath } from './helpers/temp-config.js'
4453
import { buildDocxFixture } from './helpers/ooxml_fixtures.js'
4554
import Database from '../src/sqlite_driver.js'
4655

@@ -86,24 +95,19 @@ function storedChunkCount(dbPath: string): number {
8695

8796
let TMP: string
8897
let prevEmbeddings: string | undefined
89-
let prevConfigText: string | null
9098

9199
beforeEach(() => {
92100
TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'tg-stored-chunk-cap-'))
93101
prevEmbeddings = process.env['TOKEN_GOAT_EMBEDDINGS_ENABLED']
94102
process.env['TOKEN_GOAT_EMBEDDINGS_ENABLED'] = 'true'
95-
// loadConfig keys its cache on a content hash of this file, so writing it is enough to invalidate; the assertion in each test confirms the value really took effect rather than assuming it. TOKEN_GOAT_HOME is isolated for the run by tests/setup/isolate-home.ts, so this is not a real ~/.token-goat, but it IS shared with other test files in the same run -- hence the restore in afterEach.
103+
// loadConfig keys its cache on a content hash of this file, so writing it is enough to invalidate; the assertion in each test confirms the value really took effect rather than assuming it. The path is this file's own, per the vi.mock at the head, so there is nothing to save and restore: no other test file can observe it, and tempConfigPath's root is removed when the process exits.
96104
const p = configPath()
97-
prevConfigText = fs.existsSync(p) ? fs.readFileSync(p, 'utf8') : null
98105
fs.mkdirSync(path.dirname(p), { recursive: true })
99106
fs.writeFileSync(p, `[indexing]\nmax_chunks_per_file = ${CAP}\n`)
100107
})
101108

102109
afterEach(() => {
103110
closeAllDbs()
104-
const p = configPath()
105-
if (prevConfigText === null) fs.rmSync(p, { force: true })
106-
else fs.writeFileSync(p, prevConfigText)
107111
fs.rmSync(TMP, { recursive: true, force: true })
108112
if (prevEmbeddings === undefined) delete process.env['TOKEN_GOAT_EMBEDDINGS_ENABLED']
109113
else process.env['TOKEN_GOAT_EMBEDDINGS_ENABLED'] = prevEmbeddings

0 commit comments

Comments
 (0)