|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | +const fs = require('node:fs'); |
| 4 | +const os = require('node:os'); |
| 5 | +const path = require('node:path'); |
| 6 | +const { createExampleCache, fingerprint } = require('./example-cache.cjs'); |
| 7 | +const { inputDigest } = require('./run.cjs'); |
| 8 | + |
| 9 | +function fixture(t) { |
| 10 | + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'fern-cache-test-')); |
| 11 | + t.after(() => fs.rmSync(directory, { recursive: true, force: true })); |
| 12 | + const errors = []; |
| 13 | + const context = { |
| 14 | + spec: { components: { schemas: { Voice: { type: 'string', enum: ['marin'] } } } }, |
| 15 | + settings: {}, |
| 16 | + errorCollector: { collect(value) { errors.push(value); } }, |
| 17 | + }; |
| 18 | + const args = { context, propertySchema: { $ref: '#/components/schemas/Voice' }, breadcrumbs: ['voice'], propertyId: 'voice' }; |
| 19 | + return { directory, context, args, errors, cache: createExampleCache({ directory, namespace: 'test' }) }; |
| 20 | +} |
| 21 | + |
| 22 | +test('a new process can reuse an exact result without sharing mutable objects', (t) => { |
| 23 | + const { directory, args, cache } = fixture(t); |
| 24 | + const expected = { userSpecifiedExamples: {}, autogeneratedExamples: { voice: { value: 'marin', missing: undefined } } }; |
| 25 | + assert.deepEqual(cache.run(args, () => expected), expected); |
| 26 | + expected.autogeneratedExamples.voice.value = 'changed after conversion'; |
| 27 | + const next = createExampleCache({ directory, namespace: 'test' }); |
| 28 | + const hit = next.run(args, () => { throw new Error('must be a cache hit'); }); |
| 29 | + assert.equal(hit.autogeneratedExamples.voice.value, 'marin'); |
| 30 | + assert.ok(Object.hasOwn(hit.autogeneratedExamples.voice, 'missing')); |
| 31 | + hit.autogeneratedExamples.voice.value = 'changed after cache read'; |
| 32 | + assert.equal(next.run(args, () => assert.fail()).autogeneratedExamples.voice.value, 'marin'); |
| 33 | +}); |
| 34 | + |
| 35 | +test('schemas, parser settings, property inputs, and namespaces invalidate entries', (t) => { |
| 36 | + const { directory, args, cache } = fixture(t); |
| 37 | + cache.run(args, () => 'old'); |
| 38 | + for (const context of [ |
| 39 | + { ...args.context, spec: { changed: true } }, |
| 40 | + { ...args.context, settings: { changed: true } }, |
| 41 | + { ...args.context, generationLanguage: 'python' }, |
| 42 | + { ...args.context, smartCasing: true }, |
| 43 | + { ...args.context, documentBaseDir: '/different-reference-base' }, |
| 44 | + ]) assert.equal(cache.run({ ...args, context }, () => 'new'), 'new'); |
| 45 | + assert.equal(cache.run({ ...args, propertySchema: { type: 'number' } }, () => 'new'), 'new'); |
| 46 | + assert.equal(createExampleCache({ directory, namespace: 'new-version' }).run(args, () => 'new'), 'new'); |
| 47 | +}); |
| 48 | + |
| 49 | +test('diagnostics are emitted on every run and the collector is restored', (t) => { |
| 50 | + const { args, context, errors, cache } = fixture(t); |
| 51 | + const original = context.errorCollector.collect; |
| 52 | + for (let i = 0; i < 2; i++) { |
| 53 | + cache.run(args, () => { context.errorCollector.collect('invalid example'); return {}; }); |
| 54 | + assert.equal(context.errorCollector.collect, original); |
| 55 | + } |
| 56 | + assert.deepEqual(errors, ['invalid example', 'invalid example']); |
| 57 | + assert.equal(cache.stats.hits, 0); |
| 58 | +}); |
| 59 | + |
| 60 | +test('generation errors propagate and do not poison the cache', (t) => { |
| 61 | + const { args, context, cache } = fixture(t); |
| 62 | + const original = context.errorCollector.collect; |
| 63 | + assert.throws(() => cache.run(args, () => { throw new Error('invalid schema'); }), /invalid schema/); |
| 64 | + assert.equal(context.errorCollector.collect, original); |
| 65 | + assert.equal(cache.run(args, () => 'recovered'), 'recovered'); |
| 66 | +}); |
| 67 | + |
| 68 | +test('cache keys preserve null, undefined, and non-JSON numeric values', (t) => { |
| 69 | + const { args, cache } = fixture(t); |
| 70 | + for (const example of [null, undefined, NaN, Infinity]) { |
| 71 | + const inputs = { ...args, mediaExampleArgs: { example } }; |
| 72 | + assert.deepEqual(cache.run(inputs, () => ({ example })), { example }); |
| 73 | + assert.deepEqual(cache.run(inputs, () => assert.fail()), { example }); |
| 74 | + } |
| 75 | + assert.equal(cache.stats.misses, 4); |
| 76 | + assert.equal(cache.stats.hits, 4); |
| 77 | +}); |
| 78 | + |
| 79 | +test('fingerprints are stable across key order and shared object identities', () => { |
| 80 | + const shared = { text: 'hello', number: 3 }; |
| 81 | + assert.equal(fingerprint({ a: shared, b: shared }), fingerprint({ b: { number: 3, text: 'hello' }, a: { text: 'hello', number: 3 } })); |
| 82 | + assert.notEqual(fingerprint([undefined]), fingerprint(Array(1))); |
| 83 | + assert.notEqual(fingerprint(NaN), fingerprint(['number', 'NaN'])); |
| 84 | +}); |
| 85 | + |
| 86 | +test('corrupt entries fall back to original generation', (t) => { |
| 87 | + const { directory, args, cache } = fixture(t); |
| 88 | + cache.run(args, () => 'old'); |
| 89 | + const contextDirectory = path.join(directory, 'test', fs.readdirSync(path.join(directory, 'test'))[0]); |
| 90 | + fs.writeFileSync(path.join(contextDirectory, fs.readdirSync(contextDirectory)[0]), 'corrupt'); |
| 91 | + assert.equal(cache.run(args, () => 'regenerated'), 'regenerated'); |
| 92 | +}); |
| 93 | + |
| 94 | +test('all local API inputs invalidate the cache but prose does not', (t) => { |
| 95 | + const { directory } = fixture(t); |
| 96 | + fs.mkdirSync(path.join(directory, 'fern/apis/api'), { recursive: true }); |
| 97 | + fs.writeFileSync(path.join(directory, 'fern/fern.config.json'), '{"version":"5.112.0"}'); |
| 98 | + fs.writeFileSync(path.join(directory, 'fern/apis/api/openapi.json'), '{}'); |
| 99 | + const before = inputDigest(directory); |
| 100 | + fs.writeFileSync(path.join(directory, 'fern/overview.mdx'), 'new prose'); |
| 101 | + assert.equal(inputDigest(directory), before); |
| 102 | + fs.writeFileSync(path.join(directory, 'fern/apis/api/ai_examples_override.yml'), 'generated output'); |
| 103 | + assert.equal(inputDigest(directory), before); |
| 104 | + fs.writeFileSync(path.join(directory, 'fern/apis/api/overrides.yml'), 'new schema override'); |
| 105 | + assert.notEqual(inputDigest(directory), before); |
| 106 | +}); |
0 commit comments