-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.spec.ts
139 lines (124 loc) · 3.24 KB
/
analysis.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// deno-lint-ignore-file no-explicit-any
import {
afterEach,
assertEquals,
assertType,
beforeEach,
describe,
faker,
IsExact,
it,
restore,
returnsNext,
simpleFaker,
stub,
} from '@/dev_deps.ts'
import { bulkRetrieveItems, filterStories } from '@/analysis.ts'
import { Item, TopStories } from '@/types.ts'
import { R } from '@/deps.ts'
import { promisifyFactoryObj } from '@/utils/test_helpers.ts'
import * as F from '@/functions.ts'
describe('filterStories', () => {
beforeEach(() => {
// Mock the getTopStories function
const fakeTopStories = simpleFaker.helpers.multiple(
simpleFaker.number.int,
{
count: 3,
},
)
const fakeStoryTitles = [
'Watch out this new AWS Service: Deno on TypeScript',
'Deno vs Node: A Comprehensive Comparison',
'My journey to become a lazy developer',
]
// Mock getItem function
const fakeItems: Item[] = F.mapIndexed(
(id, idx: number) => ({
id,
type: 'story',
by: faker.person.fullName(),
title: fakeStoryTitles[idx],
time: faker.date.recent().getTime(),
}),
fakeTopStories,
)
const fakeGeminiResponse = {
candidates: [
{
content: {
parts: [
{
text: R.pipe(
R.take(2),
R.map(R.toString),
R.assoc('storyIds', R.__, {}),
(v: any) => JSON.stringify(v),
)(fakeTopStories),
},
],
},
safetyRatings: [],
},
],
}
stub(
globalThis,
'fetch',
returnsNext([
promisifyFactoryObj<TopStories>(fakeTopStories),
...R.map(promisifyFactoryObj<Item>, fakeItems),
promisifyFactoryObj(fakeGeminiResponse),
]),
)
})
afterEach(() => {
restore()
})
it('does nothing when no preferences are provided', async () => {
const preferences: string[] = []
const result = await filterStories(preferences)
assertEquals(result, [])
})
it('filters stories by preferences', async () => {
const preferences = ['TypeScript', 'AWS', 'Deno']
const filteredStories = await filterStories(preferences)
assertEquals(
filteredStories,
[
'Watch out this new AWS Service: Deno on TypeScript',
'Deno vs Node: A Comprehensive Comparison',
],
)
})
})
describe('bulkRetrieveItems', () => {
describe('no items', () => {
it('returns an empty array', async () => {
assertEquals(await bulkRetrieveItems([]), [])
})
})
it('retrieves items in bulk', async () => {
const fakeItems = R.times(
(id: number) => ({
id,
type: 'story',
by: faker.person.fullName(),
title: faker.lorem.sentence(),
time: faker.date.recent().getTime(),
} as Item),
3,
)
using _fetchStub = stub(
globalThis,
'fetch',
returnsNext(R.map(promisifyFactoryObj<Item>, fakeItems)),
)
const ids = [1, 2, 3]
const result = await bulkRetrieveItems(ids)
assertType<IsExact<typeof result, Item[]>>(true)
assertEquals(result.length, ids.length)
assertEquals(result, fakeItems)
})
// TODO: add error handling tests
})