fix: AsyncDrain double-allocation bug and history test singleton isolation

This commit is contained in:
can1357
2026-04-08 12:52:32 +02:00
parent 468601935c
commit 2bdc80eedc
2 changed files with 12 additions and 3 deletions
@@ -28,8 +28,7 @@ class AsyncDrain<T> {
push(value: T, hnd: (values: T[]) => Promise<void> | void): Promise<void> {
let queue = this.#queue;
if (!queue) {
queue = [];
this.#queue = [];
this.#queue = queue = [];
this.#promise = new Promise((resolve, reject) => {
const exec = () => {
try {
@@ -128,6 +127,11 @@ CREATE TRIGGER IF NOT EXISTS history_ai AFTER INSERT ON history BEGIN
return HistoryStorage.#instance;
}
/** @internal Reset the singleton — test-only. */
static resetInstance(): void {
HistoryStorage.#instance = undefined;
}
#insertBatch(rows: Array<Pick<HistoryEntry, "prompt" | "cwd">>): void {
this.#db.transaction((rows: Array<Pick<HistoryEntry, "prompt" | "cwd">>) => {
for (const row of rows) {
@@ -1,5 +1,5 @@
import { Database } from "bun:sqlite";
import { afterEach, expect, it } from "bun:test";
import { afterEach, beforeEach, expect, it } from "bun:test";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
@@ -21,7 +21,12 @@ function readTableSql(dbPath: string, tableName: string): string | null {
let tempDir = "";
beforeEach(() => {
HistoryStorage.resetInstance();
});
afterEach(async () => {
HistoryStorage.resetInstance();
if (tempDir) {
await fs.rm(tempDir, { recursive: true, force: true });
tempDir = "";