84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
import { describe, it, expect } from "bun:test"
|
|
|
|
import {
|
|
buildAudioFileName,
|
|
normalizeMimeType,
|
|
pickSupportedMimeType,
|
|
} from "@/components/chat/audio-recorder-utils"
|
|
import {
|
|
extractPeaks,
|
|
formatAttachmentSize,
|
|
formatDuration,
|
|
isAudioAttachment,
|
|
isImageAttachment,
|
|
} from "@/components/chat/chat-attachment-utils"
|
|
|
|
describe("audio-recorder-utils", () => {
|
|
it("normaliza o mimeType removendo parametros", () => {
|
|
expect(normalizeMimeType("audio/ogg;codecs=opus")).toBe("audio/ogg")
|
|
})
|
|
|
|
it("seleciona o primeiro mime suportado", () => {
|
|
const supported = new Set(["audio/ogg", "audio/mp4"])
|
|
const result = pickSupportedMimeType((mimeType) => supported.has(mimeType))
|
|
expect(result).toBe("audio/ogg")
|
|
})
|
|
|
|
it("retorna vazio quando nenhum mime for suportado", () => {
|
|
const result = pickSupportedMimeType(() => false)
|
|
expect(result).toBe("")
|
|
})
|
|
|
|
it("gera nome de arquivo com extensao correta", () => {
|
|
const fixedDate = new Date("2025-01-01T12:34:56.789Z")
|
|
const fileName = buildAudioFileName("audio/mpeg", fixedDate)
|
|
expect(fileName).toBe("audio-2025-01-01T12-34-56-789Z.mp3")
|
|
})
|
|
|
|
it("usa webm como fallback para mimeType desconhecido", () => {
|
|
const fixedDate = new Date("2025-01-01T12:34:56.789Z")
|
|
const fileName = buildAudioFileName("audio/unknown", fixedDate)
|
|
expect(fileName).toBe("audio-2025-01-01T12-34-56-789Z.webm")
|
|
})
|
|
})
|
|
|
|
describe("chat-attachment-utils", () => {
|
|
it("formata tamanhos de arquivo em bytes, KB e MB", () => {
|
|
expect(formatAttachmentSize(undefined)).toBeNull()
|
|
expect(formatAttachmentSize(512)).toBe("512B")
|
|
expect(formatAttachmentSize(1024)).toBe("1KB")
|
|
expect(formatAttachmentSize(1024 * 1024)).toBe("1.0MB")
|
|
})
|
|
|
|
it("formata duracao em minutos e segundos", () => {
|
|
expect(formatDuration(0)).toBe("00:00")
|
|
expect(formatDuration(5)).toBe("00:05")
|
|
expect(formatDuration(65)).toBe("01:05")
|
|
})
|
|
|
|
it("detecta anexos de audio por type ou extensao", () => {
|
|
expect(isAudioAttachment({ name: "voz.mp3", type: null })).toBe(true)
|
|
expect(isAudioAttachment({ name: "arquivo.dat", type: "audio/ogg" })).toBe(true)
|
|
expect(isAudioAttachment({ name: "foto.png", type: null })).toBe(false)
|
|
})
|
|
|
|
it("detecta anexos de imagem por type ou extensao", () => {
|
|
expect(isImageAttachment({ name: "foto.PNG", type: null })).toBe(true)
|
|
expect(isImageAttachment({ name: "arquivo.txt", type: "image/jpeg" })).toBe(true)
|
|
expect(isImageAttachment({ name: "audio.mp3", type: null })).toBe(false)
|
|
})
|
|
|
|
it("extrai picos normalizados com quantidade de barras fixa", () => {
|
|
const buffer = {
|
|
getChannelData: () => new Float32Array([0, 0.5, 0.2, -0.8, 0.1, -0.1, 0.9, 0.3]),
|
|
} as unknown as AudioBuffer
|
|
|
|
const peaks = extractPeaks(buffer, 4)
|
|
expect(peaks).toHaveLength(4)
|
|
expect(Math.max(...peaks)).toBeCloseTo(1, 5)
|
|
for (const value of peaks) {
|
|
expect(value).toBeGreaterThanOrEqual(0)
|
|
expect(value).toBeLessThanOrEqual(1)
|
|
}
|
|
})
|
|
})
|