Repository navigation
Resolve TypeScript .ts files as text previews #4924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ba6124
b4df44d
0671a59
7c9d61a
a15a580
6df0af9
484cd7f
e1c2b6e
db19dd1
05f22dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,118 @@ final class FilePreviewReviewFeedbackTests: XCTestCase { | |
| XCTAssertEqual(FilePreviewKindResolver.mode(for: url), .text) | ||
| } | ||
|
|
||
| func testExtensionlessANSITextResolvesAsTextAfterSniffing() throws { | ||
| let url = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent(UUID().uuidString) | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
| try "\u{001B}[31mred\u{001B}[0m\n".write(to: url, atomically: true, encoding: .utf8) | ||
|
|
||
| XCTAssertEqual(FilePreviewKindResolver.initialMode(for: url), .quickLook) | ||
| XCTAssertEqual(FilePreviewKindResolver.mode(for: url), .text) | ||
| } | ||
|
|
||
| func testTypeScriptFileResolvesAsTextInsteadOfTransportStreamMedia() throws { | ||
| let url = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent(UUID().uuidString) | ||
| .appendingPathExtension("ts") | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
|
|
||
| try """ | ||
| export const answer: number = 42; | ||
| console.log(answer); | ||
| """.write(to: url, atomically: true, encoding: .utf8) | ||
|
|
||
| XCTAssertEqual(FilePreviewKindResolver.initialMode(for: url), .quickLook) | ||
| XCTAssertEqual(FilePreviewKindResolver.mode(for: url), .text) | ||
| } | ||
|
|
||
| func testUTF8BOMTypeScriptFileResolvesAsText() throws { | ||
| let url = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent(UUID().uuidString) | ||
| .appendingPathExtension("ts") | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
|
|
||
| var data = Data([0xEF, 0xBB, 0xBF]) | ||
| data.append(Data("export const answer: number = 42;\n".utf8)) | ||
| try data.write(to: url, options: .atomic) | ||
|
|
||
| XCTAssertEqual(FilePreviewKindResolver.initialMode(for: url), .quickLook) | ||
| XCTAssertEqual(FilePreviewKindResolver.mode(for: url), .text) | ||
| } | ||
|
|
||
| func testTypeScriptFileWithNULBytesDoesNotResolveAsText() throws { | ||
| let url = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent(UUID().uuidString) | ||
| .appendingPathExtension("ts") | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
|
|
||
| var data = Data("export const answer = 42;".utf8) | ||
| data.append(contentsOf: [0x00, 0x00]) | ||
| try data.write(to: url, options: .atomic) | ||
|
|
||
| XCTAssertEqual(FilePreviewKindResolver.initialMode(for: url), .quickLook) | ||
| XCTAssertNotEqual(FilePreviewKindResolver.mode(for: url), .text) | ||
| } | ||
|
|
||
| func testTypeScriptTextWinsOverTransportStreamSyncBytePattern() throws { | ||
| let url = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent(UUID().uuidString) | ||
| .appendingPathExtension("ts") | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
|
|
||
| let source = "G" | ||
| + String(repeating: "a", count: 187) | ||
| + "G" | ||
| + String(repeating: "b", count: 187) | ||
| + "\nexport const answer: number = 42;\n" | ||
| try source.write(to: url, atomically: true, encoding: .utf8) | ||
|
|
||
| XCTAssertEqual(FilePreviewKindResolver.initialMode(for: url), .quickLook) | ||
| XCTAssertEqual(FilePreviewKindResolver.mode(for: url), .text) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| func testBinaryTransportStreamFileKeepsMediaPreview() throws { | ||
| let url = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent(UUID().uuidString) | ||
| .appendingPathExtension("ts") | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
|
|
||
| var data = Data(repeating: 0, count: 188 * 2) | ||
| data[0] = 0x47 | ||
| data[1] = 0x40 | ||
| data[2] = 0x00 | ||
| data[3] = 0x10 | ||
| data[188] = 0x47 | ||
| data[189] = 0x41 | ||
| data[190] = 0x00 | ||
| data[191] = 0x10 | ||
| try data.write(to: url, options: .atomic) | ||
|
|
||
| XCTAssertEqual(FilePreviewKindResolver.initialMode(for: url), .quickLook) | ||
| XCTAssertEqual(FilePreviewKindResolver.mode(for: url), .media) | ||
| } | ||
|
|
||
| func testM2TSTransportStreamFileKeepsMediaPreview() throws { | ||
| let url = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent(UUID().uuidString) | ||
| .appendingPathExtension("ts") | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
|
|
||
| var data = Data(repeating: 0, count: 192 * 2) | ||
| data[4] = 0x47 | ||
| data[5] = 0x40 | ||
| data[6] = 0x00 | ||
| data[7] = 0x10 | ||
| data[196] = 0x47 | ||
| data[197] = 0x41 | ||
| data[198] = 0x00 | ||
| data[199] = 0x10 | ||
| try data.write(to: url, options: .atomic) | ||
|
|
||
| XCTAssertEqual(FilePreviewKindResolver.initialMode(for: url), .quickLook) | ||
| XCTAssertEqual(FilePreviewKindResolver.mode(for: url), .media) | ||
| } | ||
|
Comment on lines
+141
to
+181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
After this PR, Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. The binary .ts regression now asserts the intentional initial Quick Look mode before resolved media mode. — Claude Code
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Covered in the transport-stream tests: both binary .ts fixtures now assert initialMode is quickLook before resolving to media. — Claude Code |
||
|
|
||
| func testQuickLookSessionCloseDoesNotDeactivateMountedRepresentableView() throws { | ||
| let url = try temporaryBinaryFile() | ||
| defer { try? FileManager.default.removeItem(at: url) } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.