|
| 1 | +import { afterEach, describe, expect, it } from "bun:test"; |
| 2 | +import { mkdtempSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { cleanupTursoTestDirectory } from "./turso-test-utils.js"; |
| 6 | + |
| 7 | +describe("plugin Turso refcount (#443)", () => { |
| 8 | + let baseDir: string; |
| 9 | + |
| 10 | + afterEach(async () => { |
| 11 | + const { resetPluginTursoRefsForTests } = await import("../src/services/turso/lifecycle.js"); |
| 12 | + resetPluginTursoRefsForTests(); |
| 13 | + await cleanupTursoTestDirectory(baseDir); |
| 14 | + }); |
| 15 | + |
| 16 | + it("releasePluginTurso keeps the pool open while another instance is alive", async () => { |
| 17 | + baseDir = mkdtempSync(join(tmpdir(), "turso-refcount-")); |
| 18 | + const { CONFIG } = await import("../src/config.js"); |
| 19 | + CONFIG.storagePath = baseDir; |
| 20 | + |
| 21 | + const { |
| 22 | + retainPluginTurso, |
| 23 | + releasePluginTurso, |
| 24 | + pluginTursoRefCount, |
| 25 | + resetPluginTursoRefsForTests, |
| 26 | + closeTursoAndInvalidateCaches, |
| 27 | + } = await import("../src/services/turso/lifecycle.js"); |
| 28 | + const { userPromptManager } = |
| 29 | + await import("../src/services/user-prompt/user-prompt-manager.js"); |
| 30 | + const { tursoConnectionManager } = await import("../src/services/turso/connection-manager.js"); |
| 31 | + |
| 32 | + await closeTursoAndInvalidateCaches(); |
| 33 | + resetPluginTursoRefsForTests(); |
| 34 | + |
| 35 | + retainPluginTurso(); |
| 36 | + retainPluginTurso(); |
| 37 | + expect(pluginTursoRefCount()).toBe(2); |
| 38 | + |
| 39 | + const id = await userPromptManager.savePrompt("sess-a", "msg-a", "/tmp/p", "hello"); |
| 40 | + const dbPath = join(baseDir, "user-prompts.db"); |
| 41 | + const handleBefore = await tursoConnectionManager.getConnection(dbPath); |
| 42 | + |
| 43 | + const closedOnFirstRelease = await releasePluginTurso(); |
| 44 | + expect(closedOnFirstRelease).toBe(false); |
| 45 | + expect(pluginTursoRefCount()).toBe(1); |
| 46 | + |
| 47 | + // Sibling instance still live: same pooled handle, queries still work. |
| 48 | + expect(await tursoConnectionManager.getConnection(dbPath)).toBe(handleBefore); |
| 49 | + const prompt = await userPromptManager.getPromptById(id); |
| 50 | + expect(prompt?.content).toBe("hello"); |
| 51 | + |
| 52 | + const closedOnLastRelease = await releasePluginTurso(); |
| 53 | + expect(closedOnLastRelease).toBe(true); |
| 54 | + expect(pluginTursoRefCount()).toBe(0); |
| 55 | + }); |
| 56 | + |
| 57 | + it("memoryClient.close only invalidates when the last lease is released", async () => { |
| 58 | + baseDir = mkdtempSync(join(tmpdir(), "turso-refcount-client-")); |
| 59 | + const { CONFIG } = await import("../src/config.js"); |
| 60 | + CONFIG.storagePath = baseDir; |
| 61 | + |
| 62 | + const { |
| 63 | + retainPluginTurso, |
| 64 | + pluginTursoRefCount, |
| 65 | + resetPluginTursoRefsForTests, |
| 66 | + closeTursoAndInvalidateCaches, |
| 67 | + } = await import("../src/services/turso/lifecycle.js"); |
| 68 | + const { memoryClient } = await import("../src/services/client.js"); |
| 69 | + const { userPromptManager } = |
| 70 | + await import("../src/services/user-prompt/user-prompt-manager.js"); |
| 71 | + |
| 72 | + await closeTursoAndInvalidateCaches(); |
| 73 | + resetPluginTursoRefsForTests(); |
| 74 | + |
| 75 | + retainPluginTurso(); |
| 76 | + retainPluginTurso(); |
| 77 | + expect(pluginTursoRefCount()).toBe(2); |
| 78 | + |
| 79 | + const id = await userPromptManager.savePrompt("sess-b", "msg-b", "/tmp/p", "world"); |
| 80 | + |
| 81 | + await memoryClient.close(); |
| 82 | + expect(pluginTursoRefCount()).toBe(1); |
| 83 | + expect((await userPromptManager.getPromptById(id))?.content).toBe("world"); |
| 84 | + |
| 85 | + await memoryClient.close(); |
| 86 | + expect(pluginTursoRefCount()).toBe(0); |
| 87 | + }); |
| 88 | +}); |
0 commit comments