diff --git a/jest.config.cjs b/jest.config.cjs
new file mode 100644
index 0000000..b413e10
--- /dev/null
+++ b/jest.config.cjs
@@ -0,0 +1,5 @@
+/** @type {import('ts-jest').JestConfigWithTsJest} */
+module.exports = {
+ preset: 'ts-jest',
+ testEnvironment: 'node',
+};
\ No newline at end of file
diff --git a/package.json b/package.json
index ea81d73..d45e357 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
"@sveltejs/adapter-node": "^1.2.3",
"@sveltejs/kit": "^1.5.0",
"@tailwindcss/typography": "^0.5.10",
+ "@types/jest": "^29.5.12",
"@types/jest-image-snapshot": "^6.2.1",
"@types/node": "^20.8.2",
"@types/ramda": "^0.29.10",
@@ -53,6 +54,7 @@
"svelte": "^3.54.0",
"svelte-check": "^3.0.1",
"tailwindcss": "^3.3.3",
+ "ts-jest": "^29.1.2",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.2.0"
diff --git a/src/lib/components/events/content/Kind317.svelte b/src/lib/components/events/content/Patch.svelte
similarity index 80%
rename from src/lib/components/events/content/Kind317.svelte
rename to src/lib/components/events/content/Patch.svelte
index 9f9e7b0..45031a6 100644
--- a/src/lib/components/events/content/Kind317.svelte
+++ b/src/lib/components/events/content/Patch.svelte
@@ -2,12 +2,17 @@
import type { NDKTag } from '@nostr-dev-kit/ndk'
import parseDiff from 'parse-diff'
import ParsedContent from './ParsedContent.svelte'
+ import { extractPatchMessage } from './utils'
export let content: string = ''
export let tags: NDKTag[] = []
- let commit_id = extractTagContent('commit') || '[unknown commit_id]'
- let commit_message = extractTagContent('description') || '[untitled]'
+ let commit_id_shorthand =
+ extractTagContent('commit')?.substring(0, 8) || '[commit_id unknown]'
+ let commit_message =
+ extractTagContent('description') ||
+ extractPatchMessage(content) ||
+ '[untitled]'
let files = parseDiff(content)
function extractTagContent(name: string): string | undefined {
@@ -29,7 +34,7 @@
| Changes: |
- {commit_id.substring(0, 8)}
+ {commit_id_shorthand}
|
{#each files as file}
diff --git a/src/lib/components/events/content/utils.spec.ts b/src/lib/components/events/content/utils.spec.ts
new file mode 100644
index 0000000..454b7ed
--- /dev/null
+++ b/src/lib/components/events/content/utils.spec.ts
@@ -0,0 +1,41 @@
+import { extractPatchMessage } from './utils'
+
+// const simple =
+// const example = `From 35ef1fe53b5a460266a1666709d886560d99cd67 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf \nDate: Mon, 29 Jan 2024 09:41:27 -0300\nSubject: [PATCH] fix multi-attempt password prompt.\n\nthe print was doing nothing\nand the continue was missing\n---\nfound this bug while copying these functions to be used in nak\n\n helpers.go | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/helpers.go b/helpers.go\nindex 0b3790d..9b5c3da 100644\n--- a/helpers.go\n+++ b/helpers.go\n@@ -176,7 +176,7 @@ func promptDecrypt(ncryptsec1 string) (string, error) {\n \t\t}\n \t\tsec, err := nip49.Decrypt(ncryptsec1, password)\n \t\tif err != nil {\n-\t\t\tfmt.Fprintf(os.Stderr, "failed to decrypt: %s", err)\n+\t\t\tcontinue\n \t\t}\n \t\treturn sec, nil\n \t}\n--\n2.43.0\n', tags: (3) […], kind: 1617, id: "fd5d1be541bf2d20c51ca63265cc893eecb4be8720db9b42abec21b9ca9747de", sig: "d4733b8b32c05d1fb33a76105926fc537e4060df25405521b3f74f91ed7d65f345386260e8a825c79d67c3dd67f5e7eea7d532cda48cb8d45f09f9be19775289", pubkey: "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", … }`
+
+test('extractPatchMessage - normal message end', () => {
+ expect(
+ extractPatchMessage(
+ 'From 5ec8fb38b7e4d7b2081e276be456519e2dc76d46 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf \nDate: Mon, 29 Jan 2024 09:49:32 -0300\nSubject: [PATCH] invert alias order for `git str send --to`\n\n---\n send.go | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/send.go b/send.go\nindex bc81c00..d9017b6 100644\n--- a/send.go\n+++ b/send.go\n@@ -25,8 +25,8 @@ var send = &cli.Command{\n \t\t\tUsage: "if we should save the secret key to git config --local",\n \t\t},\n \t\t&cli.StringFlag{\n-\t\t\tName: "repository",\n-\t\t\tAliases: []string{"a", "to"},\n+\t\t\tName: "to",\n+\t\t\tAliases: []string{"a", "repository"},\n \t\t\tUsage: "repository reference, as an naddr1... code",\n \t\t},\n \t\t&cli.StringSliceFlag{\n@@ -170,7 +170,7 @@ func getAndApplyTargetRepository(\n \t\treturn nil, nil\n \t}\n \n-\ttarget := c.String("repository")\n+\ttarget := c.String("to")\n \tvar stored string\n \tif target == "" {\n \t\ttarget, _ = git("config", "--local", "str.upstream")\n-- \n2.43.0'
+ )
+ ).toEqual('invert alias order for `git str send --to`')
+})
+
+test('extractPatchMessage - unusual message end', () => {
+ expect(
+ extractPatchMessage(
+ `From 35ef1fe53b5a460266a1666709d886560d99cd67 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf \nDate: Mon, 29 Jan 2024 09:41:27 -0300\nSubject: [PATCH] fix multi-attempt password prompt.\n\nthe print was doing nothing\nand the continue was missing\n---\nfound this bug while copying these functions to be used in nak\n\n helpers.go | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/helpers.go b/helpers.go\nindex 0b3790d..9b5c3da 100644\n--- a/helpers.go\n+++ b/helpers.go\n@@ -176,7 +176,7 @@ func promptDecrypt(ncryptsec1 string) (string, error) {\n \t\t}\n \t\tsec, err := nip49.Decrypt(ncryptsec1, password)\n \t\tif err != nil {\n-\t\t\tfmt.Fprintf(os.Stderr, "failed to decrypt: %s", err)\n+\t\t\tcontinue\n \t\t}\n \t\treturn sec, nil\n \t}\n--\n2.43.0\n`
+ )
+ ).toEqual(
+ 'fix multi-attempt password prompt.\n\nthe print was doing nothing\nand the continue was missing\n---\nfound this bug while copying these functions to be used in nak'
+ )
+})
+
+test('extractPatchMessage - returns undefined if not parsed', () => {
+ expect(
+ extractPatchMessage(
+ `From 35ef1fe53b5a460266a1666709d886560d99cd67 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf \nDate: Mon, 29 Jan 20`
+ )
+ ).toBeUndefined()
+})
+
+// TODO make this pass
+test.skip('extractPatchMessage - anotherunusual message end', () => {
+ expect(
+ extractPatchMessage(
+ `From 1263051aa4426937c5ef4f7616e06e9a8ea021e0 Mon Sep 17 00:00:00 2001\nFrom: William Casarin \nDate: Mon, 22 Jan 2024 14:41:54 -0800\nSubject: [PATCH] Revert "mention: fix broken mentions when there is text is\n directly after"\n\nThis reverts commit af75eed83a2a1dd0eb33a0a27ded71c9f44dacbd.\n---\n damus/Views/PostView.swift | 7 -------\n damusTests/PostViewTests.swift | 22 ----------------------\n 2 files changed, 29 deletions(-)\n\ndiff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift\nindex 21ca0498..934ed7de 100644\n--- a/damus/Views/PostView.swift\n+++ b/damus/Views/PostView.swift\n@@ -619,13 +619,6 @@ func load_draft_for_post(drafts: Drafts, action: PostAction) -> DraftArtifacts?\n func build_post(state: DamusState, post: NSMutableAttributedString, action: PostAction, uploadedMedias: [UploadedMedia], references: [RefId]) -> NostrPost {\n post.enumerateAttributes(in: NSRange(location: 0, length: post.length), options: []) { attributes, range, stop in\n if let link = attributes[.link] as? String {\n- let nextCharIndex = range.upperBound\n- if nextCharIndex < post.length,\n- let nextChar = post.attributedSubstring(from: NSRange(location: nextCharIndex, length: 1)).string.first,\n- !nextChar.isWhitespace {\n- post.insert(NSAttributedString(string: " "), at: nextCharIndex)\n- }\n-\n let normalized_link: String\n if link.hasPrefix("damus:nostr:") {\n // Replace damus:nostr: URI prefix with nostr: since the former is for internal navigation and not meant to be posted.\ndiff --git a/damusTests/PostViewTests.swift b/damusTests/PostViewTests.swift\nindex 51976cad..ae78c3e6 100644\n--- a/damusTests/PostViewTests.swift\n+++ b/damusTests/PostViewTests.swift\n@@ -142,28 +142,6 @@ final class PostViewTests: XCTestCase {\n checkMentionLinkEditorHandling(content: content, replacementText: "", replacementRange: NSRange(location: 5, length: 28), shouldBeAbleToChangeAutomatically: true)\n \n }\n- \n- func testMentionLinkEditorHandling_noWhitespaceAfterLink1_addsWhitespace() {\n- var content: NSMutableAttributedString\n-\n- content = NSMutableAttributedString(string: "Hello @user ")\n- content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5))\n- checkMentionLinkEditorHandling(content: content, replacementText: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in\n- XCTAssertEqual(newManuallyEditedContent.string, "Hello @user up")\n- XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil))\n- })\n- }\n- \n- func testMentionLinkEditorHandling_noWhitespaceAfterLink2_addsWhitespace() {\n- var content: NSMutableAttributedString\n-\n- content = NSMutableAttributedString(string: "Hello @user test")\n- content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5))\n- checkMentionLinkEditorHandling(content: content, replacementText: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in\n- XCTAssertEqual(newManuallyEditedContent.string, "Hello @user uptest")\n- XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil))\n- })\n- }\n }\n \n func checkMentionLinkEditorHandling(\n\nbase-commit: c67741983e3f07f2386eaa388cb8a1475e8e0471\n-- \n2.42.0\n\n`
+ )
+ ).toEqual(
+ 'Revert "mention: fix broken mentions when there is text is\n directly after"\n\nThis reverts commit af75eed83a2a1dd0eb33a0a27ded71c9f44dacbd.'
+ )
+})
diff --git a/src/lib/components/events/content/utils.ts b/src/lib/components/events/content/utils.ts
index 54d22b6..93b0327 100644
--- a/src/lib/components/events/content/utils.ts
+++ b/src/lib/components/events/content/utils.ts
@@ -93,3 +93,14 @@ export const parseContent = ({ content }: ContentArgs): ParsedPart[] => {
return result
}
+
+/** this doesn't work for all patch formats and options */
+export const extractPatchMessage = (s: string): string | undefined => {
+ try {
+ const t = s.split('\nSubject: [')[1].split('] ')[1]
+ if (t.split('\n\n---\n ').length > 1) return t.split('\n\n---\n ')[0]
+ return t.split('\n\ndiff --git ')[0].split('\n\n ').slice(0, -1).join('')
+ } catch {
+ return undefined
+ }
+}
diff --git a/src/lib/wrappers/EventCard.svelte b/src/lib/wrappers/EventCard.svelte
index a79e89d..829adf3 100644
--- a/src/lib/wrappers/EventCard.svelte
+++ b/src/lib/wrappers/EventCard.svelte
@@ -1,7 +1,7 @@