|
| 1 | +import { defineCommand, option } from "@bunli/core"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { |
| 4 | + createCliRenderer, |
| 5 | + BoxRenderable, |
| 6 | + TextRenderable, |
| 7 | + type CliRenderer, |
| 8 | +} from "@opentui/core"; |
| 9 | +import { getLinkInformation } from "../functions/linkValidation"; |
| 10 | +import { |
| 11 | + getLinkTimings, |
| 12 | + type TimingPhase, |
| 13 | + type DownloadProgress, |
| 14 | + type DownloadResult, |
| 15 | + SeekRandomMultipleTimes, |
| 16 | + downloadFull, |
| 17 | +} from "../functions/downloadFunctions"; |
| 18 | +import { |
| 19 | + PHASES, |
| 20 | + renderTable, |
| 21 | + renderMultiTable, |
| 22 | + linkInfoRows, |
| 23 | + timingRows, |
| 24 | + seekRows, |
| 25 | + downloadRows, |
| 26 | + formatBytes, |
| 27 | + formatSpeed, |
| 28 | +} from "../library/tables"; |
| 29 | + |
| 30 | +const MULTI_CONNECTIONS = 4; |
| 31 | +const BAR_WIDTH = 40; |
| 32 | + |
| 33 | +function makeProgressBox(renderer: CliRenderer, title: string) { |
| 34 | + const box = new BoxRenderable(renderer, { |
| 35 | + borderStyle: "rounded", |
| 36 | + padding: 1, |
| 37 | + title, |
| 38 | + }); |
| 39 | + const bar = new TextRenderable(renderer, { content: "", fg: "#888888" }); |
| 40 | + const stats = new TextRenderable(renderer, { content: "", fg: "#888888" }); |
| 41 | + box.add(bar); |
| 42 | + box.add(stats); |
| 43 | + |
| 44 | + const update = (state: DownloadProgress) => { |
| 45 | + if (state.totalBytes !== null && state.totalBytes > 0) { |
| 46 | + const pct = Math.min(1, state.bytes / state.totalBytes); |
| 47 | + const filled = Math.floor(pct * BAR_WIDTH); |
| 48 | + bar.content = `${"█".repeat(filled)}${"░".repeat(BAR_WIDTH - filled)} ${(pct * 100).toFixed(1)}%`; |
| 49 | + } else { |
| 50 | + bar.content = "░".repeat(BAR_WIDTH); |
| 51 | + } |
| 52 | + const total = state.totalBytes !== null ? ` / ${formatBytes(state.totalBytes)}` : ""; |
| 53 | + stats.content = `${formatBytes(state.bytes)}${total} · ${formatSpeed(state.bytesPerSecond)}`; |
| 54 | + }; |
| 55 | + |
| 56 | + const finish = (color: string) => { |
| 57 | + bar.fg = color; |
| 58 | + stats.fg = color; |
| 59 | + }; |
| 60 | + |
| 61 | + return { box, update, finish }; |
| 62 | +} |
| 63 | + |
| 64 | +export default defineCommand({ |
| 65 | + name: "test" as const, |
| 66 | + description: "Tests a video link and simulates start, seek and buffering.", |
| 67 | + options: { |
| 68 | + link: option(z.url().optional(), { description: "Link to test", short: "l" }), |
| 69 | + }, |
| 70 | + handler: async ({ flags, positional }) => { |
| 71 | + const link = z.url().parse(positional[0] ?? flags.link); |
| 72 | + |
| 73 | + const linkInfo = await getLinkInformation(link); |
| 74 | + |
| 75 | + const renderer = await createCliRenderer({ exitOnCtrlC: true }); |
| 76 | + |
| 77 | + const progressBox = new BoxRenderable(renderer, { |
| 78 | + borderStyle: "rounded", |
| 79 | + padding: 1, |
| 80 | + title: "Measuring", |
| 81 | + }); |
| 82 | + const rows = new Map<TimingPhase, TextRenderable>(); |
| 83 | + for (const { key, label } of PHASES) { |
| 84 | + const row = new TextRenderable(renderer, { |
| 85 | + content: `⋯ ${label}`, |
| 86 | + fg: "#888888", |
| 87 | + }); |
| 88 | + rows.set(key, row); |
| 89 | + progressBox.add(row); |
| 90 | + } |
| 91 | + renderer.root.add(progressBox); |
| 92 | + |
| 93 | + const results = new Map<TimingPhase, number>(); |
| 94 | + await getLinkTimings(link, undefined, (phase, ms) => { |
| 95 | + results.set(phase, ms); |
| 96 | + const row = rows.get(phase); |
| 97 | + if (!row) return; |
| 98 | + const label = PHASES.find((p) => p.key === phase)?.label ?? phase; |
| 99 | + row.content = `✓ ${label.padEnd(16)} ${ms.toFixed(2)} ms`; |
| 100 | + row.fg = "#00d787"; |
| 101 | + }); |
| 102 | + |
| 103 | + const seekResults = await SeekRandomMultipleTimes(linkInfo, link, 5); |
| 104 | + |
| 105 | + const single = makeProgressBox(renderer, "Downloading (single connection)"); |
| 106 | + renderer.root.add(single.box); |
| 107 | + const singleResult = await downloadFull(link, { |
| 108 | + connections: 1, |
| 109 | + size: linkInfo.size ?? undefined, |
| 110 | + onProgress: single.update, |
| 111 | + }); |
| 112 | + single.finish(singleResult ? "#00d787" : "#ff5f5f"); |
| 113 | + |
| 114 | + let multiResult: DownloadResult | null = null; |
| 115 | + const canMulti = !!linkInfo.size && linkInfo.acceptsRanges; |
| 116 | + if (canMulti) { |
| 117 | + const multi = makeProgressBox( |
| 118 | + renderer, |
| 119 | + `Downloading (${MULTI_CONNECTIONS} connections)`, |
| 120 | + ); |
| 121 | + renderer.root.add(multi.box); |
| 122 | + multiResult = await downloadFull(link, { |
| 123 | + connections: MULTI_CONNECTIONS, |
| 124 | + size: linkInfo.size ?? undefined, |
| 125 | + onProgress: multi.update, |
| 126 | + }); |
| 127 | + multi.finish(multiResult ? "#00d787" : "#ff5f5f"); |
| 128 | + } |
| 129 | + |
| 130 | + renderer.destroy(); |
| 131 | + |
| 132 | + console.log(renderTable("Link Information", linkInfoRows(linkInfo))); |
| 133 | + console.log(renderTable("Network Timings", timingRows(results))); |
| 134 | + console.log( |
| 135 | + renderMultiTable( |
| 136 | + "Seek Results", |
| 137 | + ["#", "Status", "TTFB", "Receive", "Total"], |
| 138 | + seekRows(seekResults), |
| 139 | + ), |
| 140 | + ); |
| 141 | + console.log( |
| 142 | + renderMultiTable( |
| 143 | + "Download Comparison", |
| 144 | + ["Mode", "Conns", "Time", "Bytes", "Speed"], |
| 145 | + downloadRows([ |
| 146 | + { label: "Single", result: singleResult }, |
| 147 | + ...(canMulti |
| 148 | + ? [{ label: `Multi`, result: multiResult }] |
| 149 | + : []), |
| 150 | + ]), |
| 151 | + ), |
| 152 | + ); |
| 153 | + }, |
| 154 | +}); |
0 commit comments