File size: 7,855 Bytes
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { test, expect } from '@playwright/test'
import { API_KEY_HEADER } from './helpers'

const stamp = Date.now()
const testFile = `memory/e2e-mk-${stamp}.md`
const testFile2 = `memory/e2e-mk-linked-${stamp}.md`

test.describe('Memory Knowledge Features', () => {
  // Setup: create test files with wiki-links and schema
  test.beforeAll(async ({ request }) => {
    const file1 = await request.post('/api/memory', {
      headers: API_KEY_HEADER,
      data: {
        action: 'create',
        path: testFile,
        content: [
          '---',
          '_schema:',
          '  type: note',
          '  required: [title, tags]',
          'title: E2E Test Note',
          'tags: [test, e2e]',
          '---',
          `# E2E Note ${stamp}`,
          '',
          `This links to [[e2e-mk-linked-${stamp}]] for testing.`,
          '',
          'Some searchable content: unique-token-' + stamp,
        ].join('\n'),
      },
    })
    expect(file1.status()).toBe(200)

    const file2 = await request.post('/api/memory', {
      headers: API_KEY_HEADER,
      data: {
        action: 'create',
        path: testFile2,
        content: [
          '---',
          'title: Linked Note',
          '---',
          '# Linked Note',
          '',
          `Back-reference to [[e2e-mk-${stamp}]].`,
        ].join('\n'),
      },
    })
    expect(file2.status()).toBe(200)
  })

  // Cleanup
  test.afterAll(async ({ request }) => {
    await request.delete('/api/memory', {
      headers: API_KEY_HEADER,
      data: { action: 'delete', path: testFile },
    })
    await request.delete('/api/memory', {
      headers: API_KEY_HEADER,
      data: { action: 'delete', path: testFile2 },
    })
  })

  // --- Links API ---

  test('GET /api/memory/links returns link graph', async ({ request }) => {
    const res = await request.get('/api/memory/links', {
      headers: API_KEY_HEADER,
    })
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(Array.isArray(body.nodes)).toBe(true)
    expect(body.nodes.length).toBeGreaterThan(0)
    // Each node should have expected shape
    const node = body.nodes[0]
    expect(node).toHaveProperty('path')
    expect(node).toHaveProperty('linkCount')
  })

  test('GET /api/memory/links?file= returns per-file links', async ({ request }) => {
    const res = await request.get(
      `/api/memory/links?file=${encodeURIComponent(testFile)}`,
      { headers: API_KEY_HEADER }
    )
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(body.file).toBe(testFile)
    expect(Array.isArray(body.wikiLinks)).toBe(true)
    // Should find the wiki-link to the linked file
    const link = body.wikiLinks.find((l: any) => l.target.includes('linked'))
    expect(link).toBeTruthy()
  })

  test('links API requires auth', async ({ request }) => {
    const res = await request.get('/api/memory/links')
    expect(res.status()).toBe(401)
  })

  // --- Health API ---

  test('GET /api/memory/health returns health report', async ({ request }) => {
    const res = await request.get('/api/memory/health', {
      headers: API_KEY_HEADER,
    })
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(typeof body.overallScore).toBe('number')
    expect(body.overallScore).toBeGreaterThanOrEqual(0)
    expect(body.overallScore).toBeLessThanOrEqual(100)
    expect(Array.isArray(body.categories)).toBe(true)
    expect(body.categories.length).toBe(8)
    // Each category has expected shape
    for (const cat of body.categories) {
      expect(cat).toHaveProperty('name')
      expect(cat).toHaveProperty('score')
      expect(typeof cat.score).toBe('number')
    }
  })

  test('health API requires auth', async ({ request }) => {
    const res = await request.get('/api/memory/health')
    expect(res.status()).toBe(401)
  })

  // --- Context API ---

  test('GET /api/memory/context returns context payload', async ({ request }) => {
    const res = await request.get('/api/memory/context', {
      headers: API_KEY_HEADER,
    })
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(body).toHaveProperty('fileTree')
    expect(body).toHaveProperty('recentFiles')
    expect(body).toHaveProperty('healthSummary')
    expect(body).toHaveProperty('maintenanceSignals')
    expect(Array.isArray(body.fileTree)).toBe(true)
    expect(Array.isArray(body.recentFiles)).toBe(true)
    expect(Array.isArray(body.maintenanceSignals)).toBe(true)
    expect(typeof body.healthSummary).toBe('object')
  })

  test('context API requires auth', async ({ request }) => {
    const res = await request.get('/api/memory/context')
    expect(res.status()).toBe(401)
  })

  // --- Process API ---

  test('POST /api/memory/process reflect action', async ({ request }) => {
    const res = await request.post('/api/memory/process', {
      headers: API_KEY_HEADER,
      data: { action: 'reflect' },
    })
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(body.action).toBe('reflect')
    expect(Array.isArray(body.suggestions)).toBe(true)
  })

  test('POST /api/memory/process reweave action', async ({ request }) => {
    const res = await request.post('/api/memory/process', {
      headers: API_KEY_HEADER,
      data: { action: 'reweave' },
    })
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(body.action).toBe('reweave')
    expect(Array.isArray(body.suggestions)).toBe(true)
  })

  test('POST /api/memory/process generate-moc action', async ({ request }) => {
    const res = await request.post('/api/memory/process', {
      headers: API_KEY_HEADER,
      data: { action: 'generate-moc' },
    })
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(body.action).toBe('generate-moc')
    expect(Array.isArray(body.groups)).toBe(true)
    expect(typeof body.totalGroups).toBe('number')
    expect(typeof body.totalEntries).toBe('number')
  })

  test('process API rejects invalid action', async ({ request }) => {
    const res = await request.post('/api/memory/process', {
      headers: API_KEY_HEADER,
      data: { action: 'invalid' },
    })
    expect(res.status()).toBe(400)
  })

  test('process API requires auth', async ({ request }) => {
    const res = await request.post('/api/memory/process', {
      data: { action: 'reflect' },
    })
    expect(res.status()).toBe(401)
  })

  // --- Memory content now returns wiki-links and schema ---

  test('GET /api/memory content includes wikiLinks and schema', async ({ request }) => {
    const res = await request.get(
      `/api/memory?action=content&path=${encodeURIComponent(testFile)}`,
      { headers: API_KEY_HEADER }
    )
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(body.path).toBe(testFile)
    // Wiki-links extracted
    expect(Array.isArray(body.wikiLinks)).toBe(true)
    expect(body.wikiLinks.length).toBeGreaterThan(0)
    // Schema validation result
    expect(body.schema).toBeTruthy()
    expect(body.schema.valid).toBe(true)
    expect(body.schema.errors).toEqual([])
  })

  test('save returns schema warnings for missing required fields', async ({ request }) => {
    const badContent = [
      '---',
      '_schema:',
      '  type: note',
      '  required: [title, tags, missing_field]',
      'title: Present',
      'tags: [a]',
      '---',
      '# Body',
    ].join('\n')

    const res = await request.post('/api/memory', {
      headers: API_KEY_HEADER,
      data: { action: 'save', path: testFile, content: badContent },
    })
    expect(res.status()).toBe(200)
    const body = await res.json()
    expect(body.success).toBe(true)
    expect(Array.isArray(body.schemaWarnings)).toBe(true)
    expect(body.schemaWarnings).toContain('Missing required field: missing_field')
  })
})