forked from oramasearch/orama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.ts
135 lines (113 loc) · 4.7 KB
/
integration.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
import { create, load, search } from '@orama/orama'
import assert from 'node:assert'
import { exec, ExecException } from 'node:child_process'
import { existsSync } from 'node:fs'
import { cp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { dirname, resolve } from 'node:path'
import { chdir } from 'node:process'
import { test } from 'node:test'
import { fileURLToPath } from 'node:url'
import { gunzipSync } from 'node:zlib'
import { INDEX_FILE, schema } from '../src/server/types.js'
interface Execution {
code: number
stdout: string
stderr: string
error?: Error
}
const sandboxSource = fileURLToPath(new URL('./sandbox', import.meta.url))
const sandbox = process.env.KEEP_SANDBOX_DOCUSAURUS
? '/tmp/orama-docusaurus-sandbox'
: resolve(tmpdir(), `orama-plugin-docusaurus-${Date.now()}`)
async function cleanup(): Promise<void> {
await rm(sandbox, { force: true, recursive: true })
}
async function execute(command: string, cwd?: string): Promise<Execution> {
const { HOME, PATH } = process.env
const env = cwd ? { HOME, PATH } : process.env
return new Promise((resolve: (execution: Execution) => void, reject: (error: Error) => void) => {
exec(command, { cwd, env }, (error: ExecException | null, stdout: string, stderr: string) => {
if (error) {
if (process.env.NODE_DEBUG?.includes('test')) {
console.error('COMMAND ERRORED', error)
}
reject(error)
return
}
if (process.env.NODE_DEBUG?.includes('test')) {
console.error(`--- STDOUT [${command}] ---\n${stdout.trim()}\n\n`)
console.error(`--- STDERR [${command}] ---\n${stderr.trim()}\n\n`)
}
resolve({ code: 0, stdout, stderr })
})
})
}
await cleanup()
await test('plugin is able to generate orama DB at build time', async () => {
// Obtain general information
const pluginInfo: Record<string, string> = JSON.parse(
await readFile(fileURLToPath(new URL('../package.json', import.meta.url)), 'utf-8')
)
const rootDir = dirname(fileURLToPath(new URL(`../..`, import.meta.url)))
const version = pluginInfo.version
// Prepare the sandbox
const packageJsonPath = resolve(sandbox, 'package.json')
await cp(sandboxSource, sandbox, { recursive: true })
chdir(sandbox)
console.log(`Sandbox created in ${sandbox}`)
// Update dependencies location
const packageJson = await readFile(packageJsonPath, 'utf-8')
await writeFile(packageJsonPath, packageJson.replace(/@ROOT@/g, rootDir).replace(/@VERSION@/g, version), 'utf-8')
// Install dependencies
const installResult = await execute('pnpm install', sandbox)
assert.equal(installResult.code, 0)
// docusaurus build is successful
const buildResult = await execute('./node_modules/.bin/docusaurus build', sandbox)
assert.equal(buildResult.code, 0)
// The orama DBs have been generated
assert.ok(existsSync(resolve(sandbox, `build/${INDEX_FILE.replace('@VERSION@', 'current')}`)))
})
await test('generated DBs have indexed pages content', async () => {
// Loading "animals DB"
const rawCompressedData = await readFile(resolve(sandbox, `build/${INDEX_FILE.replace('@VERSION@', 'current')}`))
const rawData = gunzipSync(rawCompressedData).toString('utf-8')
const data = JSON.parse(rawData)
const database = await create({ schema })
await load(database, data)
// Search results seem reasonable
const indexSearchResult = await search(database, {
term: 'index',
properties: ['sectionTitle', 'sectionContent', 'type']
})
assert.ok(indexSearchResult.count === 1)
assert.ok(indexSearchResult.hits[0].document.pageRoute === '/#main')
const catSearchResult = await search(database, {
term: 'cat',
properties: ['sectionTitle', 'sectionContent', 'type']
})
assert.ok(catSearchResult.count === 1)
assert.ok(catSearchResult.hits[0].document.pageRoute === '/animals_cat')
const dogSearchResult = await search(database, {
term: 'dog',
properties: ['sectionTitle', 'sectionContent', 'type']
})
assert.ok(dogSearchResult.count === 2)
assert.ok(dogSearchResult.hits[0].document.pageRoute === '/animals_dog#dog')
const domesticSearchResult = await search(database, {
term: 'domestic',
properties: ['sectionTitle', 'sectionContent', 'type']
})
assert.ok(domesticSearchResult.count === 2)
assert.ok(domesticSearchResult.hits[0].document.pageRoute === '/animals_cat')
assert.ok(domesticSearchResult.hits[1].document.pageRoute === '/animals_dog#dog')
// We do not have content about turtles
const turtleSearchResult = await search(database, {
term: 'turtle',
properties: ['sectionTitle', 'sectionContent', 'type']
})
assert.ok(turtleSearchResult.count === 0)
})
if (!process.env.KEEP_SANDBOX_DOCUSAURUS) {
await cleanup()
}