|
1 | 1 | // @effect-diagnostics-next-line nodeBuiltinImport:off - Effect's File.Info has no ctime or sub-millisecond mtime, which the cache tests compare. |
2 | 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | +import * as NodeSqlite from "node:sqlite"; |
3 | 4 |
|
4 | 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
5 | | -import { assert, describe, it } from "@effect/vitest"; |
| 6 | +import { assert, describe, it, vi } from "@effect/vitest"; |
6 | 7 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
7 | 8 | import * as Context from "effect/Context"; |
8 | 9 | import * as Effect from "effect/Effect"; |
9 | 10 | import * as FileSystem from "effect/FileSystem"; |
10 | 11 | import * as Layer from "effect/Layer"; |
11 | 12 | import * as Path from "effect/Path"; |
12 | 13 | import * as SqlClient from "effect/sql/SqlClient"; |
| 14 | +import * as Tracer from "effect/Tracer"; |
13 | 15 |
|
14 | 16 | import { makeAntigravityUsageCache, readAntigravityUsage } from "./antigravityUsageReader.ts"; |
15 | 17 |
|
@@ -445,4 +447,141 @@ describe("readAntigravityUsage", () => { |
445 | 447 | assert.strictEqual(records(result)[0]?.totals.uncachedInputTokens, 10); |
446 | 448 | }).pipe(Effect.provide(NodeServices.layer)), |
447 | 449 | ); |
| 450 | + |
| 451 | + /** Counts the rows each StatementSync.all() call returns, by its SQL. */ |
| 452 | + const recordReads = Effect.gen(function* () { |
| 453 | + const reads: Array<{ sql: string; rows: number }> = []; |
| 454 | + const all = NodeSqlite.StatementSync.prototype.all; |
| 455 | + yield* Effect.acquireRelease( |
| 456 | + Effect.sync(() => |
| 457 | + vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function ( |
| 458 | + this: NodeSqlite.StatementSync, |
| 459 | + ...params |
| 460 | + ) { |
| 461 | + const result = all.apply(this, params); |
| 462 | + reads.push({ sql: this.sourceSQL, rows: result.length }); |
| 463 | + return result; |
| 464 | + }), |
| 465 | + ), |
| 466 | + (spy) => Effect.sync(() => spy.mockRestore()), |
| 467 | + ); |
| 468 | + return reads; |
| 469 | + }); |
| 470 | + |
| 471 | + const stepUsage = (responseId: string) => |
| 472 | + new Uint8Array(protoBytes(9, [...protoNumber(2, 10), ...protoText(11, responseId)])); |
| 473 | + |
| 474 | + it.effect("reads one Antigravity generation or one page of steps per statement", () => |
| 475 | + Effect.gen(function* () { |
| 476 | + const path = yield* Path.Path; |
| 477 | + const dir = yield* tempDir; |
| 478 | + yield* seedDatabase(path.join(dir, "session-1.db"), (db) => |
| 479 | + Effect.gen(function* () { |
| 480 | + // Antigravity's keys and indexes: idx is the INTEGER PRIMARY KEY, and steps has indexes. |
| 481 | + yield* db.exec( |
| 482 | + "CREATE TABLE `gen_metadata` (`idx` integer,`data` blob,`size` integer NOT NULL DEFAULT 0,PRIMARY KEY (`idx`))", |
| 483 | + ); |
| 484 | + yield* db.exec( |
| 485 | + "CREATE TABLE `steps` (`idx` integer,`step_type` integer NOT NULL DEFAULT 0,`status` integer NOT NULL DEFAULT 0,`metadata` blob,PRIMARY KEY (`idx`))", |
| 486 | + ); |
| 487 | + yield* db.exec("CREATE INDEX `idx_steps_status` ON `steps`(`status`)"); |
| 488 | + yield* db.exec("CREATE INDEX `idx_steps_step_type` ON `steps`(`step_type`)"); |
| 489 | + yield* db.exec("BEGIN"); |
| 490 | + for (let idx = 0; idx < 3; idx++) { |
| 491 | + yield* db.run("INSERT INTO gen_metadata (idx, data) VALUES (?, ?)", [ |
| 492 | + idx, |
| 493 | + antigravityGeneration(`g-${idx}`), |
| 494 | + ]); |
| 495 | + } |
| 496 | + // A run of steps without metadata sits between steps with usage. |
| 497 | + for (let idx = 0; idx < 250; idx++) { |
| 498 | + yield* db.run("INSERT INTO steps (idx, metadata) VALUES (?, ?)", [ |
| 499 | + idx, |
| 500 | + idx >= 75 && idx < 175 ? null : stepUsage(`s-${idx}`), |
| 501 | + ]); |
| 502 | + } |
| 503 | + yield* db.exec("COMMIT"); |
| 504 | + }), |
| 505 | + ); |
| 506 | + const reads = yield* recordReads; |
| 507 | + const spans: Tracer.NativeSpan[] = []; |
| 508 | + const tracer = Tracer.make({ |
| 509 | + span: (options) => { |
| 510 | + const span = new Tracer.NativeSpan(options); |
| 511 | + spans.push(span); |
| 512 | + return span; |
| 513 | + }, |
| 514 | + }); |
| 515 | + const result = yield* readAntigravityUsage(dir, 0).pipe( |
| 516 | + Effect.provideService(Tracer.Tracer, tracer), |
| 517 | + ); |
| 518 | + assert.deepStrictEqual(result.errors, []); |
| 519 | + assert.deepStrictEqual( |
| 520 | + records(result).map((record) => record.dedupeKey), |
| 521 | + [ |
| 522 | + ...Array.from({ length: 75 }, (_, idx) => `antigravity:11:s-${idx}`), |
| 523 | + ...Array.from({ length: 75 }, (_, idx) => `antigravity:11:s-${idx + 175}`), |
| 524 | + ...Array.from({ length: 3 }, (_, idx) => `antigravity:11:g-${idx}`), |
| 525 | + ], |
| 526 | + ); |
| 527 | + // Page statements are untraced, so the trace does not grow with the tables. |
| 528 | + assert.isTrue(spans.some((span) => span.name === "sqlite")); |
| 529 | + assert.deepStrictEqual( |
| 530 | + spans.flatMap((span) => { |
| 531 | + const query = span.attributes.get("db.query.text"); |
| 532 | + return typeof query === "string" && query.includes("rowid > ?") ? [query] : []; |
| 533 | + }), |
| 534 | + [], |
| 535 | + ); |
| 536 | + // Every row, with metadata or not, comes back through a bounded statement. |
| 537 | + for (const [table, total, most] of [ |
| 538 | + ["steps", 250, 64], |
| 539 | + ["gen_metadata", 3, 1], |
| 540 | + ] as const) { |
| 541 | + const rows = reads |
| 542 | + .filter((read) => read.sql.includes(`FROM ${table} `)) |
| 543 | + .map((read) => read.rows); |
| 544 | + assert.strictEqual( |
| 545 | + rows.reduce((sum, count) => sum + count, 0), |
| 546 | + total, |
| 547 | + table, |
| 548 | + ); |
| 549 | + assert.isAtMost(Math.max(...rows), most, table); |
| 550 | + } |
| 551 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 552 | + ); |
| 553 | + |
| 554 | + it.effect("returns Antigravity records in idx order however the rows are stored", () => |
| 555 | + Effect.gen(function* () { |
| 556 | + const path = yield* Path.Path; |
| 557 | + const dir = yield* tempDir; |
| 558 | + yield* seedDatabase(path.join(dir, "session-1.db"), (db) => |
| 559 | + Effect.gen(function* () { |
| 560 | + yield* createTables(db); |
| 561 | + yield* db.exec("BEGIN"); |
| 562 | + // Stored in reverse idx order, so rowid order is the opposite of idx order. |
| 563 | + for (let idx = 99; idx >= 0; idx--) { |
| 564 | + yield* db.run("INSERT INTO steps VALUES (?, ?)", [idx, stepUsage(`s-${idx}`)]); |
| 565 | + } |
| 566 | + for (const idx of [1, 0]) { |
| 567 | + yield* db.run("INSERT INTO gen_metadata VALUES (?, ?)", [ |
| 568 | + idx, |
| 569 | + antigravityGeneration(`g-${idx}`), |
| 570 | + ]); |
| 571 | + } |
| 572 | + yield* db.exec("COMMIT"); |
| 573 | + }), |
| 574 | + ); |
| 575 | + const result = yield* readAntigravityUsage(dir, 0); |
| 576 | + assert.deepStrictEqual(result.errors, []); |
| 577 | + assert.deepStrictEqual( |
| 578 | + records(result).map((record) => record.dedupeKey), |
| 579 | + [ |
| 580 | + ...Array.from({ length: 100 }, (_, idx) => `antigravity:11:s-${idx}`), |
| 581 | + "antigravity:11:g-0", |
| 582 | + "antigravity:11:g-1", |
| 583 | + ], |
| 584 | + ); |
| 585 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 586 | + ); |
448 | 587 | }); |
0 commit comments