Commit 8c264cc8 authored by wycer's avatar wycer
Browse files

fix(feedback): fix no penalty

parent e56d9c9d
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -66,7 +66,9 @@
		"typescript": "^5.3.3",
		"vite": "^5.0.3",
		"vite-plugin-prismjs": "^0.0.11",
		"vitest": "^1.2.0",
		"vite-tsconfig-paths": "^4.3.2",
		"vitest": "^1.3.1",
		"vitest-mock-extended": "^1.3.1",
		"zod": "^3.22.4"
	},
	"type": "module",
+59 −1
Original line number Diff line number Diff line
@@ -280,9 +280,15 @@ devDependencies:
  vite-plugin-prismjs:
    specifier: ^0.0.11
    version: 0.0.11(prismjs@1.29.0)
  vite-tsconfig-paths:
    specifier: ^4.3.2
    version: 4.3.2(typescript@5.3.3)(vite@5.1.5)
  vitest:
    specifier: ^1.2.0
    specifier: ^1.3.1
    version: 1.3.1(@types/node@20.11.24)(sass@1.71.1)
  vitest-mock-extended:
    specifier: ^1.3.1
    version: 1.3.1(typescript@5.3.3)(vitest@1.3.1)
  zod:
    specifier: ^3.22.4
    version: 3.22.4
@@ -8328,6 +8334,17 @@ packages:
    resolution: {integrity: sha512-WZ/iAJrKDhdINv1WG6KZIGHrZDar6VfhftG1QJFpVbOYZMYJLJOvZOo1amictRXVdBXZIgBHKswMTXzElngprA==}
    engines: {node: '>=14.13.1'}

  /ts-essentials@9.4.2(typescript@5.3.3):
    resolution: {integrity: sha512-mB/cDhOvD7pg3YCLk2rOtejHjjdSi9in/IBYE13S+8WA5FBSraYf4V/ws55uvs0IvQ/l0wBOlXy5yBNZ9Bl8ZQ==}
    peerDependencies:
      typescript: '>=4.1.0'
    peerDependenciesMeta:
      typescript:
        optional: true
    dependencies:
      typescript: 5.3.3
    dev: true

  /ts-interface-checker@0.1.13:
    resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==}

@@ -8361,6 +8378,19 @@ packages:
      v8-compile-cache-lib: 3.0.1
      yn: 3.1.1

  /tsconfck@3.0.3(typescript@5.3.3):
    resolution: {integrity: sha512-4t0noZX9t6GcPTfBAbIbbIU4pfpCwh0ueq3S4O/5qXI1VwK1outmxhe9dOiEWqMz3MW2LKgDTpqWV+37IWuVbA==}
    engines: {node: ^18 || >=20}
    hasBin: true
    peerDependencies:
      typescript: ^5.0.0
    peerDependenciesMeta:
      typescript:
        optional: true
    dependencies:
      typescript: 5.3.3
    dev: true

  /tslib@2.4.0:
    resolution: {integrity: sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==}
    optional: true
@@ -8637,6 +8667,23 @@ packages:
      - supports-color
    dev: true

  /vite-tsconfig-paths@4.3.2(typescript@5.3.3)(vite@5.1.5):
    resolution: {integrity: sha512-0Vd/a6po6Q+86rPlntHye7F31zA2URZMbH8M3saAZ/xR9QoGN/L21bxEGfXdWmFdNkqPpRdxFT7nmNe12e9/uA==}
    peerDependencies:
      vite: '*'
    peerDependenciesMeta:
      vite:
        optional: true
    dependencies:
      debug: 4.3.4
      globrex: 0.1.2
      tsconfck: 3.0.3(typescript@5.3.3)
      vite: 5.1.5(@types/node@20.11.24)(sass@1.71.1)
    transitivePeerDependencies:
      - supports-color
      - typescript
    dev: true

  /vite@5.1.5(@types/node@20.11.24)(sass@1.71.1):
    resolution: {integrity: sha512-BdN1xh0Of/oQafhU+FvopafUp6WaYenLU/NFoL5WyJL++GxkNfieKzBhM24H3HVsPQrlAqB7iJYTHabzaRed5Q==}
    engines: {node: ^18.0.0 || >=20.0.0}
