Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit stats in time #50

Merged
merged 2 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 2 additions & 0 deletions __mocks__/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module.exports = {
setEndTime: jest.fn(async (instanceRef, timezone) => {}),
setStartTime: jest.fn(async (instanceRef, timezone) => {}),
setTimezone: jest.fn(async (instanceRef, timezone) => {}),
setUserSetting: jest.fn(async (instanceRef, userRef, name, value) => {}),
setWeekdays: jest.fn(async (instanceRef, weekdayMask) => {}),
scheduled: jest.fn(async (instanceRef) => {}),
storeScheduled: jest.fn(async (instanceRef, timestamp, messageId, channel) => {}),
Expand All @@ -96,5 +97,6 @@ module.exports = {
}),
async recentClickTimes () { return [] },
async slowestClickTimes (instanceRef) { return [] },
async userSettings (instanceRef, userRef) { return {} },
async winningStreaks (instanceRef) { return [{ user: 'test1', streak: 3 }] }
}
220 changes: 220 additions & 0 deletions __tests__/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ describe('database', () => {
authedUser: {
id: 'U9'
},
userSettings: {
U1: { statsInterval: 0 },
U2: { statsInterval: 7 }
},
channel: 'C1',
scheduled: {}
}
Expand Down Expand Up @@ -214,6 +218,30 @@ describe('database', () => {
{ user: 'U99991111', count: 1 }
])
})

test('is sorted and correct no interval stats set', async () => {
const clicks = await db.clicksPerUser('T2', 'U0')
expect(clicks).toEqual([
{ user: 'U12341234', count: 2 },
{ user: 'U99991111', count: 1 }
])
})

test('is sorted and correct last forever stats', async () => {
const clicks = await db.clicksPerUser('T2', 'U1')
expect(clicks).toEqual([
{ user: 'U12341234', count: 2 },
{ user: 'U99991111', count: 1 }
])
})

test('is sorted and correct last 7 days stats', async () => {
const clicks = await db.clicksPerUser('T2', 'U2', DateTime.fromISO('2020-03-22T00:00:00.000Z').toUTC())
expect(clicks).toEqual([
{ user: 'U12341234', count: 1 },
{ user: 'U99991111', count: 1 }
])
})
})

