File size: 4,801 Bytes
97ee7cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | /**
* Schema-invariant locks for the `users` table. Catches accidental
* regressions if a future edit breaks required-field enforcement, optional
* field handling, or one of the three indexes.
*/
import { convexTest } from "convex-test";
import { describe, expect, test } from "vitest";
import schema from "../schema";
const modules = import.meta.glob("../**/*.ts");
describe("users schema invariants", () => {
test("required fields only — userId, firstSeenAt, lastSeenAt are sufficient to insert", async () => {
const t = convexTest(schema, modules);
const now = Date.now();
await t.run(async (ctx) => {
await ctx.db.insert("users", {
userId: "u-required-only",
firstSeenAt: now,
lastSeenAt: now,
});
});
const row = await t.run(async (ctx) =>
await ctx.db
.query("users")
.withIndex("by_userId", (q) => q.eq("userId", "u-required-only"))
.unique(),
);
expect(row).not.toBeNull();
expect(row?.email).toBeUndefined();
expect(row?.localeTag).toBeUndefined();
expect(row?.country).toBeUndefined();
});
test("all fields populated — round-trip preserves all values", async () => {
const t = convexTest(schema, modules);
const now = Date.now();
await t.run(async (ctx) => {
await ctx.db.insert("users", {
userId: "u-full",
email: "alice@example.com",
normalizedEmail: "alice@example.com",
localeTag: "zh-Hant-TW",
localePrimary: "zh",
timezone: "Asia/Taipei",
country: "TW",
firstSeenAt: now,
lastSeenAt: now,
});
});
const row = await t.run(async (ctx) =>
await ctx.db
.query("users")
.withIndex("by_userId", (q) => q.eq("userId", "u-full"))
.unique(),
);
expect(row?.email).toBe("alice@example.com");
expect(row?.localeTag).toBe("zh-Hant-TW");
expect(row?.localePrimary).toBe("zh");
expect(row?.timezone).toBe("Asia/Taipei");
expect(row?.country).toBe("TW");
});
test("by_normalizedEmail index returns row by email", async () => {
const t = convexTest(schema, modules);
const now = Date.now();
await t.run(async (ctx) => {
await ctx.db.insert("users", {
userId: "u-email-index",
normalizedEmail: "lookup@example.com",
firstSeenAt: now,
lastSeenAt: now,
});
});
const row = await t.run(async (ctx) =>
await ctx.db
.query("users")
.withIndex("by_normalizedEmail", (q) =>
q.eq("normalizedEmail", "lookup@example.com"),
)
.unique(),
);
expect(row?.userId).toBe("u-email-index");
});
test("by_localePrimary index returns matches", async () => {
const t = convexTest(schema, modules);
const now = Date.now();
await t.run(async (ctx) => {
await ctx.db.insert("users", {
userId: "u-zh-1",
localePrimary: "zh",
firstSeenAt: now,
lastSeenAt: now,
});
await ctx.db.insert("users", {
userId: "u-zh-2",
localePrimary: "zh",
firstSeenAt: now,
lastSeenAt: now,
});
await ctx.db.insert("users", {
userId: "u-en-1",
localePrimary: "en",
firstSeenAt: now,
lastSeenAt: now,
});
});
const zhRows = await t.run(async (ctx) =>
await ctx.db
.query("users")
.withIndex("by_localePrimary", (q) => q.eq("localePrimary", "zh"))
.collect(),
);
expect(zhRows.length).toBe(2);
expect(zhRows.every((r) => r.localePrimary === "zh")).toBe(true);
});
test("localeTag and localePrimary independently optional", async () => {
const t = convexTest(schema, modules);
const now = Date.now();
// localeTag set, localePrimary unset
await t.run(async (ctx) => {
await ctx.db.insert("users", {
userId: "u-tag-only",
localeTag: "fr-CA",
firstSeenAt: now,
lastSeenAt: now,
});
// localePrimary set, localeTag unset
await ctx.db.insert("users", {
userId: "u-primary-only",
localePrimary: "en",
firstSeenAt: now,
lastSeenAt: now,
});
});
const tagOnly = await t.run(async (ctx) =>
await ctx.db
.query("users")
.withIndex("by_userId", (q) => q.eq("userId", "u-tag-only"))
.unique(),
);
const primaryOnly = await t.run(async (ctx) =>
await ctx.db
.query("users")
.withIndex("by_userId", (q) => q.eq("userId", "u-primary-only"))
.unique(),
);
expect(tagOnly?.localeTag).toBe("fr-CA");
expect(tagOnly?.localePrimary).toBeUndefined();
expect(primaryOnly?.localeTag).toBeUndefined();
expect(primaryOnly?.localePrimary).toBe("en");
});
});
|