@@ -8683,6 +8730,17 @@ packages:
    dependencies:
      vite: 5.1.5(@types/node@20.11.24)(sass@1.71.1)

  /vitest-mock-extended@1.3.1(typescript@5.3.3)(vitest@1.3.1):
    resolution: {integrity: sha512-OpghYjh4BDuQ/Mzs3lFMQ1QRk9D8/2O9T47MLUA5eLn7K4RWIy+MfIivYOWEyxjTENjsBnzgMihDjyNalN/K0Q==}
    peerDependencies:
      typescript: 3.x || 4.x || 5.x
      vitest: '>=0.31.1'
    dependencies:
      ts-essentials: 9.4.2(typescript@5.3.3)
      typescript: 5.3.3
      vitest: 1.3.1(@types/node@20.11.24)(sass@1.71.1)
    dev: true

  /vitest@1.3.1(@types/node@20.11.24)(sass@1.71.1):
    resolution: {integrity: sha512-/1QJqXs8YbCrfv/GPQ05wAZf2eakUPLPa18vkJAKE7RXOKfVHqMZZ1WlTjiwl6Gcn65M5vpNUB6EFLnEdRdEXQ==}
    engines: {node: ^18.0.0 || >=20.0.0}
+14 −0
Original line number Diff line number Diff line
// libs/__mocks__/prisma.ts
// 1
import { PrismaClient } from '@prisma/client'
import { beforeEach } from 'vitest'
import { mockDeep, mockReset } from 'vitest-mock-extended'

// 2
beforeEach(() => {
    mockReset(db)
})

// 3
const db = mockDeep<PrismaClient>()
export { db }
 No newline at end of file
+83 −0
Original line number Diff line number Diff line
import { describe, it, expect, test, vi } from 'vitest';
import { fixAndCalculatePenalty } from '$lib/server/db/feedback'
import { db } from '$lib/server/db/__mocks__';

vi.mock('$lib/server/db')

describe('create feedback', async () => {
    test('calculate penalty', async () => {
        const userId = 233
        const problemId = 450
        const submissions = [{
            id: 1,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: [
                {
                    penalty: 5,
                    penaltyAfter: 2
                }
            ],
            submitterId: userId,
            volumeId: "x",
            feedback: {

                isValid: true, score: 80
            }
        }, {
            id: 2,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: [
                {
                    penalty: 5,
                    penaltyAfter: 2
                }
            ],
            submitterId: userId,
            volumeId: "x",
            feedback: {

                isValid: true, score: 80
            }
        }, {
            id: 3,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: [
                {
                    penalty: 5,
                    penaltyAfter: 2
                }
            ],
            submitterId: userId,
            volumeId: "x",
            feedback: {

                isValid: true, score: 80
            }
        }, {
            id: 4,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: {
                id: problemId,
                penalty: 5,
                penaltyAfter: 2
            },
            submitterId: userId,
            volumeId: "x",
            feedback: {
                score: null
            }
        }]
        db.submission.findUnique.mockResolvedValue(submissions[3])
        db.submission.findMany.mockResolvedValue(submissions)
        const penalty = await fixAndCalculatePenalty(4)
        expect(penalty).toStrictEqual(10)
    })
})
 No newline at end of file
+3 −3
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ export const fixAndCalculatePenalty = async (currentSubmissionId: number): Promi
        }
    })
    if (_.isNil(submission)) {
        return 0;
        return null;
    }

    // determine the problem's penalty
@@ -55,18 +55,18 @@ export const fixAndCalculatePenalty = async (currentSubmissionId: number): Promi
    })

    let num = 0;
    const cal = (num: number) => (_.max([num - penaltyAfter, 0]) || 0) * penalty;
    const cal = (n: number) => (_.max([n - penaltyAfter, 0]) || 0) * penalty;
    for (const s of submissions) {
        if (_.isNil(s.feedback)) {
            // no feedback
            return null
        }
        const { feedback } = s
        num += feedback.isValid ? 1 : 0;
        if (feedback.score >= 0) {
            // already had score
            continue
        }
        num += feedback.isValid ? 1 : 0;
        const penaltyScore = cal(num)
        const originalScore = await getFeedbackScore(s.feedback)
        const finalScore = _.max([originalScore - penaltyScore, 0]) || 0;
Loading