Files
overleaf-cep/libraries/overleaf-editor-core/test/unit/schemas.test.js
T
Jakob Ackermann f6bb5ce1bd [real-time] adopt strict zod types and extend test coverage (#36031)
* [editor-core] extend coverage of zod schemas

* [real-time] adopt strict zod types and extend test coverage

* [project-history] make type check happy

* [web] make type check happy

GitOrigin-RevId: e1a65b9b3560b31ad2af817067a4b549865a5bb1
2026-09-17 11:42:50 +00:00

157 lines
5.2 KiB
JavaScript

const { expect } = require('chai')
const { rawLinkedFileData, rawFileMetadata } = require('../../lib/schemas')
describe('schemas', function () {
describe('rawLinkedFileData', function () {
it('rejects an empty object', function () {
const result = rawLinkedFileData.safeParse({})
expect(result.success).to.equal(false)
})
it('rejects an unrecognized provider', function () {
const result = rawLinkedFileData.safeParse({
provider: 'not-a-provider',
})
expect(result.success).to.equal(false)
})
it('rejects a project_file provider with an unrecognized extra key', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_file',
source_entity_path: '/main.tex',
extraUnrecognizedKey: 'abcd',
})
expect(result.success).to.equal(false)
})
it('accepts a project_file provider with a valid source_project_id', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_file',
source_project_id: '507f1f77bcf86cd799439011',
source_entity_path: '/main.tex',
})
expect(result.success).to.equal(true)
})
it('accepts a project_file provider with a missing source_project_id', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_file',
source_entity_path: '/main.tex',
})
expect(result.success).to.equal(true)
})
it('rejects a project_file provider with a malformed source_project_id', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_file',
source_project_id: 'not-an-object-id',
source_entity_path: '/main.tex',
})
expect(result.success).to.equal(false)
})
it('accepts a project_output_file provider with a valid source_project_id', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_output_file',
source_project_id: '507f1f77bcf86cd799439011',
source_output_file_path: 'output.pdf',
})
expect(result.success).to.equal(true)
})
it('rejects a project_output_file provider with a malformed source_project_id', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_output_file',
source_project_id: 'not-an-object-id',
source_output_file_path: 'output.pdf',
})
expect(result.success).to.equal(false)
})
it('accepts a project_output_file provider with a valid build_id', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_output_file',
source_output_file_path: 'output.pdf',
build_id: '1234-abcd',
})
expect(result.success).to.equal(true)
})
it('rejects a project_output_file provider with a malformed build_id', function () {
const result = rawLinkedFileData.safeParse({
provider: 'project_output_file',
source_output_file_path: 'output.pdf',
build_id: 'not-a-valid-build-id',
})
expect(result.success).to.equal(false)
})
for (const provider of ['mendeley', 'zotero', 'papers']) {
it(`accepts a ${provider} provider with an opaque group_id`, function () {
const result = rawLinkedFileData.safeParse({
provider,
group_id: 'abcd',
})
expect(result.success).to.equal(true)
})
it(`accepts a ${provider} provider with a missing group_id`, function () {
const result = rawLinkedFileData.safeParse({ provider })
expect(result.success).to.equal(true)
})
it(`rejects a ${provider} provider with a group_id containing a path separator`, function () {
const result = rawLinkedFileData.safeParse({
provider,
group_id: 'abcd/../../etc/passwd',
})
expect(result.success).to.equal(false)
})
it(`rejects a ${provider} provider with a group_id of ".."`, function () {
const result = rawLinkedFileData.safeParse({
provider,
group_id: '..',
})
expect(result.success).to.equal(false)
})
}
})
describe('rawFileMetadata', function () {
it('accepts a legacy v1 main flag', function () {
const result = rawFileMetadata.safeParse({
main: true,
})
expect(result.success).to.equal(true)
})
it('accepts a legacy v1 main flag with an importedAt timestamp', function () {
const result = rawFileMetadata.safeParse({
main: true,
importedAt: '2024-01-01T00:00:00.000Z',
})
expect(result.success).to.equal(true)
})
it('accepts an importedAt timestamp with no provider', function () {
const result = rawFileMetadata.safeParse({
importedAt: '2024-01-01T00:00:00.000Z',
})
expect(result.success).to.equal(true)
})
it('rejects a malformed importedAt timestamp with no provider', function () {
const result = rawFileMetadata.safeParse({
importedAt: 'not-a-date',
})
expect(result.success).to.equal(false)
})
it('accepts an empty object', function () {
const result = rawFileMetadata.safeParse({})
expect(result.success).to.equal(true)
})
})
})