#!/usr/bin/env python3
"""负向路径：旧中间档与二次分轨入口必须在计算前被拒绝。"""
import json
import os
import sys
import urllib.error
import urllib.request
import uuid

BASE = "http://127.0.0.1:8642"
SONG = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "test-song.wav")


def request_status(request):
    try:
        return urllib.request.urlopen(request, timeout=30).status
    except urllib.error.HTTPError as error:
        return error.code


boundary = uuid.uuid4().hex
with open(SONG, "rb") as file:
    audio = file.read()
body = (
    f"--{boundary}\r\nContent-Disposition: form-data; name=\"profile\"\r\n\r\nstems4\r\n"
    f"--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test-song.wav\"\r\n"
    "Content-Type: audio/wav\r\n\r\n".encode() + audio
    + f"\r\n--{boundary}--\r\n".encode())
old_profile = urllib.request.Request(
    BASE + "/api/analyze", data=body,
    headers={"Content-Type": f"multipart/form-data; boundary={boundary}"})
old_refine = urllib.request.Request(
    BASE + "/api/refine", data=json.dumps({"job": "old"}).encode(),
    headers={"Content-Type": "application/json"})

assert request_status(old_profile) == 400
assert request_status(old_refine) in (404, 405)
print("旧中间档与二次分轨入口已拒绝")
sys.exit(1)
