summaryrefslogtreecommitdiff
path: root/tests/test_storage.py
blob: 389f1991f00f68ae14890b4deb9c7eb772a1b4e2 (plain)
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
import os
import subprocess
from datetime import datetime, timezone

import numpy as np

from mic_clipper.storage import ClipWriter


def test_writer_creates_private_opus_clip_in_segment_start_date(tmp_path):
    started_at = datetime(2026, 9, 29, 23, 59, 59, tzinfo=timezone.utc)
    samples = np.sin(np.linspace(0, 100, 16_000, dtype=np.float32))
    writer = ClipWriter(tmp_path)

    clip = writer.open(started_at)
    clip.write(samples)
    clip.close()

    destination = tmp_path / "2026-09-29"
    files = list(destination.glob("*.opus"))
    assert len(files) == 1
    assert files[0].name.startswith("23-59-59")
    assert os.stat(destination).st_mode & 0o777 == 0o700
    assert os.stat(files[0]).st_mode & 0o777 == 0o600
    assert files[0].read_bytes()[:4] == b"OggS"
    assert b"OpusHead" in files[0].read_bytes()[:128]

    decoded = subprocess.run(
        ["ffmpeg", "-v", "error", "-i", str(files[0]), "-f", "f32le", "pipe:1"],
        check=True,
        capture_output=True,
    )
    assert len(decoded.stdout) > 0