|
| 1 | +// Integration proof: real saveMediaStream + mocked LINE client shows filename-based audio detection. |
| 2 | +import { mkdtempSync, rmSync, existsSync } from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const getMessageContentMock = vi.hoisted(() => vi.fn()); |
| 8 | + |
| 9 | +vi.mock("@line/bot-sdk", () => ({ |
| 10 | + messagingApi: { |
| 11 | + MessagingApiBlobClient: class { |
| 12 | + getMessageContent(messageId: string) { |
| 13 | + return getMessageContentMock(messageId); |
| 14 | + } |
| 15 | + }, |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("openclaw/plugin-sdk/runtime-env", () => ({ |
| 20 | + createSubsystemLogger: () => ({ |
| 21 | + debug: () => {}, |
| 22 | + info: () => {}, |
| 23 | + warn: () => {}, |
| 24 | + error: () => {}, |
| 25 | + child: () => ({ debug: () => {}, info: () => {}, warn: () => {}, error: () => {} }), |
| 26 | + }), |
| 27 | + logVerbose: () => {}, |
| 28 | +})); |
| 29 | + |
| 30 | +let tmpHome: string; |
| 31 | +let originalHome: string | undefined; |
| 32 | +let downloadLineMedia: typeof import("./download.js").downloadLineMedia; |
| 33 | + |
| 34 | +async function* chunks(parts: Buffer[]): AsyncGenerator<Buffer> { |
| 35 | + for (const part of parts) { |
| 36 | + yield part; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +describe("downloadLineMedia integration proof", () => { |
| 41 | + beforeAll(async () => { |
| 42 | + originalHome = process.env.OPENCLAW_HOME; |
| 43 | + tmpHome = mkdtempSync(path.join(os.tmpdir(), "oc-line-proof-")); |
| 44 | + process.env.OPENCLAW_HOME = tmpHome; |
| 45 | + ({ downloadLineMedia } = await import("./download.js")); |
| 46 | + }); |
| 47 | + |
| 48 | + afterAll(() => { |
| 49 | + if (originalHome === undefined) { |
| 50 | + delete process.env.OPENCLAW_HOME; |
| 51 | + } else { |
| 52 | + process.env.OPENCLAW_HOME = originalHome; |
| 53 | + } |
| 54 | + rmSync(tmpHome, { recursive: true, force: true }); |
| 55 | + }); |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + vi.restoreAllMocks(); |
| 59 | + getMessageContentMock.mockReset(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("saves a LINE file-upload audio attachment with audio/mpeg MIME using the filename hint", async () => { |
| 63 | + // No audio magic bytes; only the .mp3 filename hint can classify this as audio. |
| 64 | + const data = Buffer.from([0x00, 0x01, 0x02, 0x03]); |
| 65 | + getMessageContentMock.mockResolvedValueOnce(chunks([data])); |
| 66 | + |
| 67 | + const result = await downloadLineMedia("mid-file", "token", 10 * 1024 * 1024, "voice.mp3"); |
| 68 | + |
| 69 | + expect(result.size).toBe(data.length); |
| 70 | + expect(result.contentType).toBe("audio/mpeg"); |
| 71 | + expect(path.extname(result.path)).toBe(".mp3"); |
| 72 | + expect(existsSync(result.path)).toBe(true); |
| 73 | + }); |
| 74 | +}); |
0 commit comments