Skip to main content
Glama
by microsoft
filecache.test.ts2.84 kB
import assert from "node:assert/strict" import test, { beforeEach, describe } from "node:test" import { dirname, join } from "node:path" import { stat, readdir, rm } from "fs/promises" import { existsSync } from "fs" import { fileCacheImage, fileWriteCached, fileWriteCachedJSON, patchCachedImages, } from "./filecache" import { TestHost } from "./testhost" import { readFile } from "node:fs/promises" describe("fileWriteCached", () => { const tempDir = join(dirname(__filename), "temp") beforeEach(async () => { TestHost.install() if (existsSync(tempDir)) { await rm(tempDir, { recursive: true, force: true }) } }) test("should write buffer to cache and return correct filename", async () => { const buffer: BufferLike = Buffer.from("test content") const filePath = await fileWriteCached(tempDir, buffer) const files = await readdir(tempDir) assert.equal(files.length, 1) const writtenFile = join(tempDir, files[0]) const stats = await stat(writtenFile) assert(stats.isFile()) assert.equal(filePath, writtenFile) }) test("should write JSON to cache and return correct filename", async () => { const testData = { test: "content" } const filePath = await fileWriteCachedJSON(tempDir, testData) const files = await readdir(tempDir) assert.equal(files.length, 1) const writtenFile = join(tempDir, files[0]) const stats = await stat(writtenFile) assert(stats.isFile()) assert.equal(filePath, writtenFile) const content = JSON.parse(await readFile(writtenFile, "utf-8")) assert.deepEqual(content, testData) }) test("fileCacheImage should return empty string for falsy input", async () => { assert.equal(await fileCacheImage(""), "") assert.equal(await fileCacheImage(null), "") assert.equal(await fileCacheImage(undefined), "") }) test("fileCacheImage should return URL unchanged when input is HTTPS URL", async () => { const url = "https://example.com/image.jpg" assert.equal(await fileCacheImage(url), url) }) test("fileCacheImage should cache local image and return relative path", async () => { const imageBuffer = Buffer.from("fake image data") const result = await fileCacheImage(imageBuffer, { dir: tempDir }) assert(result.startsWith("./")) const files = await readdir(tempDir) assert.equal(files.length, 1) }) test("patchCachedImages should replace image paths", () => { const input = "![alt](.genaiscript/images/test.jpg)" const output = patchCachedImages(input, (url) => "newpath/" + url) assert.equal(output, "![alt](newpath/.genaiscript/images/test.jpg)") }) })

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/microsoft/genaiscript'

If you have feedback or need assistance with the MCP directory API, please join our Discord server