-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathScriptDocumentImportServiceTests.cs
More file actions
142 lines (119 loc) · 5.75 KB
/
ScriptDocumentImportServiceTests.cs
File metadata and controls
142 lines (119 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using PrompterOne.Core.Services.Editor;
namespace PrompterOne.Core.Tests;
public sealed class ScriptDocumentImportServiceTests
{
private const string DocxContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
private const string ImportedDocxFileName = "Imported Design Review From File Name.docx";
private const string ImportedDocxTitle = "Imported Design Review From File Name";
private const string ImportedHeading = "Converted heading should stay in the editor body";
private const string ImportedParagraph = "MarkItDown should convert this DOCX paragraph into Markdown for the editor.";
private const string FrontMatterMarkdownFileName = "Quarterly Town Hall.md";
private const string FrontMatterMarkdownTitle = "Front matter title should win";
private const string FrontMatterMarkdownText =
"""
---
title: "Front matter title should win"
---
## [Opening|140WPM|Professional]
### [First block|140WPM|Warm]
Imported markdown should preserve the explicit front matter title in the editor.
""";
private const string NativeScriptFileName = "camera-check.tps";
private const string NativeScriptText =
"""
## [Camera Check|140WPM|Professional]
### [Opening|140WPM|Warm]
Native TPS imports should stay untouched.
""";
private const string PlainTextFileName = "Quarterly Town Hall Notes.txt";
private const string PlainTextImportDocumentName = "Quarterly Town Hall Notes.tps.md";
private const string PlainTextImportTitle = "Quarterly Town Hall Notes";
private const string PlainTextImportText =
"""
## [Quarterly Town Hall|140WPM|Professional]
### [Opening|140WPM|Warm]
Plain text imports should become editor-ready TPS markdown documents.
""";
[Test]
[Arguments("episode.tps")]
[Arguments("episode.md")]
[Arguments("episode.txt")]
[Arguments("episode.pdf")]
[Arguments("episode.docx")]
[Arguments("episode.html")]
public void CanImport_ReturnsTrue_ForSupportedPickerFileNames(string fileName)
{
var service = new ScriptDocumentImportService(new ScriptImportDescriptorService());
Assert.True(service.CanImport(fileName));
}
[Test]
[Arguments("")]
[Arguments("episode")]
[Arguments("episode.exe")]
public void CanImport_ReturnsFalse_ForUnsupportedPickerFileNames(string fileName)
{
var service = new ScriptDocumentImportService(new ScriptImportDescriptorService());
Assert.False(service.CanImport(fileName));
}
[Test]
public async Task ImportAsync_PlainTextCanonicalizesDocumentName_AndPreservesBody()
{
var service = new ScriptDocumentImportService(new ScriptImportDescriptorService());
await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(PlainTextImportText));
var descriptor = await service.ImportAsync(stream, PlainTextFileName, "text/plain");
Assert.Equal(PlainTextImportTitle, descriptor.Title);
Assert.Equal(PlainTextImportDocumentName, descriptor.DocumentName);
Assert.Equal(PlainTextImportText, descriptor.Text);
}
[Test]
public async Task ImportAsync_NativeTpsPreservesDocumentName_AndText()
{
var service = new ScriptDocumentImportService(new ScriptImportDescriptorService());
await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(NativeScriptText));
var descriptor = await service.ImportAsync(stream, NativeScriptFileName, "text/plain");
Assert.Equal("camera-check", descriptor.Title);
Assert.Equal(NativeScriptFileName, descriptor.DocumentName);
Assert.Equal(NativeScriptText, descriptor.Text);
}
[Test]
public async Task ImportAsync_MarkdownImport_PreservesFrontMatterTitle_WhenProvided()
{
var service = new ScriptDocumentImportService(new ScriptImportDescriptorService());
await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(FrontMatterMarkdownText));
var descriptor = await service.ImportAsync(stream, FrontMatterMarkdownFileName, "text/markdown");
Assert.Equal(FrontMatterMarkdownTitle, descriptor.Title);
Assert.Equal("Quarterly Town Hall.tps.md", descriptor.DocumentName);
Assert.Equal(FrontMatterMarkdownText, descriptor.Text);
}
[Test]
public async Task ImportAsync_DocxConvertsToMarkdown_AndUsesFileStemTitle()
{
var service = new ScriptDocumentImportService(new ScriptImportDescriptorService());
await using var stream = CreateDocxStream();
var descriptor = await service.ImportAsync(stream, ImportedDocxFileName, DocxContentType);
Assert.Equal(ImportedDocxTitle, descriptor.Title);
Assert.Equal("Imported Design Review From File Name.tps.md", descriptor.DocumentName);
Assert.Contains(ImportedHeading, descriptor.Text, StringComparison.Ordinal);
Assert.Contains(ImportedParagraph, descriptor.Text, StringComparison.Ordinal);
}
private static MemoryStream CreateDocxStream()
{
var stream = new MemoryStream();
using (var document = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, autoSave: true))
{
var mainPart = document.AddMainDocumentPart();
mainPart.Document = new Document(
new Body(
CreateParagraph(ImportedHeading),
CreateParagraph(ImportedParagraph)));
}
stream.Position = 0;
return stream;
}
private static Paragraph CreateParagraph(string text) =>
new(new Run(new Text(text)));
}