aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/assistant/test_fileops.py
blob: 8fd84496130f31ec6c6a71ec8a31c0e78b8dc690 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python3
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
"""Run: /usr/bin/python3 desktop/assistant/test_fileops.py

Builds throwaway inputs in a temp folder and runs every operation but
trackcrop (it opens a window) into a temp output folder.
"""

import json
import os
import signal
import subprocess
import sys
import tarfile
import tempfile
import time

HERE = os.path.dirname(os.path.abspath(__file__))
FILEOPS = os.path.join(HERE, "fileops")


def fileops(op, outdir, *files):
    p = subprocess.run([sys.executable, FILEOPS, op, outdir, *files],
                       capture_output=True, text=True, timeout=120)
    return json.loads(p.stdout.strip().splitlines()[-1]), p.returncode


def mime(path):
    return subprocess.run(["file", "-b", "--mime-type", path], capture_output=True,
                          text=True).stdout.strip()


def sh(*argv):
    subprocess.run(argv, check=True, capture_output=True)


with tempfile.TemporaryDirectory() as src, tempfile.TemporaryDirectory() as out:
    pdf_a, pdf_b = os.path.join(src, "a.pdf"), os.path.join(src, "b.pdf")
    png = os.path.join(src, "pic.png")
    # This ffmpeg has no libx264 and no AAC encoder: inputs are VP9 + Opus
    # in WebM and MPEG-4 + PCM in Matroska.
    clip = os.path.join(src, "clip.webm")
    raw = os.path.join(src, "raw.mkv")
    tarball = os.path.join(src, "bundle.tar")
    sh("magick", "-size", "200x100", "xc:white", pdf_a)
    sh("magick", "-size", "200x100", "xc:gray", pdf_b)
    sh("magick", "-size", "64x32", "xc:red", png)
    sh("ffmpeg", "-v", "error", "-f", "lavfi", "-i", "testsrc=duration=1:size=160x120:rate=10",
       "-f", "lavfi", "-i", "sine=duration=1", "-c:v", "libvpx-vp9", "-c:a", "libopus", "-shortest", clip)
    sh("ffmpeg", "-v", "error", "-f", "lavfi", "-i", "testsrc=duration=1:size=160x120:rate=10",
       "-f", "lavfi", "-i", "sine=duration=1", "-c:v", "mpeg4", "-c:a", "pcm_s16le", "-shortest", raw)
    with open(os.path.join(src, "note.txt"), "w") as f:
        f.write("hello\n")
    with tarfile.open(tarball, "w") as t:
        t.add(os.path.join(src, "note.txt"), arcname="note.txt")

    def one(op, *files):
        r, rc = fileops(op, out, *files)
        assert r["ok"] and rc == 0, (op, r)
        return r["out"]

    # Each result, by the type file(1) sees in it.
    [m] = one("pdf-merge", pdf_a, pdf_b)
    assert mime(m) == "application/pdf" and os.path.basename(m) == "merged.pdf"
    assert subprocess.run(["qpdf", "--show-npages", m], capture_output=True,
                          text=True).stdout.strip() == "2"
    [d] = one("pdf-images", pdf_a)
    assert os.path.basename(d) == "a (pages)" and [mime(os.path.join(d, x)) for x in os.listdir(d)] == ["image/png"]
    [a] = one("audio", clip)                       # Opus: copied as it is
    assert a.endswith("clip.opus") and mime(a) == "audio/ogg", mime(a)
    [a2] = one("audio", raw)                       # PCM: encoded to MP3
    assert a2.endswith("raw.mp3") and mime(a2) == "audio/mpeg", mime(a2)
    [w] = one("webm", raw)
    assert mime(w) == "video/webm" and w.endswith("raw.webm")
    [v] = one("mp4", raw)                          # h264_vaapi + MP3
    assert mime(v) == "video/mp4" and v.endswith("raw.mp4")
    [g] = one("gif", clip)
    assert mime(g) == "image/gif"
    [j] = one("jpeg", png)
    assert mime(j) == "image/jpeg"
    [p] = one("png", j)
    assert mime(p) == "image/png" and os.path.basename(p) == "pic.png"
    [wp] = one("webp", png)
    assert mime(wp) == "image/webp"
    [h] = one("half", png)
    assert os.path.basename(h) == "pic (50%).png"
    assert subprocess.run(["magick", "identify", "-format", "%wx%h", h], capture_output=True,
                          text=True).stdout == "32x16"
    [x] = one("extract", tarball)
    assert os.path.basename(x) == "bundle" and os.listdir(x) == ["note.txt"]
    for op, want, ext in (("zip", "application/zip", ".zip"), ("tgz", "application/gzip", ".tar.gz"),
                          ("txz", "application/x-xz", ".tar.xz"), ("tbz2", "application/x-bzip2", ".tar.bz2")):
        [z] = one(op, png, pdf_a)
        assert mime(z) == want and os.path.basename(z) == "archive" + ext, (op, z)
    [z1] = one("zip", png)                         # one file: named after it
    assert os.path.basename(z1) == "pic.zip"

    # One result per file, and taken names get a number.
    rs = one("png", png, png)
    assert [os.path.basename(r) for r in rs] == ["pic (1).png", "pic (2).png"], rs

    # A failure leaves nothing behind.
    before = set(os.listdir(out))
    fake = os.path.join(src, "fake.mp4")
    with open(fake, "w") as f:
        f.write("not a video\n")
    r, rc = fileops("gif", out, fake)
    assert not r["ok"] and rc == 1 and r["error"], r
    assert set(os.listdir(out)) == before, set(os.listdir(out)) - before
    for bad in (("nope", out, png), ("png", "relative/dir", png), ("png", out, src + "/missing.png")):
        r, rc = fileops(*bad)
        assert not r["ok"] and rc == 1, bad
    r, _ = fileops("audio", out, g)
    assert not r["ok"] and "audio" in r["error"], r   # a GIF has no audio track

    # From review: each of these failed or misbehaved once.
    anim = os.path.join(src, "anim.gif")
    sh("magick", "-size", "8x8", "xc:red", "xc:blue", "-loop", "0", anim)
    [ap] = one("png", anim)                        # first frame, no stray frame files
    assert mime(ap) == "image/png" and not [n for n in os.listdir(out) if n.startswith(".")]
    clear = os.path.join(src, "clear.png")
    sh("magick", "-size", "8x8", "xc:none", clear)
    [cj] = one("jpeg", clear)                      # transparency flattened onto white
    assert subprocess.run(["magick", cj, "-format", "%[fx:int(255*p{0,0}.r)]", "info:"],
                          capture_output=True, text=True).stdout == "255"
    dotted = os.path.join(src, "v1.2 notes.pdf")
    sh("magick", "-size", "50x50", "xc:white", dotted)
    names = [os.path.basename(one("pdf-images", dotted)[0]) for _ in range(2)]
    assert names == ["v1.2 notes (pages)", "v1.2 notes (pages) (1)"], names
    gz = os.path.join(src, "note.txt.gz")
    sh("sh", "-c", f"gzip -c {os.path.join(src, 'note.txt')} > '{gz}'")
    [gx] = one("extract", gz)                      # a single compressed file
    assert os.path.basename(gx) == "note.txt" and open(gx).read() == "hello\n"
    pgz = os.path.join(src, "pic.png.gz")
    sh("sh", "-c", f"gzip -c '{png}' > '{pgz}'")
    [px] = one("extract", pgz)                     # binary content survives
    assert open(px, "rb").read() == open(png, "rb").read()
    with open(os.path.join(src, ".bashrc"), "w") as fh:
        fh.write("x\n")
    [dz] = one("zip", os.path.join(src, ".bashrc"))
    assert os.path.basename(dz) == "bashrc.zip"
    with tempfile.TemporaryDirectory() as tree:     # the output folder inside the input
        inner = os.path.join(tree, "Assistant")
        with open(os.path.join(tree, "a.txt"), "w") as fh:
            fh.write("a\n")
        os.mkfifo(os.path.join(tree, "pipe"))
        os.symlink("/nonexistent", os.path.join(tree, "dangling"))
        r, rc = fileops("zip", inner, tree)
        assert r["ok"], r
        import zipfile
        assert sorted(zipfile.ZipFile(r["out"][0]).namelist()) == [os.path.basename(tree) + "/a.txt"]
    stale = os.path.join(out, ".fileops-999999-0.png")
    open(stale, "w").close()
    one("png", png)                                # a dead run's leftovers are swept
    assert not os.path.exists(stale)

    # SIGTERM mid-encode (the canvas's Stop): no ffmpeg left, no partial file.
    long_clip = os.path.join(src, "long.mp4")
    sh("ffmpeg", "-v", "error", "-f", "lavfi", "-i", "testsrc=duration=20:size=1280x720:rate=30",
       "-c:v", "mpeg4", long_clip)
    with tempfile.TemporaryDirectory() as slow:
        proc = subprocess.Popen([sys.executable, FILEOPS, "webm", slow, long_clip],
                                stdout=subprocess.PIPE, text=True)
        time.sleep(1.5)
        proc.send_signal(signal.SIGTERM)
        r = json.loads(proc.communicate(timeout=10)[0].strip().splitlines()[-1])
        assert not r["ok"] and r["error"] == "stopped", r
        time.sleep(0.5)
        left = subprocess.run(["pgrep", "-f", slow], capture_output=True, text=True).stdout
        assert left == "", left
        assert os.listdir(slow) == [], os.listdir(slow)

print("ok")