fix: parseTextNote bug

This commit is contained in:
Shusui MOYATANI
2023-03-21 01:03:57 +09:00
parent 44a512c64b
commit 85ef6e4def
9 changed files with 860 additions and 6075 deletions

38
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@@ -0,0 +1,38 @@
---
name: バグ報告
about: 改善に取り組めるように報告を作成します
title: ''
labels: bug
assignees: syusui-s
---
**バグの説明**
バグについて、明確かつ簡潔に説明してください。
**再現手順**
発生させる手順を説明してください:
1. '...'に移動
2. '....'をクリック
3. '....'までスクロールする
4. エラーが表示される
**期待される挙動**
どうなってほしいかについて、明確かつ簡潔に説明してください。
**スクリーンショット**
スクリーンショットがあればこちらに。
**デスクトップ (該当する場合は以下の情報を埋めてください):**
- OS: (例: Windows 11, macOS Ventura
- ブラウザ: (例: Chrome, Firefox, Safari, Edge
- バージョン: (例: 110
**スマートフォン (該当する場合は以下の情報を埋めてください):**
- デバイス: (例: iPhone 12)
- OS: (例: iOS 16.2, Android 13
- ブラウザ: (例: Safari, Chrome
- バージョン: (例: 110
**追加の情報**
問題に関する他の情報があればこちらに。

View File

@@ -1,6 +1,6 @@
--- ---
name: 機能要望 name: 機能要望
about: このプロジェクトへのアイデア提案 about: このプロジェクトアイデア提案します
title: '' title: ''
labels: enhancement labels: enhancement
assignees: syusui-s assignees: syusui-s

View File

@@ -1,199 +0,0 @@
import { JestConfigWithTsJest } from 'ts-jest';
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/
export default {
// All imported modules in your tests should be mocked automatically
// automock: false,
// Stop running tests after `n` failures
// bail: 0,
// The directory where Jest should store its cached dependency information
// cacheDirectory: "/tmp/jest_rs",
// Automatically clear mock calls, instances, contexts and results before every test
// clearMocks: false,
// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
coverageDirectory: 'coverage',
// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],
// Indicates which provider should be used to instrument code for coverage
coverageProvider: 'v8',
// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
// A path to a custom dependency extractor
// dependencyExtractor: undefined,
// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,
// The default configuration for fake timers
// fakeTimers: {
// "enableGlobally": false
// },
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],
// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,
// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,
// A set of global variables that need to be available in all test environments
// globals: {},
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],
// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "mjs",
// "cjs",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
// Activates notifications for test results
// notify: false,
// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",
// A preset that is used as a base for Jest's configuration
// preset: undefined,
preset: 'ts-jest',
// Run tests from one or more projects
// projects: undefined,
// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
// Automatically reset mock state before every test
// resetMocks: false,
// Reset the module registry before running each individual test
// resetModules: false,
// A path to a custom resolver
// resolver: undefined,
// Automatically restore mock state and implementation before every test
// restoreMocks: false,
// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,
// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],
// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
// The test environment that will be used for testing
// testEnvironment: 'jsdom',
testEnvironment: 'node',
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
// Adds a location field to test results
// testLocationInResults: false,
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",
// A map from regular expressions to paths to transformers
// transform: undefined,
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
// Indicates whether each individual test should be reported during the run
// verbose: undefined,
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
// Whether to use watchman for file crawling
// watchman: true,
} satisfies JestConfigWithTsJest;

6560
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,15 +11,15 @@
"serve": "npm run generatePackageInfo && vite preview", "serve": "npm run generatePackageInfo && vite preview",
"lint": "eslint .", "lint": "eslint .",
"tsc": "tsc --noEmit --skipLibCheck", "tsc": "tsc --noEmit --skipLibCheck",
"test": "jest", "test": "vitest run --no-watch",
"cover": "nyc npm run test", "watch-test": "vitest --watch",
"cover": "vitest run --coverage",
"fix": "eslint --fix .", "fix": "eslint --fix .",
"prepare": "husky install", "prepare": "husky install",
"generatePackageInfo": "node -e 'import(\"./scripts/generatePackageInfo.mjs\").then((m) => m.default())'", "generatePackageInfo": "node -e 'import(\"./scripts/generatePackageInfo.mjs\").then((m) => m.default())'",
"checkLicense": "node -e 'import(\"./scripts/checkLicense.mjs\").then((m) => m.default())'" "checkLicense": "node -e 'import(\"./scripts/checkLicense.mjs\").then((m) => m.default())'"
}, },
"devDependencies": { "devDependencies": {
"@jest/globals": "^29.5.0",
"@typescript-eslint/eslint-plugin": "^5.48.1", "@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1", "@typescript-eslint/parser": "^5.48.1",
"autoprefixer": "^10.4.13", "autoprefixer": "^10.4.13",
@@ -33,25 +33,24 @@
"eslint-plugin-solid": "^0.9.2", "eslint-plugin-solid": "^0.9.2",
"eslint-plugin-tailwindcss": "^3.8.0", "eslint-plugin-tailwindcss": "^3.8.0",
"husky": "^8.0.3", "husky": "^8.0.3",
"jest": "^29.5.0",
"license-checker": "^25.0.1", "license-checker": "^25.0.1",
"lint-staged": "^13.1.0", "lint-staged": "^13.1.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"postcss": "^8.4.21", "postcss": "^8.4.21",
"power-assert": "^1.6.1", "power-assert": "^1.6.1",
"prettier": "^2.8.3", "prettier": "^2.8.3",
"ts-jest": "^29.0.5",
"typescript": "^4.9.4", "typescript": "^4.9.4",
"vite": "^4.0.4", "vite": "^4.0.4",
"vite-plugin-solid": "^2.5.0", "vite-plugin-solid": "^2.5.0",
"vite-plugin-solid-svg": "^0.6.0" "vite-plugin-solid-svg": "^0.6.0",
"vitest": "^0.29.5"
}, },
"dependencies": { "dependencies": {
"@solidjs/meta": "^0.28.2", "@solidjs/meta": "^0.28.2",
"@solidjs/router": "^0.6.0", "@solidjs/router": "^0.6.0",
"@tailwindcss/forms": "^0.5.3", "@tailwindcss/forms": "^0.5.3",
"@tanstack/query-sync-storage-persister": "^4.24.10",
"@tanstack/query-persist-client-core": "^4.24.10", "@tanstack/query-persist-client-core": "^4.24.10",
"@tanstack/query-sync-storage-persister": "^4.24.10",
"@tanstack/solid-query": "^4.24.10", "@tanstack/solid-query": "^4.24.10",
"@thisbeyond/solid-dnd": "^0.7.3", "@thisbeyond/solid-dnd": "^0.7.3",
"@types/lodash": "^4.14.191", "@types/lodash": "^4.14.191",

View File

@@ -1,7 +1,7 @@
import assert from 'assert'; import assert from 'assert';
import { describe } from '@jest/globals'; import { describe, it } from 'vitest';
import parseTextNote from './parseTextNote'; import parseTextNote, { type ParsedTextNoteNode } from './parseTextNote';
describe('parseTextNote', () => { describe('parseTextNote', () => {
/* /*
@@ -23,11 +23,88 @@ describe('parseTextNote', () => {
pubkey: '9366708117c4a7edf9178acdce538c95059b9eb3394808cdd90564094172d972', pubkey: '9366708117c4a7edf9178acdce538c95059b9eb3394808cdd90564094172d972',
}); });
const expected = [ const expected: ParsedTextNoteNode[] = [
{ type: 'PlainText', content: 'this is url\n' }, { type: 'PlainText', content: 'this is url\n' },
{ type: 'URL', content: 'https://github.com/syusui-s/rabbit/#readme' }, { type: 'URL', content: 'https://github.com/syusui-s/rabbit/#readme' },
{ type: 'PlainText', content: ' ' }, { type: 'PlainText', content: ' ' },
{ type: 'HashTag', content: '#rabbit' }, { type: 'HashTag', content: '#rabbit', tagName: 'rabbit' },
];
assert.deepStrictEqual(parsed, expected);
});
it('should parse text note with the url with hash', () => {
const parsed = parseTextNote({
kind: 1,
content: 'this is url\nhttps://github.com/syusui-s/rabbit/#readme #rabbit',
tags: [],
created_at: 1678377182,
pubkey: '9366708117c4a7edf9178acdce538c95059b9eb3394808cdd90564094172d972',
});
const expected: ParsedTextNoteNode[] = [
{ type: 'PlainText', content: 'this is url\n' },
{ type: 'URL', content: 'https://github.com/syusui-s/rabbit/#readme' },
{ type: 'PlainText', content: ' ' },
{ type: 'HashTag', content: '#rabbit', tagName: 'rabbit' },
];
assert.deepStrictEqual(parsed, expected);
});
it('should parse text note with pubkey mentions', () => {
const parsed = parseTextNote({
kind: 1,
content: 'this is pubkey\n#[0] #[1]',
tags: [
['p', '9366708117c4a7edf9178acdce538c95059b9eb3394808cdd90564094172d972'],
['p', '80d3a41d8a00679c0105faac2cdf7643c9ba26835cff096bf7f9c7a0eee8c8fc'],
],
created_at: 1678377182,
pubkey: '9366708117c4a7edf9178acdce538c95059b9eb3394808cdd90564094172d972',
});
const expected: ParsedTextNoteNode[] = [
{ type: 'PlainText', content: 'this is pubkey\n' },
{
type: 'MentionedUser',
tagIndex: 0,
content: '#[0]',
pubkey: '9366708117c4a7edf9178acdce538c95059b9eb3394808cdd90564094172d972',
},
{ type: 'PlainText', content: ' ' },
{
type: 'MentionedUser',
tagIndex: 1,
content: '#[1]',
pubkey: '80d3a41d8a00679c0105faac2cdf7643c9ba26835cff096bf7f9c7a0eee8c8fc',
},
];
assert.deepStrictEqual(parsed, expected);
});
it('should parse text note which includes npub string', () => {
const parsed = parseTextNote({
kind: 1,
content:
'this is pubkey\nnpub1srf6g8v2qpnecqg9l2kzehmkg0ym5f5rtnlsj6lhl8r6pmhger7q5mtt3q\nhello',
tags: [],
created_at: 1678377182,
pubkey: '9366708117c4a7edf9178acdce538c95059b9eb3394808cdd90564094172d972',
});
const expected: ParsedTextNoteNode[] = [
{ type: 'PlainText', content: 'this is pubkey\n' },
{
type: 'Bech32Entity',
content: 'npub1srf6g8v2qpnecqg9l2kzehmkg0ym5f5rtnlsj6lhl8r6pmhger7q5mtt3q',
data: {
type: 'npub',
data: '80d3a41d8a00679c0105faac2cdf7643c9ba26835cff096bf7f9c7a0eee8c8fc',
},
},
{ type: 'PlainText', content: '\nhello' },
]; ];
assert.deepStrictEqual(parsed, expected); assert.deepStrictEqual(parsed, expected);

View File

@@ -28,9 +28,9 @@ export type Bech32Entity = {
data: data:
| { type: 'npub' | 'note'; data: string } | { type: 'npub' | 'note'; data: string }
| { type: 'nprofile'; data: ProfilePointer } | { type: 'nprofile'; data: ProfilePointer }
| { type: 'nevent'; data: EventPointer } | { type: 'nevent'; data: EventPointer };
| { type: 'naddr'; data: AddressPointer };
}; };
// | { type: 'naddr'; data: AddressPointer };
export type HashTag = { export type HashTag = {
type: 'HashTag'; type: 'HashTag';
@@ -63,7 +63,7 @@ export const parseTextNote = (event: NostrEvent): ParsedTextNote => {
...event.content.matchAll( ...event.content.matchAll(
/(?<url>(https?|wss?):\/\/[-a-zA-Z0-9.]+(?:\/[-\w.@%:]+|\/)*(?:\?[-\w=.@%:&]*)?(?:#[-\w=.%:&]*)?)/g, /(?<url>(https?|wss?):\/\/[-a-zA-Z0-9.]+(?:\/[-\w.@%:]+|\/)*(?:\?[-\w=.@%:&]*)?(?:#[-\w=.%:&]*)?)/g,
), ),
].sort((a, b) => a?.index - b?.index); ].sort((a, b) => (a.index as number) - (b.index as number));
let pos = 0; let pos = 0;
const result: ParsedTextNote = []; const result: ParsedTextNote = [];
@@ -76,16 +76,17 @@ export const parseTextNote = (event: NostrEvent): ParsedTextNote => {
}; };
matches.forEach((match) => { matches.forEach((match) => {
if (match.groups?.url && match.index >= pos) { const { index } = match as RegExpMatchArray & { index: number };
pushPlainText(match.index); if (match.groups?.url && index >= pos) {
pushPlainText(index);
const url: UrlText = { type: 'URL', content: match.groups?.url }; const url: UrlText = { type: 'URL', content: match.groups?.url };
result.push(url); result.push(url);
} else if (match.groups?.idx && match.index >= pos) { } else if (match.groups?.idx && index >= pos) {
const tagIndex = parseInt(match.groups.idx, 10); const tagIndex = parseInt(match.groups.idx, 10);
const tag = event.tags[tagIndex]; const tag = event.tags[tagIndex];
if (tag == null) return; if (tag == null) return;
pushPlainText(match.index); pushPlainText(index);
const tagName = tag[0]; const tagName = tag[0];
if (tagName === 'p') { if (tagName === 'p') {
@@ -110,7 +111,8 @@ export const parseTextNote = (event: NostrEvent): ParsedTextNote => {
}; };
result.push(mentionedEvent); result.push(mentionedEvent);
} }
} else if (match.groups?.nip19 && match.index >= pos) { } else if (match.groups?.nip19 && index >= pos) {
pushPlainText(index);
try { try {
const decoded = decode(match[0]); const decoded = decode(match[0]);
const bech32Entity: Bech32Entity = { const bech32Entity: Bech32Entity = {
@@ -122,8 +124,8 @@ export const parseTextNote = (event: NostrEvent): ParsedTextNote => {
} catch (e) { } catch (e) {
console.error(`failed to parse Bech32 entity (NIP-19) but ignore this: ${match[0]}`); console.error(`failed to parse Bech32 entity (NIP-19) but ignore this: ${match[0]}`);
} }
} else if (match.groups?.hashtag && match.index >= pos) { } else if (match.groups?.hashtag && index >= pos) {
pushPlainText(match.index); pushPlainText(index);
const tagName = match.groups?.hashtag; const tagName = match.groups?.hashtag;
const hashtag: HashTag = { const hashtag: HashTag = {
type: 'HashTag', type: 'HashTag',
@@ -132,7 +134,7 @@ export const parseTextNote = (event: NostrEvent): ParsedTextNote => {
}; };
result.push(hashtag); result.push(hashtag);
} }
pos = match.index + match[0].length; pos = index + match[0].length;
}); });
if (pos !== event.content.length) { if (pos !== event.content.length) {

View File

@@ -1,8 +0,0 @@
import assert from 'assert';
import { describe } from '@jest/globals';
describe('1 + 1', () => {
it('should be 2', () => {
assert(1 + 1 === 2);
});
});

6
vitest.config.ts Normal file
View File

@@ -0,0 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {},
});