-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnotes.test.ts
More file actions
159 lines (138 loc) · 5.21 KB
/
Copy pathnotes.test.ts
File metadata and controls
159 lines (138 loc) · 5.21 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {API} from '@hackmd/api'
import {CommentPermissionType, NotePermissionRole} from '@hackmd/api/dist/type'
import {expect} from 'chai'
import {
cleanupTestConfigDir, clearConfigCache, getTestAPIEndpoint, setupTestConfigDir, shouldRunIntegrationTests,
} from './helpers'
describe('Integration: API Client - Notes', () => {
let testConfigDir: string
let apiClient: API
let createdNoteIds: string[] = []
before(function () {
// Ensure .env is loaded (it should be loaded by init.js, but double-check)
if (!process.env.HMD_API_ACCESS_TOKEN) {
const dotenv = require('dotenv')
// Try to load .env if not already loaded
const path = require('node:path')
const envPath = path.resolve(process.cwd(), '.env')
dotenv.config({override: true, path: envPath})
}
if (!shouldRunIntegrationTests()) {
this.skip()
}
})
beforeEach(() => {
clearConfigCache()
testConfigDir = setupTestConfigDir()
// Ensure env vars are set from .env
if (!process.env.HMD_API_ENDPOINT_URL) {
process.env.HMD_API_ENDPOINT_URL = getTestAPIEndpoint()
}
if (!process.env.HMD_API_ACCESS_TOKEN) {
throw new Error('HMD_API_ACCESS_TOKEN not set. Please check your .env file.')
}
const endpoint = getTestAPIEndpoint()
const token = process.env.HMD_API_ACCESS_TOKEN!
apiClient = new API(token, endpoint)
})
afterEach(async () => {
// Cleanup: delete created notes
if (apiClient && createdNoteIds.length > 0) {
await Promise.all(createdNoteIds.map(async noteId => {
try {
await apiClient.deleteNote(noteId)
} catch {
// Ignore errors during cleanup
}
}))
}
createdNoteIds = []
cleanupTestConfigDir(testConfigDir)
// Don't delete env vars - they're needed for other tests
})
it('should create a new note', async () => {
const note = await apiClient.createNote({
commentPermission: 'disabled' as CommentPermissionType,
content: '# Test Content\n\nThis is a test note created by integration tests.',
readPermission: 'owner' as NotePermissionRole,
title: 'Test Note',
writePermission: 'owner' as NotePermissionRole,
})
expect(note).to.have.property('id')
expect(note).to.have.property('title')
// Note: Some HackMD instances use content as title, so we check for either
expect(note.title === 'Test Note' || note.title === 'Test Content').to.be.true
expect(note).to.have.property('content')
createdNoteIds.push(note.id)
})
it('should list user notes', async () => {
const notes = await apiClient.getNoteList()
expect(notes).to.be.an('array')
if (notes.length > 0) {
expect(notes[0]).to.have.property('id')
expect(notes[0]).to.have.property('title')
}
})
it('should get a specific note', async () => {
// Create a note first
const createdNote = await apiClient.createNote({
commentPermission: 'disabled' as CommentPermissionType,
content: '# Test Content',
readPermission: 'owner' as NotePermissionRole,
title: 'Note to Get',
writePermission: 'owner' as NotePermissionRole,
})
createdNoteIds.push(createdNote.id)
// Get the note
const note = await apiClient.getNote(createdNote.id)
expect(note).to.have.property('id', createdNote.id)
expect(note).to.have.property('title')
// Note: Some HackMD instances use content as title, so we check for either
expect(note.title === 'Note to Get' || note.title === 'Test Content').to.be.true
expect(note).to.have.property('content')
})
it('should update a note', async () => {
// Create a note first
const createdNote = await apiClient.createNote({
commentPermission: 'disabled' as CommentPermissionType,
content: '# Original Content',
readPermission: 'owner' as NotePermissionRole,
title: 'Note to Update',
writePermission: 'owner' as NotePermissionRole,
})
createdNoteIds.push(createdNote.id)
// Update the note
const updatedNote = await apiClient.updateNote(createdNote.id, {
content: '# Updated Content',
})
// Some API versions return status object, others return note object
if (updatedNote.id) {
expect(updatedNote).to.have.property('id', createdNote.id)
expect(updatedNote.content).to.contain('Updated Content')
} else {
// If it returns status object, verify the update by fetching the note
const fetchedNote = await apiClient.getNote(createdNote.id)
expect(fetchedNote.content).to.contain('Updated Content')
}
})
it('should delete a note', async () => {
// Create a note first
const createdNote = await apiClient.createNote({
commentPermission: 'disabled' as CommentPermissionType,
content: '# Test',
readPermission: 'owner' as NotePermissionRole,
title: 'Note to Delete',
writePermission: 'owner' as NotePermissionRole,
})
// Delete the note
await apiClient.deleteNote(createdNote.id)
// Verify it's deleted by trying to get it (should fail)
try {
await apiClient.getNote(createdNote.id)
expect.fail('Note should have been deleted')
} catch (error) {
// Expected - note should not exist
expect(error).to.exist
}
})
})