import { describe, expect, test } from 'bun:test'
import { documentIdFromFigKey } from '@/app/integrations/storage/namespace'
import {
parseListObjectsV2Page,
parseListObjectsV2XML,
parseS3ErrorXML
} from '@/app/integrations/storage/s3/xml'
describe('parseListObjectsV2Xml', () => {
test('extracts keys and ignores objects outside canvas fig pattern when filtered', () => {
const xml = `
open_pencil_storage/canvases/a1.fig
2026-01-02T03:04:05.000Z
12
open_pencil_storage/canvases/a1.meta.json
2026-01-02T03:04:06.000Z
40
other-app/file.bin
2026-01-01T00:00:00.000Z
1
`
const listed = parseListObjectsV2XML(xml)
expect(listed).toHaveLength(3)
expect(listed[0]?.key).toBe('open_pencil_storage/canvases/a1.fig')
expect(listed[0]?.lastModified).toBe('2026-01-02T03:04:05.000Z')
expect(listed[0]?.size).toBe(12)
const canvasIds = listed
.map((object) => documentIdFromFigKey(object.key))
.filter((id): id is string => id != null)
expect(canvasIds).toEqual(['a1'])
})
test('decodes entities and reads namespaced pagination fields', () => {
const xml = `open_pencil_storage/canvases/a&b.fig1truea&b`
const page = parseListObjectsV2Page(xml)
expect(page.objects[0]?.key).toBe('open_pencil_storage/canvases/a&b.fig')
expect(page.isTruncated).toBe(true)
expect(page.nextContinuationToken).toBe('a&b')
})
test('parses S3 error XML without interpreting markup-like text', () => {
expect(
parseS3ErrorXML(
`AccessDeniedKey contains <Code>fake</Code>`,
403
)
).toEqual({ code: 'AccessDenied', message: 'Key contains fake' })
})
test('returns an empty page for malformed XML', () => {
expect(parseListObjectsV2Page('')).toEqual({
objects: [],
isTruncated: false,
nextContinuationToken: null
})
})
})