session-database-identity-migration.test.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { DatabaseSync } from "node:sqlite";
  2. import { expect, it } from "vitest";
  3. import {
  4. runSqliteSessionMigrations,
  5. runSqliteSessionMigrationsAsync,
  6. type SqliteMigrationProgress,
  7. } from "../src/storage/session-store/migration-runner.js";
  8. import { runSessionDatabaseIdentityMigration } from "@zcode/shared/sqlite-migrations";
  9. it("Source 记账失败回滚新表,Agent 与 Source 都拒绝已有 checksum 损坏", async () => {
  10. const db = new DatabaseSync(":memory:");
  11. try {
  12. db.exec(`CREATE TABLE schema_migration (id TEXT PRIMARY KEY, checksum TEXT NOT NULL, app_version TEXT, time_applied INTEGER NOT NULL);
  13. CREATE TRIGGER reject_identity BEFORE INSERT ON schema_migration
  14. WHEN NEW.id='0024_session_database_identity'
  15. BEGIN SELECT RAISE(ABORT, 'injected identity ledger failure'); END;`);
  16. expect(() => runSessionDatabaseIdentityMigration(db)).toThrow(
  17. "injected identity ledger failure",
  18. );
  19. expect(
  20. db.prepare("SELECT 1 FROM sqlite_master WHERE name='zcode_database_identity'").get(),
  21. ).toBeUndefined();
  22. expect(db.prepare("SELECT * FROM schema_migration").all()).toEqual([]);
  23. db.exec("DROP TRIGGER reject_identity");
  24. runSessionDatabaseIdentityMigration(db);
  25. db.exec("UPDATE schema_migration SET checksum='broken'");
  26. expect(() => runSessionDatabaseIdentityMigration(db)).toThrow(/checksum mismatch/);
  27. await expect(runSqliteSessionMigrations(db, ":memory:")).rejects.toThrow(/checksum mismatch/);
  28. // Agent 更早的迁移也在同一事务内回滚,损坏记录不会被覆盖。
  29. expect(db.prepare("SELECT id, checksum FROM schema_migration").all()).toEqual([
  30. { id: "0024_session_database_identity", checksum: "broken" },
  31. ]);
  32. } finally {
  33. db.close();
  34. }
  35. });
  36. it("Agent 正式迁移接管已有 Source identity,不覆盖代际且重复打开不改账本", async () => {
  37. const db = new DatabaseSync(":memory:");
  38. try {
  39. db.exec(`CREATE TABLE zcode_database_identity (scope TEXT PRIMARY KEY, identity TEXT NOT NULL);
  40. INSERT INTO zcode_database_identity VALUES ('session', 'existing-generation')`);
  41. await runSqliteSessionMigrations(db, ":memory:");
  42. expect(
  43. db.prepare("SELECT id FROM schema_migration WHERE id='0024_session_database_identity'").get(),
  44. ).toEqual({
  45. id: "0024_session_database_identity",
  46. });
  47. const ledger = db.prepare("SELECT * FROM schema_migration ORDER BY id").all();
  48. await runSqliteSessionMigrations(db, ":memory:");
  49. expect(db.prepare("SELECT * FROM schema_migration ORDER BY id").all()).toEqual(ledger);
  50. expect(db.prepare("SELECT identity FROM zcode_database_identity").get()).toEqual({
  51. identity: "existing-generation",
  52. });
  53. } finally {
  54. db.close();
  55. }
  56. });
  57. it("Agent/Source 共用应用原语,identity SQL 成功但记账失败时保留真实执行数量", async () => {
  58. const db = new DatabaseSync(":memory:");
  59. const states: SqliteMigrationProgress[] = [];
  60. try {
  61. await expect(
  62. runSqliteSessionMigrationsAsync(db, ":memory:", {
  63. onProgress: async (state) => {
  64. states.push(state);
  65. if (
  66. state.phase === "migrating" &&
  67. state.migrationId === "0024_session_database_identity"
  68. ) {
  69. db.exec(`CREATE TRIGGER reject_identity BEFORE INSERT ON schema_migration
  70. WHEN NEW.id='0024_session_database_identity'
  71. BEGIN SELECT RAISE(ABORT, 'injected identity ledger failure'); END;`);
  72. }
  73. },
  74. }),
  75. ).rejects.toMatchObject({ kind: "sql_failed", migrationId: "0024_session_database_identity" });
  76. expect(states.at(-1)).toMatchObject({
  77. phase: "failed",
  78. migration: { kind: "initialize", executedCount: 24, committedCount: 0 },
  79. });
  80. expect(db.isTransaction).toBe(false);
  81. expect(
  82. db.prepare("SELECT 1 FROM sqlite_master WHERE name='zcode_database_identity'").get(),
  83. ).toBeUndefined();
  84. expect(
  85. db.prepare("SELECT 1 FROM sqlite_master WHERE name='schema_migration'").get(),
  86. ).toBeUndefined();
  87. } finally {
  88. db.close();
  89. }
  90. });