Skip to content

Commit

Permalink
Merge pull request #69 from moeyashi/feature/57
Browse files Browse the repository at this point in the history
Feature/57
  • Loading branch information
moeyashi authored Mar 17, 2024
2 parents 0c5b909 + 973efbf commit a611beb
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 30 deletions.
12 changes: 9 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# flyctl launch added from .gitignore
node_modules
**\.env
.github/
node_modules/
wr-python/
**/.env*
.eslint*
.git*
fly.toml
**/*.md
schema.hcl
**/*.test.*
17 changes: 0 additions & 17 deletions .github/workflows/test-track-js.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test

on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
# refer: https://hub.docker.com/_/postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 25432:5432
steps:
- uses: actions/checkout@v4
- uses: ariga/setup-atlas@master
- uses: actions/setup-node@v4
with:
cache: "npm"
- run: npm install
- run: npm test run
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"type": "module",
"scripts": {
"lint": "eslint .",
"db:run": "docker run --name mk8d-nita-record-db -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e POSTGRES_DB=postgres -p 25432:5432 -d postgres",
"db:migrate": "atlas schema apply --url \"postgres://postgres:postgres@localhost:25432?sslmode=disable\" --file schema.hcl --auto-approve",
"db:clean": "atlas schema clean --url \"postgres://postgres:postgres@localhost:25432?sslmode=disable\" --auto-approve",
"db:reset": "npm run db:clean && npm run db:migrate",
"test": "vitest",
"start": "node .",
"deploy:commands": "node ./src/deploy-commands.js"
Expand Down
16 changes: 8 additions & 8 deletions src/infra/repository/nita.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const postgresNitaRepository = () => {
host: process.env.DATABASE_HOST,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
port: 5432,
port: Number(process.env.DATABASE_PORT || '5432'),
};

const sql = postgres(config);
Expand Down Expand Up @@ -88,15 +88,15 @@ export const postgresNitaRepository = () => {
lastMilliseconds: row.last_milliseconds,
}));
},
/**
* @param {string} trackCode
* @param {import('discord.js').GuildMember[]} discordMembers
* @returns {Promise<{member: import('discord.js').GuildMember, milliseconds: number}[]>}
*/
async selectRanking(trackCode, discordMembers) {
async selectRanking(trackCode, discordMembers, limit = 20) {
const discordUserIds = discordMembers.map((member) => member.user.id);
const results = await sql`
SELECT discord_user_id, milliseconds FROM nita WHERE discord_user_id IN ${sql(discordUserIds)} AND track_code = ${trackCode} ORDER BY milliseconds ASC
SELECT discord_user_id, milliseconds
FROM nita
WHERE discord_user_id IN ${sql(discordUserIds)}
AND track_code = ${trackCode}
ORDER BY milliseconds ASC
LIMIT ${limit}
`;
return results.map((row) => ({
member: discordMembers.find(member => member.user.id === row.discord_user_id) || discordMembers[0],
Expand Down
49 changes: 49 additions & 0 deletions src/infra/repository/nita.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { execSync } from 'child_process';
import { expect, test, describe, beforeAll } from 'vitest';
import { postgresNitaRepository } from './nita.js';

describe('postgresNitaRepository', () => {
const repo = postgresNitaRepository();
describe('selectRanking', async () => {
const trackCode = 'MKS';
const users = Array.from({ length: 21 }).map((_, i) => ({
user: { id: `${i + 1}` },
}));
beforeAll(async () => {
execSync('npm run db:reset');
for (const i of users) {
await repo.insertNita({
discordUserId: i.user.id,
trackCode,
milliseconds: i.user.id === '2' ? 99999 : 1000 + Number(i.user.id),
});
}
});

test('上位20名が取得されること', async () => {
const ranking = await repo.selectRanking(trackCode, users);
expect(ranking).toEqual([
{ member: { user: { id: '1' } }, milliseconds: 1001 },
{ member: { user: { id: '3' } }, milliseconds: 1003 },
{ member: { user: { id: '4' } }, milliseconds: 1004 },
{ member: { user: { id: '5' } }, milliseconds: 1005 },
{ member: { user: { id: '6' } }, milliseconds: 1006 },
{ member: { user: { id: '7' } }, milliseconds: 1007 },
{ member: { user: { id: '8' } }, milliseconds: 1008 },
{ member: { user: { id: '9' } }, milliseconds: 1009 },
{ member: { user: { id: '10' } }, milliseconds: 1010 },
{ member: { user: { id: '11' } }, milliseconds: 1011 },
{ member: { user: { id: '12' } }, milliseconds: 1012 },
{ member: { user: { id: '13' } }, milliseconds: 1013 },
{ member: { user: { id: '14' } }, milliseconds: 1014 },
{ member: { user: { id: '15' } }, milliseconds: 1015 },
{ member: { user: { id: '16' } }, milliseconds: 1016 },
{ member: { user: { id: '17' } }, milliseconds: 1017 },
{ member: { user: { id: '18' } }, milliseconds: 1018 },
{ member: { user: { id: '19' } }, milliseconds: 1019 },
{ member: { user: { id: '20' } }, milliseconds: 1020 },
{ member: { user: { id: '21' } }, milliseconds: 1021 },
]);
});
});
});
2 changes: 1 addition & 1 deletion src/slash-command/ranking.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
const groupedRanking = groppByRank(track, ranking);

await interaction.followUp({
content: `NITAランキング - ${track.trackName}`,
content: `## NITAランキング - ${track.trackName}\n※一時的に上位20名までを出力しています`,
embeds: groupedRanking.flatMap(([rank, color, nita]) => {
/** @type {import('discord.js').APIEmbed[]} */
const embeds = [];
Expand Down
6 changes: 5 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type {

export type EventType = keyof ClientEvents;

type InsertNitaParameters = Nita;
type UpdateNitaParameters = Nita;

export type NitaRepository = {
insertNita: (params: InsertNitaParameters) => Promise<any>;
updateNita: (params: UpdateNitaParameters) => Promise<any>;
Expand All @@ -19,7 +22,8 @@ export type NitaRepository = {
selectNitaByUser: (discordUserId: string) => Promise<Nita[]>;
selectRanking: (
trackCode: string,
discordMembers: GuildMember[]
discordMembers: GuildMember[],
limit?: number
) => Promise<{ member: GuildMember; milliseconds: number }[]>;
};

Expand Down
12 changes: 12 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
env: {
DATABASE_HOST: "localhost",
DATABASE_PORT: "25432",
DATABASE_USERNAME: "postgres",
DATABASE_PASSWORD: "postgres",
},
},
});

0 comments on commit a611beb

Please sign in to comment.