mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
510 lines
23 KiB
Python
510 lines
23 KiB
Python
"""Importing a OneLibrary drive into a Traktor collection.
|
|
|
|
The first operation that puts tracks into a library that never had them, and
|
|
therefore the first that writes an ENTRY Traktor did not write. Two things need
|
|
guarding, and they pull in opposite directions:
|
|
|
|
* **The new entries must carry the prep.** Cues on the pads they were on, the
|
|
beatgrid including its tempo changes, and nothing silently dropped.
|
|
* **The old entries must not move.** The whole save-fidelity guarantee rests on
|
|
every unedited object rendering the bytes it was parsed from, and appending
|
|
to COLLECTION is the first thing that could disturb that. So this asserts on
|
|
rendered bytes as well as on the projection.
|
|
|
|
Runs against a temp copy of the real Traktor collection (gotcha 2: it is real,
|
|
irreplaceable data) and the checked-in OneLibrary fixture drive.
|
|
"""
|
|
import logging
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
import warnings
|
|
from difflib import unified_diff
|
|
from pathlib import Path
|
|
|
|
warnings.filterwarnings("ignore")
|
|
os.environ["KONDUKTOR_DATA_DIR"] = tempfile.mkdtemp(prefix="konduktor-import-appdata-")
|
|
|
|
REAL = Path(__file__).resolve().parents[1] / "collection.nml"
|
|
FIXTURE = Path(__file__).resolve().parent / "fixtures" / "onelibrary"
|
|
|
|
from konduktor.adapters.onelibrary.adapter import OneLibraryAdapter # noqa: E402
|
|
from konduktor.adapters.rekordbox.adapter import RekordboxAdapter # noqa: E402
|
|
from konduktor.adapters.traktor.adapter import TraktorAdapter # noqa: E402
|
|
from konduktor.core.adapter import InvalidCommand, NewTrack, Unsupported # noqa: E402
|
|
from konduktor.core.model import CuePoint, GridMarker, Track, TrackCues # noqa: E402
|
|
|
|
failed = False
|
|
|
|
|
|
def check(label, cond, detail=""):
|
|
global failed
|
|
print(f" [{'PASS' if cond else 'FAIL'}] {label}" + (f" — {detail}" if detail and not cond else ""))
|
|
if not cond:
|
|
failed = True
|
|
|
|
|
|
def _raises(fn, exc) -> bool:
|
|
try:
|
|
fn()
|
|
except exc:
|
|
return True
|
|
except Exception: # noqa: BLE001
|
|
return False
|
|
return False
|
|
|
|
|
|
if not REAL.is_file() or not (FIXTURE / "PIONEER" / "rekordbox" / "exportLibrary.db").is_file():
|
|
print(f"SKIP: need {REAL} and the OneLibrary fixture")
|
|
sys.exit(0)
|
|
|
|
|
|
def fresh_collection() -> tuple[Path, Path, bytes]:
|
|
"""A throwaway copy of the real collection, plus a folder for copied audio."""
|
|
work = Path(tempfile.mkdtemp(prefix="konduktor-import-"))
|
|
nml = work / "collection.nml"
|
|
shutil.copy(REAL, nml)
|
|
music = work / "Imported"
|
|
music.mkdir()
|
|
return nml, music, nml.read_bytes()
|
|
|
|
|
|
def stage(src: OneLibraryAdapter, music: Path) -> list[NewTrack]:
|
|
"""Copy the drive's audio and describe each track as a NewTrack.
|
|
|
|
This is what the import feature will do around the adapter: the copy is the
|
|
caller's job, so a half-finished file copy is never something a library
|
|
write discovers.
|
|
"""
|
|
items = []
|
|
for t in src.tracks:
|
|
target = music / Path(t.filepath).name
|
|
shutil.copy(t.filepath, target)
|
|
items.append(NewTrack(track=t, audio_path=target, cues=src.track_cues(t.id)))
|
|
return items
|
|
|
|
|
|
print("== a drive's tracks land in the collection ==")
|
|
nml, music, original = fresh_collection()
|
|
# As the app opens a device: read-only (a drive opened as THE library is editable).
|
|
source = OneLibraryAdapter(FIXTURE, read_only=True)
|
|
dest = TraktorAdapter(nml)
|
|
before = len(dest.tracks)
|
|
items = stage(source, music)
|
|
ids = dest.add_tracks(items)
|
|
|
|
check("every staged track was added", len(ids) == len(items) == 2, str(len(ids)))
|
|
check("the collection grew by exactly that many", len(dest.tracks) == before + 2)
|
|
check("the new ids are unique", len(set(ids)) == len(ids))
|
|
# A track that is not in the projection is invisible to search, playlists and
|
|
# the deck — added but unusable.
|
|
check("each added track is in the projection", all(dest.track(i) is not None for i in ids))
|
|
check("and is findable by its id", all(dest.by_key.get(i) is not None for i in ids))
|
|
|
|
added = dest.track(ids[0])
|
|
origin = source.tracks[0]
|
|
check("title survives", added.title == origin.title, f"{added.title!r}")
|
|
check("artist survives", added.artist == origin.artist)
|
|
check("bpm survives", added.bpm == origin.bpm)
|
|
check("label survives", added.label == origin.label)
|
|
check("comment survives", added.comment == origin.comment)
|
|
check("the id is the Traktor primary key, not the drive's",
|
|
added.id != origin.id and added.id.startswith("Macintosh HD"), added.id)
|
|
check("filepath points at the COPY, not the drive",
|
|
str(music) in (added.filepath or ""), str(added.filepath))
|
|
|
|
print("== prep survives the crossing ==")
|
|
src_cues = source.track_cues(origin.id)
|
|
got = dest.track_cues(ids[0])
|
|
|
|
check("the flexible grid crosses intact",
|
|
[(round(m.start, 3), m.bpm) for m in got.grid_markers]
|
|
== [(round(m.start, 3), m.bpm) for m in src_cues.grid_markers],
|
|
str([(m.start, m.bpm) for m in got.grid_markers]))
|
|
check("both tempo segments are there", len(got.grid_markers) == 2)
|
|
|
|
# Hot cues must keep their PAD. Muscle memory is most of what a hotcue layout
|
|
# is, so a cue that moves is nearly as bad as one that vanishes.
|
|
src_hot = {c.slot: c for c in src_cues.cues if c.role == "hotcue"}
|
|
got_by_slot = {c.slot: c for c in got.cues}
|
|
same_pad = [
|
|
s for s, c in src_hot.items()
|
|
if s in got_by_slot and abs(got_by_slot[s].start - c.start) < 0.001
|
|
]
|
|
check("every hot cue keeps the pad it was on",
|
|
sorted(same_pad) == sorted(src_hot), f"kept {sorted(same_pad)} of {sorted(src_hot)}")
|
|
check("a loop stays a loop, with its length",
|
|
got_by_slot[0].type == "loop" and abs(got_by_slot[0].length - 1.875) < 0.001)
|
|
|
|
# Memory cues cross AS memory cues — Traktor's cues on no pad (HOTCUE="-1"),
|
|
# decided 2026-10-16 once Traktor was measured keeping them. They used to be
|
|
# promoted to spare pads.
|
|
n_memory = sum(1 for c in src_cues.cues if c.role != "hotcue")
|
|
padless = [c for c in dest._store.model_entry(ids[0]).cue_v2
|
|
if c.grid is None and c.hotcue == -1]
|
|
check("every memory cue is written as a memory cue (a CUE_V2 on no pad)",
|
|
len(padless) == n_memory and n_memory > 0, f"{len(padless)} of {n_memory}")
|
|
check("and no extra pad is taken",
|
|
sorted(c.slot for c in got.cues if c.role == "hotcue") == sorted(src_hot))
|
|
# The demo track's memory loop starts exactly where pad A's loop does. Traktor
|
|
# draws a pad and a memory cue at one START as ONE marker (measured), so the
|
|
# projection shows it once, as the pad (decided 2026-10-16).
|
|
pad_starts = {round(c.start, 3) for c in got.cues if c.role == "hotcue"}
|
|
got_memory = [c for c in got.cues if c.role == "memory"]
|
|
check("a memory cue is shown unless a pad sits on its START",
|
|
len(got_memory) + sum(1 for c in padless
|
|
if round((c.start - dest._store.time_offset_ms(
|
|
dest._store.model_entry(ids[0]))) / 1000, 3) in pad_starts)
|
|
== n_memory,
|
|
f"{len(got_memory)} shown, pads at {sorted(pad_starts)}")
|
|
check("a shown memory cue has no pad and an id",
|
|
all(c.slot is None and c.id for c in got_memory))
|
|
|
|
# `set_analysed_grid` would place Traktor's beat-1 companion on pad A. That both
|
|
# invents a cue the source never had and displaces the imported pad A cue — it
|
|
# silently ate one during development, which is why this is asserted.
|
|
check("no beatgrid companion cue is invented",
|
|
all(c.grid_marker is None for c in got.cues),
|
|
str([(c.slot, c.grid_marker) for c in got.cues]))
|
|
|
|
print("== the existing 8,485 entries do not move ==")
|
|
outcome = dest.save()
|
|
after = nml.read_bytes()
|
|
diff = list(unified_diff(original.decode("utf-8", "replace").splitlines(),
|
|
after.decode("utf-8", "replace").splitlines(), lineterm="", n=0))
|
|
removed = [l for l in diff if l.startswith("-") and not l.startswith("---")]
|
|
# Exactly one line is rewritten: the one carrying <COLLECTION ENTRIES="N">, whose
|
|
# count changed. Everything else is pure addition.
|
|
check("only the COLLECTION header line is rewritten",
|
|
len(removed) == 1 and "<COLLECTION ENTRIES=" in removed[0],
|
|
f"{len(removed)} removed: {removed[:1]}")
|
|
check("nothing else is removed or reformatted",
|
|
all("<COLLECTION ENTRIES=" in l for l in removed))
|
|
|
|
# ENTRIES is a real count Traktor reads, not decoration. Nothing else in the app
|
|
# changes how many entries exist, so nothing had ever needed to maintain it.
|
|
import re # noqa: E402
|
|
|
|
declared = int(re.search(rb'<COLLECTION ENTRIES="(\d+)"', after).group(1))
|
|
check("the ENTRIES count is updated to match",
|
|
declared == after.count(b"<ENTRY ") == before + 2,
|
|
f"declared {declared}, actual {after.count(b'<ENTRY ')}")
|
|
# The version-history message is what a user reads when deciding which version
|
|
# to roll back to, so an import must not read as an edit. Asserted on the
|
|
# SaveOutcome because save() clears the journal.
|
|
check("the version-history message says the tracks were ADDED",
|
|
"Added 2 tracks" in outcome.summary, outcome.summary)
|
|
check("and does not also call them edited",
|
|
"edited 2 tracks" not in outcome.summary.lower(), outcome.summary)
|
|
|
|
print("== reopening proves it is really there ==")
|
|
reopened = TraktorAdapter(nml)
|
|
check("the added tracks survive a round trip", len(reopened.tracks) == before + 2)
|
|
round_tripped = reopened.track(ids[0])
|
|
check("with their metadata", round_tripped is not None and round_tripped.title == origin.title)
|
|
rt_cues = reopened.track_cues(ids[0])
|
|
check("with their grid", len(rt_cues.grid_markers) == 2)
|
|
check("and with their cues", len(rt_cues.cues) == len(got.cues))
|
|
|
|
print("== refusals ==")
|
|
check("a second import of the same file is refused, not silently duplicated",
|
|
_raises(lambda: dest.add_tracks(items), InvalidCommand))
|
|
missing = NewTrack(track=items[0].track, audio_path=music / "nope.mp3", cues=None)
|
|
check("a missing audio file is refused before anything is written",
|
|
_raises(lambda: dest.add_tracks([missing]), InvalidCommand))
|
|
check("read-only OneLibrary refuses to receive tracks",
|
|
_raises(lambda: source.add_tracks(items), Unsupported))
|
|
|
|
print("== a track with no prep at all ==")
|
|
nml2, music2, _ = fresh_collection()
|
|
dest2 = TraktorAdapter(nml2)
|
|
bare_audio = music2 / "bare.mp3"
|
|
shutil.copy(items[0].audio_path, bare_audio)
|
|
bare = NewTrack(
|
|
track=Track(id="ignored", title="Bare", artist="Nobody"),
|
|
audio_path=bare_audio,
|
|
cues=None,
|
|
)
|
|
bare_ids = dest2.add_tracks([bare])
|
|
check("a track with no cues and no grid still imports", len(bare_ids) == 1)
|
|
bare_cues = dest2.track_cues(bare_ids[0])
|
|
check("and has no grid", bare_cues.grid_markers == [])
|
|
check("and no cues", bare_cues.cues == [])
|
|
check("and no BPM was invented", dest2.track(bare_ids[0]).bpm is None)
|
|
|
|
print("== a full hotcue bank ==")
|
|
# Traktor has 8 pads. Nine cues cannot all fit, so the rule has to be explicit
|
|
# about WHICH survive rather than leaving it to iteration order.
|
|
crowded_audio = music2 / "crowded.mp3"
|
|
shutil.copy(items[0].audio_path, crowded_audio)
|
|
many = TrackCues(
|
|
grid_markers=[GridMarker(start=0.0, bpm=120.0)],
|
|
cues=[CuePoint(type="cue", role="hotcue", start=float(i), length=0.0, slot=i)
|
|
for i in range(8)]
|
|
+ [CuePoint(type="cue", role="memory", start=99.0, length=0.0, slot=None),
|
|
# A pad past Traktor's bank: displaced, and with no spare pad left.
|
|
CuePoint(type="cue", role="hotcue", start=50.0, length=0.0, slot=9)],
|
|
)
|
|
crowded_id = dest2.add_tracks([NewTrack(
|
|
track=Track(id="ignored", title="Crowded", bpm=120.0),
|
|
audio_path=crowded_audio,
|
|
cues=many,
|
|
)])[0]
|
|
crowded = dest2.track_cues(crowded_id)
|
|
crowded_hot = [c for c in crowded.cues if c.role == "hotcue"]
|
|
check("the bank is filled but not overflowed", len(crowded_hot) == 8, str(len(crowded_hot)))
|
|
check("the hot cues keep their own pads",
|
|
sorted(c.slot for c in crowded_hot) == list(range(8))
|
|
and all(c.start == float(c.slot) for c in crowded_hot))
|
|
check("the memory cue stays a memory cue, and a displaced hot cue with no spare "
|
|
"pad becomes one — nothing is dropped",
|
|
sorted(round(c.start) for c in crowded.cues if c.role == "memory") == [50, 99],
|
|
str([(c.role, c.start) for c in crowded.cues]))
|
|
|
|
print("== the source slot ==")
|
|
# A source is a library read FROM, open alongside the loaded one. The whole slot
|
|
# rests on it being read-only, so that is asserted where the source is opened
|
|
# rather than assumed by everything downstream.
|
|
from konduktor.app_state import AppState # noqa: E402
|
|
|
|
state = AppState()
|
|
nml3, _, _ = fresh_collection()
|
|
state.open(nml3)
|
|
check("a collection loads as the destination", state.loaded)
|
|
check("and no source is open yet", not state.source_loaded)
|
|
|
|
state.open_source(FIXTURE)
|
|
check("a read-only drive opens as a source", state.source_loaded)
|
|
check("the destination is untouched by opening a source",
|
|
state.loaded and state.path == nml3)
|
|
check("the source projects its own tracks", len(state.source.tracks) == 2)
|
|
check("the two libraries are genuinely different objects",
|
|
state.source is not state.adapter)
|
|
|
|
# Opening your own collection as a source of itself is the mistake this guards.
|
|
before_source = state.source
|
|
check("a WRITABLE library is refused as a source",
|
|
_raises(lambda: state.open_source(nml3), ValueError))
|
|
check("and the refusal leaves the existing source in place",
|
|
state.source is before_source)
|
|
|
|
state.close_source()
|
|
check("closing releases the source", not state.source_loaded)
|
|
check("but not the destination", state.loaded)
|
|
check("closing twice is safe", state.close_source() is None)
|
|
|
|
print("== folders ==")
|
|
# Imported playlists land in a folder named after the drive, and the protocol had
|
|
# no folder verb until this feature needed one.
|
|
nml4, _, _ = fresh_collection()
|
|
dest4 = TraktorAdapter(nml4)
|
|
fid = dest4.create_folder("Hardy")
|
|
check("a folder can be created", fid.startswith("fld:"), fid)
|
|
# Re-importing the same stick must land BESIDE the first import, not next to an
|
|
# identically-named twin.
|
|
check("creating it again returns the same folder", dest4.create_folder("Hardy") == fid)
|
|
pid = dest4.create_playlist("demos", fid)
|
|
node = next((n for n in dest4.playlist_tree() if n.id == fid), None)
|
|
check("the folder is in the tree", node is not None and node.kind == "folder")
|
|
check("and the playlist is inside it",
|
|
node is not None and [c.name for c in node.children] == ["demos"])
|
|
check("read-only sources refuse to make folders",
|
|
_raises(lambda: source.create_folder("x"), Unsupported))
|
|
|
|
print("== the job registry ==")
|
|
from konduktor.jobs import JobCancelled, JobRegistry # noqa: E402
|
|
|
|
reg = JobRegistry()
|
|
job = reg.submit("test", lambda h: 41 + 1)
|
|
for _ in range(200):
|
|
if job.finished:
|
|
break
|
|
time.sleep(0.01)
|
|
check("a job runs and reports its result", job.state == "done" and job.result == 42)
|
|
|
|
def boom(h):
|
|
raise RuntimeError("nope")
|
|
|
|
|
|
# The registry logs a traceback for a failed job, which is right in production
|
|
# and alarming in test output — a deliberate failure should not look like a
|
|
# real one to someone scanning the suite.
|
|
logging.getLogger("konduktor.jobs").setLevel(logging.CRITICAL)
|
|
failing = reg.submit("test", boom)
|
|
for _ in range(200):
|
|
if failing.finished:
|
|
break
|
|
time.sleep(0.01)
|
|
check("a failing job is reported, not swallowed",
|
|
failing.state == "failed" and "nope" in (failing.error or ""))
|
|
logging.getLogger("konduktor.jobs").setLevel(logging.NOTSET)
|
|
|
|
# Cancel is a REQUEST: the job stops at its next checkpoint, so the state must
|
|
# not flip until the work has actually unwound and cleaned up.
|
|
started = threading.Event()
|
|
|
|
def slow(h):
|
|
started.set()
|
|
while True:
|
|
h.raise_if_cancelled()
|
|
time.sleep(0.01)
|
|
|
|
running = reg.submit("test", slow)
|
|
started.wait(2)
|
|
check("cancel is accepted while running", reg.cancel(running.id))
|
|
for _ in range(400):
|
|
if running.finished:
|
|
break
|
|
time.sleep(0.01)
|
|
check("and the job ends up cancelled", running.state == "cancelled")
|
|
check("cancelling a finished job does nothing", not reg.cancel(running.id))
|
|
check("cancelling an unknown job does nothing", not reg.cancel("nope"))
|
|
|
|
print("== import: plan and run ==")
|
|
from konduktor import importer # noqa: E402
|
|
|
|
nml5, music5, _ = fresh_collection()
|
|
dest5 = TraktorAdapter(nml5)
|
|
plan5 = importer.plan(source, dest5, music5)
|
|
check("the plan covers the whole drive when nothing is selected", len(plan5.tracks) == 2)
|
|
check("it finds the playlist", [n for _, n in plan5.playlists] == ["demos"])
|
|
check("it sizes the copy", plan5.total_bytes > 0)
|
|
check("nothing is missing", plan5.importable == plan5.tracks)
|
|
|
|
# Selecting a playlist AUTO-INCLUDES its tracks, deduplicated.
|
|
pl_id = importer._all_playlists(source)[0][0]
|
|
plan_pl = importer.plan(source, dest5, music5, playlist_ids=[pl_id])
|
|
check("selecting a playlist pulls in its tracks", len(plan_pl.tracks) == 2)
|
|
plan_both = importer.plan(
|
|
source, dest5, music5, playlist_ids=[pl_id],
|
|
track_ids=[t.id for t in source.tracks],
|
|
)
|
|
check("a track in both a selection and a playlist is copied once",
|
|
len(plan_both.tracks) == 2, str(len(plan_both.tracks)))
|
|
|
|
|
|
class _Handle:
|
|
"""A JobHandle stand-in, so the import can be driven without a thread."""
|
|
|
|
def __init__(self, cancel_after=None):
|
|
self.messages = []
|
|
self.done = 0
|
|
self.total = 0
|
|
self._cancel_after = cancel_after
|
|
self.cancelled = False
|
|
|
|
def progress(self, done=None, total=None, message=None):
|
|
if done is not None:
|
|
self.done = done
|
|
if total is not None:
|
|
self.total = total
|
|
if message is not None:
|
|
self.messages.append(message)
|
|
if self._cancel_after is not None and self.done >= self._cancel_after:
|
|
self.cancelled = True
|
|
|
|
def raise_if_cancelled(self):
|
|
if self.cancelled:
|
|
raise JobCancelled()
|
|
|
|
|
|
handle = _Handle()
|
|
result = importer.run(source, dest5, plan5, handle, folder_name="Hardy")
|
|
check("the import reports what it did", result["tracks"] == 2 and result["playlists"] == 1)
|
|
check("progress reached the total", handle.total > 0 and handle.done == handle.total,
|
|
f"{handle.done}/{handle.total}")
|
|
check("it said what it was doing", any("Copying" in m for m in handle.messages))
|
|
check("the audio really was copied",
|
|
sorted(p.name for p in music5.iterdir()) == ["Demo Track 1.mp3", "Demo Track 2.mp3"])
|
|
check("the tracks are in the collection", len(dest5.tracks) == 8485 + 2)
|
|
tree5 = dest5.playlist_tree()
|
|
folder = next((n for n in tree5 if n.name == "Hardy"), None)
|
|
check("the playlist landed in a folder named after the drive",
|
|
folder is not None and [c.name for c in folder.children] == ["demos"])
|
|
check("and the playlist has its tracks",
|
|
folder is not None and folder.children[0].count == 2)
|
|
|
|
print("== import: cancelling rolls back ==")
|
|
nml6, music6, original6 = fresh_collection()
|
|
dest6 = TraktorAdapter(nml6)
|
|
plan6 = importer.plan(source, dest6, music6)
|
|
# Cancel mid-copy — which is the case that used to leave a half-written file
|
|
# behind, because cleanup only covered COMPLETED copies.
|
|
cancelling = _Handle(cancel_after=1)
|
|
cancelled_ok = _raises(
|
|
lambda: importer.run(source, dest6, plan6, cancelling, folder_name="Hardy"),
|
|
JobCancelled,
|
|
)
|
|
check("a cancelled import raises rather than half-finishing", cancelled_ok)
|
|
left = sorted(p.name for p in music6.iterdir()) if music6.exists() else []
|
|
check("no audio is left orphaned, including the file being written", left == [], str(left))
|
|
check("the collection on disk is untouched", nml6.read_bytes() == original6)
|
|
check("and nothing was added in memory either", len(dest6.tracks) == 8485)
|
|
# The retry must not have to suffix around debris from the cancelled run.
|
|
retry = _Handle()
|
|
importer.run(source, dest6, plan6, retry, folder_name="Hardy")
|
|
check("a retry after a cancel copies under the original names",
|
|
sorted(p.name for p in music6.iterdir()) == ["Demo Track 1.mp3", "Demo Track 2.mp3"],
|
|
str(sorted(p.name for p in music6.iterdir())))
|
|
|
|
print("== import: missing audio ==")
|
|
nml7, music7, _ = fresh_collection()
|
|
dest7 = TraktorAdapter(nml7)
|
|
plan7 = importer.plan(source, dest7, music7)
|
|
plan7.tracks[0].missing = True
|
|
check("a missing file is excluded from the copy", len(plan7.importable) == 1)
|
|
check("but still reported", len(plan7.as_dict()["missing"]) == 1)
|
|
r7 = importer.run(source, dest7, plan7, _Handle(), folder_name="Hardy")
|
|
check("the import proceeds without it", r7["tracks"] == 1 and r7["skipped_missing"] == 1)
|
|
|
|
print("== import: duplicate detection ==")
|
|
plan8 = importer.plan(source, dest5, music5)
|
|
check("re-importing flags the tracks as already present",
|
|
all(t.duplicate for t in plan8.tracks), str([t.duplicate for t in plan8.tracks]))
|
|
check("but does not refuse them — the user chose to add everything",
|
|
len(plan8.importable) == 2)
|
|
|
|
print("== import route: chosen tracks into a playlist and an export (drag and drop) ==")
|
|
# Device tracks dropped on a collection playlist open Import with that playlist
|
|
# as the target: only the chosen tracks, no drive folder, all into the playlist.
|
|
nml9, music9, _ = fresh_collection()
|
|
drive9 = Path(tempfile.mkdtemp(prefix="konduktor-import-drive-")) / "Dingus"
|
|
shutil.copytree(FIXTURE, drive9, ignore=shutil.ignore_patterns("rekordbox-edited"))
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
import konduktor.main as main # noqa: E402
|
|
|
|
with TestClient(main.app) as c:
|
|
check("the collection opens", c.post("/api/library/open", json={"path": str(nml9)}).status_code == 200)
|
|
check("the drive opens as a source", c.post("/api/source/open", json={"path": str(drive9)}).status_code == 200)
|
|
picked = c.get("/api/source/tracks", params={"limit": 10}).json()
|
|
picked = picked["items"][:1]
|
|
target = c.post("/api/playlists", json={"name": "Dropped"}).json()["id"]
|
|
eset = c.post("/api/exports", json={"name": "Gig", "targets": ["traktor"],
|
|
"destination": str(music9.parent / "gig")}).json()["id"]
|
|
r = c.post("/api/import", json={"destination": str(music9), "track_ids": [picked[0]["id"]],
|
|
"into_playlist": target, "export_id": eset})
|
|
check("the import starts", r.status_code == 200, r.text[:200])
|
|
job = r.json()
|
|
for _ in range(200):
|
|
job = c.get(f"/api/jobs/{job['id']}").json()
|
|
if job["state"] != "running":
|
|
break
|
|
time.sleep(0.05)
|
|
check("…and finishes", job["state"] == "done", str(job)[:300])
|
|
entries = c.get(f"/api/playlists/{target}/tracks").json()
|
|
check("the one chosen track lands in the target playlist",
|
|
len(entries) == 1 and entries[0]["title"] == picked[0]["title"], str([e["title"] for e in entries]))
|
|
names = [n["name"] for n in c.get("/api/playlists").json()]
|
|
check("no drive folder is made when no playlist was chosen", "Dingus" not in names, str(names))
|
|
ex = next(e for e in c.get("/api/exports").json() if e["id"] == eset)
|
|
check("…and it is added to the export too", ex["track_ids"] == [entries[0]["id"]], str(ex["track_ids"]))
|
|
c.delete("/api/source")
|
|
|
|
print()
|
|
print("RESULT:", "FAILED" if failed else "ALL PASSED")
|
|
sys.exit(1 if failed else 0)
|