|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import { PineconeClient } from '../pinecone-client.js'; |
| 3 | +import { resolveConfig } from '../config.js'; |
| 4 | +import { setPineconeClient } from '../server/client-context.js'; |
| 5 | +import { setupCoreServer, teardownServer } from '../setup.js'; |
| 6 | +import { resolveTestConfig } from './tools/test-helpers.js'; |
| 7 | +import { ServerContext, createServer, teardownDefaultServerContext } from './server-context.js'; |
| 8 | + |
| 9 | +describe('ServerContext lifecycle', () => { |
| 10 | + afterEach(() => { |
| 11 | + teardownServer(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('sets disposed after teardown()', () => { |
| 15 | + const ctx = new ServerContext(resolveTestConfig()); |
| 16 | + ctx.teardown(); |
| 17 | + expect(ctx.disposed).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it('sets disposed after teardownDefaultServerContext()', () => { |
| 21 | + const ctx = createServer(resolveTestConfig()); |
| 22 | + teardownDefaultServerContext(); |
| 23 | + expect(ctx.disposed).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('await using disposes ServerContext on scope exit', async () => { |
| 27 | + const config = resolveTestConfig(); |
| 28 | + let ctx!: ServerContext; |
| 29 | + await (async () => { |
| 30 | + await using scoped = new ServerContext(config); |
| 31 | + ctx = scoped; |
| 32 | + expect(scoped.disposed).toBe(false); |
| 33 | + })(); |
| 34 | + expect(ctx.disposed).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('await using on setupCoreServer return value tears down and allows re-setup', async () => { |
| 38 | + const cfg = resolveConfig({ apiKey: 'lifecycle-await-key', indexName: 'test-index' }); |
| 39 | + setPineconeClient( |
| 40 | + new PineconeClient({ |
| 41 | + apiKey: cfg.apiKey, |
| 42 | + indexName: cfg.indexName, |
| 43 | + rerankModel: cfg.rerankModel, |
| 44 | + defaultTopK: cfg.defaultTopK, |
| 45 | + }) |
| 46 | + ); |
| 47 | + |
| 48 | + await (async () => { |
| 49 | + await using _server = await setupCoreServer(cfg); |
| 50 | + })(); |
| 51 | + |
| 52 | + await expect(setupCoreServer(cfg)).resolves.toBeDefined(); |
| 53 | + teardownServer(); |
| 54 | + }); |
| 55 | +}); |
0 commit comments