describe('fastestClickTimes', () => {
Expand All @@ -235,6 +263,9 @@ describe('database', () => {
authedUser: {
id: 'U9'
},
userSettings: {
U1: { statsInterval: 7 }
},
channel: 'C1',
scheduled: {}
}
Expand Down Expand Up @@ -317,6 +348,14 @@ describe('database', () => {
])
})

test('is sorted and correct when user has limited stats', async () => {
const clicks = await db.fastestClickTimes('T2', 10, 'U1', DateTime.fromISO('2020-03-22T00:00:00.000Z').toUTC())
expect(clicks).toEqual([
{ user: 'U12341234', time: 1000 },
{ user: 'U99991111', time: 4000 }
])
})

test('limits entries correctly', async () => {
const clicks = await db.fastestClickTimes('T2', 1)
expect(clicks).toHaveLength(1)
Expand Down Expand Up @@ -1396,6 +1435,9 @@ describe('database', () => {
authedUser: {
id: 'U9'
},
userSettings: {
U1: { statsInterval: 7 }
},
channel: 'C1',
scheduled: {}
}
Expand Down Expand Up @@ -1478,6 +1520,14 @@ describe('database', () => {
])
})

test('is sorted and correct when user has limited stats', async () => {
const clicks = await db.slowestClickTimes('T2', 10, 'U1', DateTime.fromISO('2020-03-22T00:00:00.000Z').toUTC())
expect(clicks).toEqual([
{ user: 'U99991111', time: 4000 },
{ user: 'U12341234', time: 1000 }
])
})

test('limits entries correctly', async () => {
const clicks = await db.slowestClickTimes('T2', 1)
expect(clicks).toHaveLength(1)
Expand Down Expand Up @@ -1580,6 +1630,164 @@ describe('database', () => {
})
})

describe('userSettings', () => {
beforeEach(async () => {
const collection = await db._instanceCollection()
await collection.deleteMany({})

// For each of these tests, initialize the db with some contents.
const instanceCommon = {
accessToken: 'xoxop-134234234',
manualAnnounce: false,
weekdays: 0,
intervalStart: 32400,
intervalEnd: 57600,
timezone: 'Europe/Copenhagen',
scope: 'chat:write',
botUserId: 'U8',
appId: 'A1',
authedUser: {
id: 'U9'
},
channel: 'C1',
scheduled: {}
}

const instance1 = {
...instanceCommon,
team: {
id: 'T1',
name: 'Team1'
},
userSettings: {
U1234: {
statsInterval: 365
}
}
}

const instance2 = {
...instanceCommon,
team: {
id: 'T2',
name: 'Team2'
}
}

await collection.insertMany([instance1, instance2])
})

test('is empty when no user settings exists', async () => {
const clicks = await db.userSettings('T2', 'U1234')
expect(clicks).toEqual({})
})

test('is empty when user does not exists', async () => {
const clicks = await db.userSettings('T1', 'U0000')
expect(clicks).toEqual({})
})

test('is not empty when user exists', async () => {
const clicks = await db.userSettings('T1', 'U1234')
expect(clicks).toEqual({ statsInterval: 365 })
})
})

describe('setUserSetting', () => {
beforeEach(async () => {
const collection = await db._instanceCollection()
await collection.deleteMany({})

// For each of these tests, initialize the db with some contents.
const sharedProperties = {
accessToken: 'xoxop-134234234',
manualAnnounce: false,
weekdays: 0,
intervalStart: 32400,
intervalEnd: 57600,
timezone: 'Europe/Copenhagen',
scope: 'chat:write',
botUserId: 'U8',
appId: 'A1',
authedUser: {
id: 'U9'
},
buttons: []
}

await collection.insertMany([{
...sharedProperties,
team: {
id: 'T1',
name: 'Team1'
},
userSettings: {
U1: {},
U2: {
statsInterval: 1
}
},
channel: null,
scheduled: {}
}, {
...sharedProperties,
team: {
id: 'T2',
name: 'Team1'
},
// missing userSettings completely
channel: null,
scheduled: {}
}])
})

test('stores setting when userSettings does not exists', async () => {
await db.setUserSetting('T2', 'U1337', 'statsInterval', 365)
const collection = await db._instanceCollection()
const instance = await collection.findOne({
'team.id': 'T2'
})

expect(instance.userSettings).toEqual({
U1337: {
statsInterval: 365
}
})
})

test('stores setting when user does not exists', async () => {
await db.setUserSetting('T1', 'U3', 'statsInterval', 365)
const collection = await db._instanceCollection()
const instance = await collection.findOne({
'team.id': 'T1'
})

expect(instance.userSettings).toHaveProperty('U3', { statsInterval: 365 }) // New
expect(instance.userSettings).toHaveProperty('U2', { statsInterval: 1 }) // old
})

test('stores setting when user exists but does not have setting', async () => {
await db.setUserSetting('T1', 'U1', 'statsInterval', 365)
const collection = await db._instanceCollection()
const instance = await collection.findOne({
'team.id': 'T1'
})

expect(instance.userSettings).toHaveProperty('U1', { statsInterval: 365 }) // New
expect(instance.userSettings).toHaveProperty('U2', { statsInterval: 1 }) // old
})

test('stores setting when user exists and has setting', async () => {
await db.setUserSetting('T1', 'U2', 'statsInterval', 365)
const collection = await db._instanceCollection()
const instance = await collection.findOne({
'team.id': 'T1'
})

expect(instance.userSettings).toHaveProperty('U2', { statsInterval: 365 }) // New
})
})

describe('winningStreaks', () => {
beforeEach(async () => {
const collection = await db._instanceCollection()
Expand All @@ -1599,6 +1807,9 @@ describe('database', () => {
authedUser: {
id: 'U9'
},
userSettings: {
U1: { statsInterval: 7 }
},
channel: 'C1',
scheduled: {}
}
Expand Down Expand Up @@ -1705,6 +1916,15 @@ describe('database', () => {
])
})

test('is sorted and correct when stats interval is limited', async () => {
const clicks = await db.winningStreaks('T2', 10, 'U1', DateTime.fromISO('2020-03-22T00:00:00.000Z').toUTC())
expect(clicks).toEqual([
{ user: 'U12341234', streak: 3 },
{ user: 'U12341234', streak: 1 },
{ user: 'U99991111', streak: 1 }
])
})

test('limits entries correctly', async () => {
const clicks = await db.winningStreaks('T2', 1)
expect(clicks).toHaveLength(1)
Expand Down
26 changes: 26 additions & 0 deletions __tests__/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,30 @@ describe('weekday setting', () => {
expect(db.setWeekdays.mock.calls[0][1]).toBe(0b0000000)
expect(res.send).toHaveBeenCalledAfter(db.setWeekdays)
})

test('can set stats interval correctly', async () => {
const instanceRef = 'T1234'
const userRef = 'U1'
const action = { // the real object has the complete plain text and stuff for the whole view too.
type: 'static_select',
action_id: 'user_stats_interval',
selected_option: {
text: {
type: 'plain_text',
text: 'Forever',
emoji: true
},
value: '0'
}
}
const res = { send: jest.fn() }

await settings.setUserSetting(res, instanceRef, action, userRef, 'statsInterval')
expect(db.setUserSetting).toHaveBeenCalledTimes(1)
expect(db.setUserSetting.mock.calls[0][0]).toBe(instanceRef)
expect(db.setUserSetting.mock.calls[0][1]).toBe(userRef)
expect(db.setUserSetting.mock.calls[0][2]).toBe('statsInterval')
expect(db.setUserSetting.mock.calls[0][3]).toBe(0)
expect(res.send).toHaveBeenCalledAfter(db.setUserSetting)
})
})
10 changes: 10 additions & 0 deletions __tests__/stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ describe('stats blocks', () => {
const stats = await statsBlocks('T1')
expect(stats[1].fields[1].text).toMatch(/3 <@test1>/)
})

test('does not includes stats interval if user id is not passed', async () => {
const stats = await statsBlocks('T1')
expect(stats).toHaveLength(3)
})

test('includes default Forever stats interval if nothing else is set', async () => {
const stats = await statsBlocks('T1', 'U1234')
expect(stats).toHaveLength(4)
})
})
Loading