Skip to content

Commit c29c905

Browse files
authored
Merge pull request #197 from soniqo/chore/transcribe-context-tests
Add transcribe --context parser tests and fix stale Qwen3-ASR test paths
2 parents f7c5246 + b949434 commit c29c905

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

Tests/AudioCLITests/AudioCLITests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ final class TranscribeCommandTests: XCTestCase {
8585
XCTAssertEqual(transcribe.language, "zh")
8686
}
8787

88+
func testDefaultContextIsNil() throws {
89+
let cmd = try AudioCLI.parseAsRoot(["transcribe", "audio.wav"])
90+
let transcribe = try XCTUnwrap(cmd as? TranscribeCommand)
91+
XCTAssertNil(transcribe.context)
92+
}
93+
94+
func testParsesContext() throws {
95+
let cmd = try AudioCLI.parseAsRoot([
96+
"transcribe", "audio.wav",
97+
"--context", "Project: Meander, participants: Will, Adam"
98+
])
99+
let transcribe = try XCTUnwrap(cmd as? TranscribeCommand)
100+
XCTAssertEqual(transcribe.context, "Project: Meander, participants: Will, Adam")
101+
}
102+
88103
func testParsesFullModelId() throws {
89104
let cmd = try AudioCLI.parseAsRoot(["transcribe", "audio.wav", "-m", "org/my-custom-model"])
90105
let transcribe = try XCTUnwrap(cmd as? TranscribeCommand)

Tests/Qwen3ASRTests/Qwen3ASRTests.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,7 @@ final class Qwen3ASRTests: XCTestCase {
198198
// MARK: - Tokenizer Tests
199199

200200
func testTokenizerLoadsVocab() throws {
201-
let cacheDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
202-
.appendingPathComponent("qwen3-asr")
203-
.appendingPathComponent("aufklarer_Qwen3-ASR-0.6B-MLX-4bit")
204-
201+
let cacheDir = try HuggingFaceDownloader.getCacheDirectory(for: "aufklarer/Qwen3-ASR-0.6B-MLX-4bit")
205202
let vocabPath = cacheDir.appendingPathComponent("vocab.json")
206203

207204
guard FileManager.default.fileExists(atPath: vocabPath.path) else {
@@ -218,10 +215,7 @@ final class Qwen3ASRTests: XCTestCase {
218215
}
219216

220217
func testTokenizerLoadsAddedTokens() throws {
221-
let cacheDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
222-
.appendingPathComponent("qwen3-asr")
223-
.appendingPathComponent("aufklarer_Qwen3-ASR-0.6B-MLX-4bit")
224-
218+
let cacheDir = try HuggingFaceDownloader.getCacheDirectory(for: "aufklarer/Qwen3-ASR-0.6B-MLX-4bit")
225219
let vocabPath = cacheDir.appendingPathComponent("vocab.json")
226220

227221
guard FileManager.default.fileExists(atPath: vocabPath.path) else {
@@ -242,10 +236,7 @@ final class Qwen3ASRTests: XCTestCase {
242236
}
243237

244238
func testTokenizerDecodeWithASRMarker() throws {
245-
let cacheDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
246-
.appendingPathComponent("qwen3-asr")
247-
.appendingPathComponent("aufklarer_Qwen3-ASR-0.6B-MLX-4bit")
248-
239+
let cacheDir = try HuggingFaceDownloader.getCacheDirectory(for: "aufklarer/Qwen3-ASR-0.6B-MLX-4bit")
249240
let vocabPath = cacheDir.appendingPathComponent("vocab.json")
250241

251242
guard FileManager.default.fileExists(atPath: vocabPath.path) else {
@@ -460,10 +451,7 @@ final class Qwen3ASRTests: XCTestCase {
460451
}
461452

462453
func testTokenizerSkipsSpecialTokensWithPipes() throws {
463-
let cacheDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
464-
.appendingPathComponent("qwen3-asr")
465-
.appendingPathComponent("aufklarer_Qwen3-ASR-0.6B-MLX-4bit")
466-
454+
let cacheDir = try HuggingFaceDownloader.getCacheDirectory(for: "aufklarer/Qwen3-ASR-0.6B-MLX-4bit")
467455
let vocabPath = cacheDir.appendingPathComponent("vocab.json")
468456

469457
guard FileManager.default.fileExists(atPath: vocabPath.path) else {

Tests/Qwen3ASRTests/SecurityHardeningTests.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,24 @@ final class DownloadSecurityTests: XCTestCase {
208208
let legacyKey = modelId.replacingOccurrences(of: "/", with: "_")
209209
XCTAssertEqual(key, legacyKey, "New sanitized key should match legacy format for standard model IDs")
210210

211-
// Check the cached directory actually exists if model was downloaded
212-
let cacheDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
213-
.appendingPathComponent("qwen3-asr")
214-
.appendingPathComponent(key)
215-
if FileManager.default.fileExists(atPath: cacheDir.path) {
216-
// Verify expected files are present
217-
let vocabPath = cacheDir.appendingPathComponent("vocab.json")
218-
XCTAssertTrue(FileManager.default.fileExists(atPath: vocabPath.path), "vocab.json should exist in cache")
211+
// Resolve the actual cache directory via the downloader (handles both
212+
// legacy flat layout and the current Hub-style nested layout).
213+
// Note: getCacheDirectory creates the dir as a side effect, so check
214+
// for vocab.json — not just dir existence — to detect a real cache.
215+
let cacheDir = try HuggingFaceDownloader.getCacheDirectory(for: modelId)
216+
let vocabPath = cacheDir.appendingPathComponent("vocab.json")
217+
if FileManager.default.fileExists(atPath: vocabPath.path) {
219218

220219
// Verify tokenizer loads from the cached path
221220
let tokenizer = Qwen3Tokenizer()
222221
try tokenizer.load(from: vocabPath)
223222
XCTAssertEqual(tokenizer.getTokenId(for: "<|im_start|>"), 151644)
224223

225-
// Verify file validation passes on real cached files
224+
// Verify file validation passes on real cached files. Skip hidden
225+
// entries like HuggingFace Hub's `.cache/` metadata directory —
226+
// those are local bookkeeping, not downloaded artifacts.
226227
let contents = try FileManager.default.contentsOfDirectory(at: cacheDir, includingPropertiesForKeys: nil)
227-
for file in contents {
228+
for file in contents where !file.lastPathComponent.hasPrefix(".") {
228229
let name = file.lastPathComponent
229230
XCTAssertNoThrow(try Qwen3ASRModel.validatedRemoteFileName(name),
230231
"Cached file '\(name)' should pass validation")

0 commit comments

Comments
 (0)