feat: add extensive test suite
This commit is contained in:
@@ -7,15 +7,7 @@ import { actionLog } from "@/lib/api";
|
||||
import { adminCheck } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { tierlistColumns, tierlistTiers } from "@/lib/db/schema";
|
||||
import { midpointOrder, normalizedOrders } from "../components/order";
|
||||
|
||||
function sameIds(left: string[], right: string[]) {
|
||||
if (left.length !== right.length) return false;
|
||||
|
||||
const a = [...left].sort();
|
||||
const b = [...right].sort();
|
||||
return a.every((id, index) => id === b[index]);
|
||||
}
|
||||
import { reorderService } from "../components/reorder-service";
|
||||
|
||||
async function reorderTable(
|
||||
ids: string[],
|
||||
@@ -24,46 +16,38 @@ async function reorderTable(
|
||||
) {
|
||||
const table = type === "tiers" ? tierlistTiers : tierlistColumns;
|
||||
|
||||
await db.transaction(async (tx) => {
|
||||
const items = await tx.select().from(table).orderBy(table.order, table.id);
|
||||
const visibleIds = items.map((item) => item.id);
|
||||
if (!sameIds(visibleIds, ids)) {
|
||||
throw new Error("Layout order is out of date. Refresh and try again.");
|
||||
}
|
||||
|
||||
const index = ids.indexOf(activeId);
|
||||
if (index === -1) throw new Error("Moved item is missing.");
|
||||
|
||||
const prev =
|
||||
index > 0 ? items.find((item) => item.id === ids[index - 1]) : undefined;
|
||||
const next =
|
||||
index < ids.length - 1
|
||||
? items.find((item) => item.id === ids[index + 1])
|
||||
: undefined;
|
||||
const nextOrder = midpointOrder(prev?.order, next?.order);
|
||||
|
||||
if (nextOrder == null) {
|
||||
for (const item of normalizedOrders(ids)) {
|
||||
await tx
|
||||
.update(table)
|
||||
.set({ order: item.order })
|
||||
.where(eq(table.id, item.id));
|
||||
}
|
||||
} else {
|
||||
await tx
|
||||
.update(table)
|
||||
.set({ order: nextOrder })
|
||||
.where(eq(table.id, activeId));
|
||||
}
|
||||
});
|
||||
|
||||
await actionLog(`Reordered tierlist ${type}`, { activeId, ids });
|
||||
revalidatePath("/admin/tl/layout");
|
||||
revalidatePath("/tl");
|
||||
return reorderService(
|
||||
activeId,
|
||||
ids,
|
||||
"Layout order is out of date. Refresh and try again.",
|
||||
{
|
||||
adminCheck: async () => {
|
||||
if (!(await adminCheck())) redirect("/login");
|
||||
return true;
|
||||
},
|
||||
transaction: (callback) =>
|
||||
db.transaction((tx) =>
|
||||
callback({
|
||||
list: () =>
|
||||
tx
|
||||
.select({ id: table.id, order: table.order })
|
||||
.from(table)
|
||||
.orderBy(table.order, table.id),
|
||||
update: async (id, order) => {
|
||||
await tx.update(table).set({ order }).where(eq(table.id, id));
|
||||
},
|
||||
}),
|
||||
),
|
||||
afterReorder: async () => {
|
||||
await actionLog(`Reordered tierlist ${type}`, { activeId, ids });
|
||||
revalidatePath("/admin/tl/layout");
|
||||
revalidatePath("/tl");
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export async function reorderTiers(args: { activeId: string; ids: string[] }) {
|
||||
if (!(await adminCheck())) redirect("/login");
|
||||
return reorderTable(args.ids, args.activeId, "tiers");
|
||||
}
|
||||
|
||||
@@ -71,6 +55,5 @@ export async function reorderColumns(args: {
|
||||
activeId: string;
|
||||
ids: string[];
|
||||
}) {
|
||||
if (!(await adminCheck())) redirect("/login");
|
||||
return reorderTable(args.ids, args.activeId, "columns");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user