-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 wrote new set of unit tests and using UTC where needed
- Loading branch information
Showing
9 changed files
with
81 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,89 @@ | ||
import { ONE_DAY_IN_MS, ttlExpired } from './ttl'; | ||
import { ONE_DAY_IN_MS, ttlExpired, UTCDate } from './ttl'; | ||
|
||
describe('ttlExpired', () => { | ||
test('same time - use createdAt when posts are null', () => { | ||
test('just created profile', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const daglig = { | ||
createdAt: now, | ||
posts: null, | ||
posts: [], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
const { expired, ttl } = ttlExpired(daglig, now); | ||
expect(expired).toBe(false); | ||
expect(ttl > 0).toBe(true); | ||
expect(ttl).toBe(14 * 60 * 60 * 1000); // 14h left of the current day | ||
}); | ||
|
||
test('10h old - use createdAt when posts are null', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setHours(createdAt.getHours() - 10); | ||
const daglig = { | ||
createdAt, | ||
posts: null, | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
expect(expired).toBe(false); | ||
expect(ttl).toBe(14 * 60 * 60 * 1000); | ||
}); | ||
|
||
test('12h old - use createdAt when posts are null', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setHours(createdAt.getHours() - 12); | ||
test('created profile but wrote no post till next day', () => { | ||
const createdAt = new Date(1987, 10, 1, 10); | ||
const daglig = { | ||
createdAt, | ||
posts: null, | ||
posts: [], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
const { expired, ttl } = ttlExpired(daglig, new Date(1987, 10, 2)); | ||
expect(expired).toBe(true); | ||
expect(ttl).toBe(0); | ||
}); | ||
|
||
test('1d old - use createdAt when posts are null', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setDate(createdAt.getDate() - 1); | ||
test('created profile but wrote no post till the end of current day', () => { | ||
const createdAt = new Date(1987, 10, 1, 10); | ||
const daglig = { | ||
createdAt, | ||
posts: null, | ||
posts: [], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
expect(expired).toBe(true); | ||
expect(ttl).toBe(0); | ||
}); | ||
|
||
test('2d old - use createdAt when posts are null', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setDate(createdAt.getDate() - 2); | ||
const daglig = { | ||
createdAt, | ||
posts: null, | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
expect(expired).toBe(true); | ||
expect(ttl).toBe(0); | ||
}); | ||
|
||
test('same time - use createdAt of oldest post', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const daglig = { | ||
createdAt: now, | ||
posts: [ | ||
{ createdAt: new Date(1987, 10, 1, 9), likes: [] }, | ||
{ createdAt: now, likes: [] }, | ||
], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
const { expired, ttl } = ttlExpired(daglig, new Date(1987, 10, 1, 23, 59, 59, 999)); | ||
expect(expired).toBe(false); | ||
expect(ttl > 0).toBe(true); | ||
expect(ttl).toBe(1); | ||
}); | ||
|
||
test('10h old - use createdAt of oldest post', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setHours(createdAt.getHours() - 10); | ||
test('just created profile and directly wrote first post', () => { | ||
const createdAt = new Date(1987, 10, 1, 10); | ||
const daglig = { | ||
createdAt, | ||
posts: [ | ||
{ createdAt: new Date(1987, 10, 1, 9), likes: [] }, | ||
{ createdAt, likes: [] }, | ||
], | ||
posts: [{ | ||
createdAt, | ||
likes: [] | ||
}], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
const { expired, ttl } = ttlExpired(daglig, createdAt); | ||
expect(expired).toBe(false); | ||
expect(ttl).toBe(14 * 60 * 60 * 1000 + ONE_DAY_IN_MS); | ||
}); | ||
|
||
test('12h old - use createdAt of oldest post', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setHours(createdAt.getHours() - 12); | ||
test('wrote post yesterday', () => { | ||
const daglig = { | ||
createdAt, | ||
posts: [ | ||
{ createdAt: new Date(1987, 10, 1, 9), likes: [] }, | ||
{ createdAt, likes: [] }, | ||
], | ||
createdAt: new Date(1987, 10, 1), | ||
posts: [{ | ||
createdAt: new Date(1987, 10, 2), | ||
likes: [] | ||
}], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
expect(expired).toBe(true); | ||
expect(ttl).toBe(0); | ||
const { expired, ttl } = ttlExpired(daglig, new Date(1987, 10, 3)); | ||
expect(expired).toBe(false); | ||
expect(ttl).toBe(ONE_DAY_IN_MS); | ||
}); | ||
|
||
test('1d old - use createdAt of oldest post', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setDate(createdAt.getDate() - 1); | ||
test('wrote post yesterday and very close to be deleted', () => { | ||
const daglig = { | ||
createdAt, | ||
posts: [ | ||
{ createdAt: new Date(1987, 10, 1, 9), likes: [] }, | ||
{ createdAt, likes: [] }, | ||
], | ||
createdAt: new Date(1987, 10, 1), | ||
posts: [{ | ||
createdAt: new Date(1987, 10, 2), | ||
likes: [] | ||
}], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
expect(expired).toBe(true); | ||
expect(ttl).toBe(0); | ||
const { expired, ttl } = ttlExpired(daglig, new Date(1987, 10, 3, 23, 59, 59, 999)); | ||
expect(expired).toBe(false); | ||
expect(ttl).toBe(1); | ||
}); | ||
|
||
test('2d old - use createdAt of oldest post', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
createdAt.setDate(createdAt.getDate() - 2); | ||
test('wrote post two days before', () => { | ||
const daglig = { | ||
createdAt, | ||
posts: [ | ||
{ createdAt: new Date(1987, 10, 1, 9), likes: [] }, | ||
{ createdAt, likes: [] }, | ||
], | ||
createdAt: new Date(1987, 10, 1), | ||
posts: [{ | ||
createdAt: new Date(1987, 10, 2), | ||
likes: [] | ||
}], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
const { expired, ttl } = ttlExpired(daglig, new Date(1987, 10, 4)); | ||
expect(expired).toBe(true); | ||
expect(ttl).toBe(0); | ||
}); | ||
|
||
test('3 extra minutes because of 3 likes', () => { | ||
const now = new Date(1987, 10, 1, 10); | ||
const createdAt = new Date(now); | ||
const daglig = { | ||
createdAt, | ||
posts: [ | ||
{ createdAt: new Date(1987, 10, 1, 9), likes: [] }, | ||
{ createdAt, likes: ['foo', 'bar', 'baz'] }, | ||
], | ||
}; | ||
const { expired, ttl } = ttlExpired(now, daglig); | ||
expect(expired).toBe(false); | ||
expect(ttl).toBe(50580000 + ONE_DAY_IN_MS); // 14h and 3min | ||
}); | ||
}); |