e2eSqliteReadiness.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { describe, expect, it, vi } from "vitest";
  2. import { waitForSqliteTable } from "./e2e/helpers/e2e-sqlite-readiness.js";
  3. describe("desktop e2e SQLite readiness", () => {
  4. it("waits until the database exists and the requested migration table is visible", async () => {
  5. let clock = 0;
  6. let attempt = 0;
  7. const close = vi.fn();
  8. const openDatabase = vi.fn(() => {
  9. attempt += 1;
  10. if (attempt === 1) {
  11. throw new Error("unable to open database file");
  12. }
  13. return {
  14. close,
  15. prepare: vi.fn(() => ({
  16. get: vi.fn(() => (attempt >= 3 ? { ready: 1 } : undefined)),
  17. })),
  18. };
  19. });
  20. await waitForSqliteTable("/e2e/cli/db/db.sqlite", "local_setting", {
  21. delay: async (ms) => {
  22. clock += ms;
  23. },
  24. now: () => clock,
  25. openDatabase,
  26. pollIntervalMs: 10,
  27. timeoutMs: 100,
  28. });
  29. expect(openDatabase).toHaveBeenCalledTimes(3);
  30. expect(close).toHaveBeenCalledTimes(2);
  31. });
  32. it("reports the database path, table, and last readiness error on timeout", async () => {
  33. let clock = 0;
  34. const openDatabase = vi.fn(() => {
  35. throw new Error("database is locked");
  36. });
  37. await expect(
  38. waitForSqliteTable("/e2e/cli/db/db.sqlite", "local_setting", {
  39. delay: async (ms) => {
  40. clock += ms;
  41. },
  42. now: () => clock,
  43. openDatabase,
  44. pollIntervalMs: 10,
  45. timeoutMs: 20,
  46. }),
  47. ).rejects.toThrow(
  48. "SQLite fixture 等待超时: path=/e2e/cli/db/db.sqlite, table=local_setting, lastError=database is locked",
  49. );
  50. });
  51. });