import { expect } from 'chai'
import { createTestApp, cleanTestDir, createFile } from './helpers.js'
describe('PATCH', () => {
let request, testDir
beforeEach(() => {
({ request, testDir } = createTestApp())
})
afterEach(() => cleanTestDir(testDir))
describe('SPARQL UPDATE', () => {
it('inserts data into a new resource (201)', async () => {
const patch = `
PREFIX dc:
INSERT DATA { <> dc:title "Test" . }
`
await request.patch('/new.ttl')
.set('Content-Type', 'application/sparql-update')
.send(patch)
.expect(201)
})
it('inserts data into an existing resource (200)', async () => {
createFile(testDir, 'existing.ttl', ' "old".')
const patch = `
PREFIX ex:
INSERT DATA { ex:s ex:q "new" . }
`
await request.patch('/existing.ttl')
.set('Content-Type', 'application/sparql-update')
.send(patch)
.expect(200)
})
it('deletes and inserts (atomic replace)', async () => {
createFile(testDir, 'replace.ttl', ' "old".')
const patch = `
PREFIX ex:
DELETE { ex:s ex:p "old" . }
INSERT { ex:s ex:p "new" . }
`
await request.patch('/replace.ttl')
.set('Content-Type', 'application/sparql-update')
.send(patch)
.expect(200)
})
})
describe('N3 Patch', () => {
it('inserts triples into a new resource (201)', async () => {
const patch = `
@prefix solid: .
<> a solid:InsertDeletePatch;
solid:inserts { "value". }.
`
await request.patch('/n3new.ttl')
.set('Content-Type', 'text/n3')
.send(patch)
.expect(201)
})
it('inserts triples into existing resource (200)', async () => {
createFile(testDir, 'n3exist.ttl', ' "old".')
const patch = `
@prefix solid: .
<> a solid:InsertDeletePatch;
solid:inserts { "new". }.
`
await request.patch('/n3exist.ttl')
.set('Content-Type', 'text/n3')
.send(patch)
.expect(200)
})
it('returns 409 when deleting non-existent triple', async () => {
createFile(testDir, 'n3del.ttl', ' "exists".')
const patch = `
@prefix solid: .
<> a solid:InsertDeletePatch;
solid:deletes { "nope". }.
`
await request.patch('/n3del.ttl')
.set('Content-Type', 'text/n3')
.send(patch)
.expect(409)
})
it('returns 400 for missing insert/delete', async () => {
const patch = `
@prefix solid: .
<> a solid:InsertDeletePatch.
`
await request.patch('/bad.ttl')
.set('Content-Type', 'text/n3')
.send(patch)
.expect(400)
})
})
it('returns 415 for unsupported patch type', async () => {
await request.patch('/file.ttl')
.set('Content-Type', 'text/plain')
.send('something')
.expect(415)
})
})