Files
mcp-home/tests/testdata/gen_testfiles.py
T
Stefan Lohmaier 5f3181b162 test: umfangreiche Testdaten (alle Dateitypen + Mail-Anhaenge)
- oCIS /testdata/: Bilder (jpg/png/webp/bmp/gif/tiff/svg), Audio (mp3/ogg/m4a/
  flac/wav), Video (mp4), PDFs (Text + Scan), Office (docx/xlsx/pptx), Text/Daten
  (md/txt/csv/json/xml/yaml/html/py/vcf/ics), Archive (zip/tar.gz).
- Maildir: Mails mit diversen Anhaengen (PDF/Bild/Word/Excel/MP3/MP4/PPTX/ZIP).
- TestFileTypes: read_file je Typ -> Content-Typ pruefen (image/text/resource).
- Generatoren tests/testdata/gen_testfiles.py + gen_maildir.py + upload_ocis.sh.
68 Tests gruen (vorher 54). Doku in MCPTEST.md.
2026-06-19 07:56:08 +02:00

76 lines
4.2 KiB
Python

import os, subprocess, zipfile, tarfile, json, wave, struct, math
from PIL import Image, ImageDraw
from fpdf import FPDF
from docx import Document
from openpyxl import Workbook
from pptx import Presentation
from pptx.util import Inches
D="/tmp/mcptest-files"; os.makedirs(D, exist_ok=True)
def p(f): return os.path.join(D,f)
def img(fn, fmt, size=(640,400), color=(70,130,180)):
im=Image.new("RGB",size,color); dr=ImageDraw.Draw(im)
dr.rectangle([20,20,size[0]-20,size[1]-20],outline=(255,255,255),width=4)
dr.text((40,40),f"MCP Test\n{fn}",fill=(255,255,255))
im.save(p(fn),fmt)
# ---- Bilder ----
img("photo.jpg","JPEG",color=(200,120,60))
img("screenshot.png","PNG")
img("picture.webp","WEBP",color=(60,160,120))
img("icon.bmp","BMP",(128,128))
img("scan_src.png","PNG",(800,1000),(245,245,235))
# GIF (animiert)
frames=[Image.new("RGB",(120,120),(i*40%255,80,160)) for i in range(3)]
frames[0].save(p("animation.gif"),save_all=True,append_images=frames[1:],duration=300,loop=0)
# TIFF
img("scan.tiff","TIFF",(600,800),(230,230,230))
# SVG (Text)
open(p("logo.svg"),"w").write('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100"><rect width="200" height="100" fill="#4682b4"/><text x="20" y="55" fill="#fff" font-size="20">MCP Test</text></svg>')
# ---- Audio (ffmpeg) ----
def ff(args): subprocess.run(["ffmpeg","-y","-loglevel","error"]+args,check=True)
ff(["-f","lavfi","-i","sine=frequency=440:duration=2","-q:a","6",p("tone.mp3")])
ff(["-f","lavfi","-i","sine=frequency=660:duration=2",p("music.ogg")])
ff(["-f","lavfi","-i","sine=frequency=330:duration=2",p("sound.m4a")])
ff(["-f","lavfi","-i","sine=frequency=550:duration=2",p("track.flac")])
# WAV (pure python, 1s 440Hz)
w=wave.open(p("voice.wav"),"w"); w.setnchannels(1); w.setsampwidth(2); w.setframerate(8000)
w.writeframes(b"".join(struct.pack("<h",int(8000*math.sin(2*math.pi*440*t/8000))) for t in range(8000))); w.close()
# ---- Video ----
ff(["-f","lavfi","-i","testsrc=duration=2:size=320x240:rate=10","-pix_fmt","yuv420p",p("clip.mp4")])
# ---- PDFs ----
pdf=FPDF(); pdf.add_page(); pdf.set_font("Helvetica",size=14)
pdf.multi_cell(0,8,"MCP Test - Text-PDF\n\nDies ist ein durchsuchbares Text-PDF mit echtem Textinhalt.\nZeile 2: Rechnung Nr. 2026-001\nBetrag: 123,45 EUR")
pdf.output(p("document.pdf"))
# Bild-/Scan-PDF aus PNG
subprocess.run(["convert",p("scan_src.png"),p("scanned.pdf")],check=True)
# ---- Office ----
doc=Document(); doc.add_heading("MCP Test Bericht",0); doc.add_paragraph("Word-Dokument (.docx) mit Absatz.")
doc.add_paragraph("Stichpunkt eins",style="List Bullet"); doc.save(p("report.docx"))
wb=Workbook(); ws=wb.active; ws.title="Budget"; ws.append(["Posten","Betrag"]); ws.append(["Server",42]); ws.append(["Strom",17]); wb.save(p("budget.xlsx"))
prs=Presentation(); s=prs.slides.add_slide(prs.slide_layouts[0]); s.shapes.title.text="MCP Test"; s.placeholders[1].text="PowerPoint (.pptx)"; prs.save(p("slides.pptx"))
# ---- Text/Data ----
open(p("readme.md"),"w").write("# MCP Test\n\nMarkdown-Datei mit **Formatierung** und Liste:\n- a\n- b\n")
open(p("notes.txt"),"w").write("Einfache Textdatei.\nZeile 2.\nUmlaute: aeoeue\n")
open(p("data.csv"),"w").write("name,wert\nAlpha,1\nBeta,2\n")
json.dump({"projekt":"mcptest","aktiv":True,"werte":[1,2,3]},open(p("config.json"),"w"),indent=2)
open(p("feed.xml"),"w").write('<?xml version="1.0"?><rss><channel><title>Test</title></channel></rss>')
open(p("settings.yaml"),"w").write("projekt: mcptest\naktiv: true\nliste:\n - a\n - b\n")
open(p("page.html"),"w").write("<!doctype html><html><body><h1>MCP Test</h1><p>HTML.</p></body></html>")
open(p("script.py"),"w").write("print('mcp test')\n")
open(p("contact.vcf"),"w").write("BEGIN:VCARD\nVERSION:3.0\nFN:Max Mustermann\nEMAIL:max@example.com\nEND:VCARD\n")
open(p("event.ics"),"w").write("BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\nUID:t1\nSUMMARY:Test\nDTSTART:20260701T100000Z\nEND:VEVENT\nEND:VCALENDAR\n")
# ---- Archive ----
with zipfile.ZipFile(p("archive.zip"),"w") as z: z.write(p("notes.txt"),"notes.txt"); z.write(p("data.csv"),"data.csv")
with tarfile.open(p("backup.tar.gz"),"w:gz") as t: t.add(p("readme.md"),"readme.md")
os.remove(p("scan_src.png"))
print("Generiert:")
for f in sorted(os.listdir(D)): print(f" {os.path.getsize(p(f)):>8} {f}")