Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
chore(lobbies): fix tests with stricter validation
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Sep 17, 2024
1 parent 84bb55b commit 21c16e3
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 12 deletions.
8 changes: 6 additions & 2 deletions modules/lobbies/scripts/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface LobbyListEntry {
id: string;
version: string;
tags: Record<string, string>;
createdAt: string;
players: number;
maxPlayers: number;
maxPlayersDirect: number;
}

export async function run(
Expand Down Expand Up @@ -46,11 +50,11 @@ export async function run(
id: lobby.id,
version: lobby.version,
tags: lobby.tags,
createdAt: lobby.createdAt,
createdAt: new Date(lobby.createdAt).toISOString(),
players: lobby.players,
maxPlayers: lobby.maxPlayers,
maxPlayersDirect: lobby.maxPlayersDirect,
}));
} satisfies LobbyListEntry));

return { lobbies: lobbyList };
}
2 changes: 1 addition & 1 deletion modules/lobbies/tests/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test("lobby tags", async (ctx: TestContext) => {
{
version: VERSION,
region: REGION,
tags: { gameMode: "a", region: "atl" },
tags: { gameMode: "a" },
players: [{}],
maxPlayers: 8,
maxPlayersDirect: 8,
Expand Down
4 changes: 2 additions & 2 deletions modules/tokens/scripts/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ScriptContext, Database, Query } from "../module.gen.ts";
import { TokenWithSecret, tokenFromRow } from "../utils/types.ts";
import { TokenWithSecret, tokenFromRow, tokenWithSecretFromRow } from "../utils/types.ts";

export interface Request {
type: string;
Expand Down Expand Up @@ -28,7 +28,7 @@ export async function run(
.returning();

return {
token: tokenFromRow(rows[0]!),
token: tokenWithSecretFromRow(rows[0]!),
};
}

Expand Down
4 changes: 2 additions & 2 deletions modules/tokens/scripts/extend.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ScriptContext, Query, Database } from "../module.gen.ts";
import { tokenFromRow, TokenWithSecret } from "../utils/types.ts";
import { tokenFromRow, TokenWithSecret, tokenWithSecretFromRow } from "../utils/types.ts";

export interface Request {
token: string;
Expand Down Expand Up @@ -27,6 +27,6 @@ export async function run(

// Return the updated token
return {
token: tokenFromRow(rows[0]!),
token: tokenWithSecretFromRow(rows[0]!),
};
}
2 changes: 1 addition & 1 deletion modules/tokens/scripts/revoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface Request {
}

export interface Response {
updates: { [key: string]: TokenUpdate };
updates: Record<string, TokenUpdate>;
}

export enum TokenUpdate {
Expand Down
5 changes: 3 additions & 2 deletions modules/tokens/tests/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
assertEquals,
assertGreater,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";
} from "jsr:@std/assert";
import { omit } from "jsr:@std/collections";

test(
"validate token not found",
Expand Down Expand Up @@ -95,7 +96,7 @@ test(
...validateResAfterWait.token,
expireAt: null,
}, {
...token,
...omit(token, ["token"]),
expireAt: null,
});
},
Expand Down
15 changes: 13 additions & 2 deletions modules/tokens/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@ export interface TokenWithSecret extends Token {

export function tokenFromRow(
row: typeof Database.tokens.$inferSelect
): TokenWithSecret {
): Token {
return {
...row,
id: row.id,
type: row.type,
meta: row.meta,
createdAt: row.createdAt.toISOString(),
expireAt: row.expireAt?.toISOString() ?? null,
revokedAt: row.revokedAt?.toISOString() ?? null,
};
}

export function tokenWithSecretFromRow(
row: typeof Database.tokens.$inferSelect
): TokenWithSecret {
return {
...tokenFromRow(row),
token: row.token,
};
}

0 comments on commit 21c16e3

Please sign in to comment.