Rocky_Mountain_Vending/lib/manuals-knowledge.test.ts

79 lines
2.7 KiB
TypeScript

import assert from "node:assert/strict"
import test from "node:test"
import {
findManualCandidates,
getManualCitationContext,
resetManualKnowledgeCache,
retrieveManualContext,
shouldUseManualKnowledgeForChat,
} from "@/lib/manuals-knowledge"
test("shouldUseManualKnowledgeForChat only triggers for relevant conversations", () => {
assert.equal(
shouldUseManualKnowledgeForChat(
"Repairs",
"My Royal machine is not accepting coins"
),
true
)
assert.equal(shouldUseManualKnowledgeForChat("Other", "Hello there"), false)
})
test("findManualCandidates resolves RVV alias queries to Royal Vendors manuals", async () => {
const candidates = await findManualCandidates("RVV 660 service manual")
assert.ok(candidates.length > 0)
assert.equal(candidates[0]?.manufacturer, "Royal Vendors")
assert.match(candidates[0]?.filename || "", /660|700|gii|giii|rvv/i)
})
test("findManualCandidates resolves Narco-style queries to Dixie-Narco manuals", async () => {
const candidates = await findManualCandidates("Narco bevmax not cooling")
assert.ok(candidates.length > 0)
assert.equal(candidates[0]?.manufacturer, "Dixie-Narco")
})
test("retrieveManualContext returns grounded troubleshooting chunks for simple public help", async () => {
const result = await retrieveManualContext("Royal machine not accepting coins")
assert.ok(result.manualCandidates.length > 0)
assert.equal(result.topChunks.length > 0, true)
assert.equal(result.topChunks[0]?.manufacturer, "Royal Vendors")
assert.match(result.topChunks[0]?.text || "", /not accepting coins/i)
assert.equal(result.isRisky, false)
})
test("getManualCitationContext returns citations for a retrieved manual page", async () => {
const result = await retrieveManualContext("Royal machine not accepting coins")
const firstChunk = result.topChunks[0]
assert.ok(firstChunk)
const citationContext = await getManualCitationContext(
firstChunk.manualId,
firstChunk.pageNumber || undefined
)
assert.ok(citationContext.manual)
assert.ok(citationContext.citations.length > 0)
assert.equal(
citationContext.citations.some(
(citation) => citation.pageNumber === firstChunk.pageNumber
),
true
)
})
test("resetManualKnowledgeCache rebuilds the manuals corpus on demand", async () => {
const beforeReset = await findManualCandidates("RVV 660 service manual")
resetManualKnowledgeCache()
const afterReset = await findManualCandidates("RVV 660 service manual")
assert.ok(beforeReset.length > 0)
assert.ok(afterReset.length > 0)
assert.equal(beforeReset[0]?.manufacturer, afterReset[0]?.manufacturer)
assert.equal(beforeReset[0]?.manualId, afterReset[0]?.manualId)
})