Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

256 changes: 256 additions & 0 deletions packages/shared/src/types/plugin-local-rest-api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
import { describe, expect, test } from "bun:test";
import { type } from "arktype";

/**
* Tests for Local REST API type schemas
* Issue #41: Make frontmatter.tags optional in ApiVaultFileResponse
*/
describe("ApiVaultFileResponse schema", () => {
// Replicate the schema from plugin-local-rest-api.ts
const ApiVaultFileResponse = type({
frontmatter: {
tags: "string[]?",
description: "string?",
},
content: "string",
path: "string",
stat: {
ctime: "number",
mtime: "number",
size: "number",
},
});

test("accepts vault file response with tags", () => {
const response = {
frontmatter: {
tags: ["tag1", "tag2"],
description: "A test file",
},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(false);

if (!(validated instanceof type.errors)) {
expect(validated.frontmatter.tags).toEqual(["tag1", "tag2"]);
expect(validated.frontmatter.description).toBe("A test file");
}
});

test("accepts vault file response without tags", () => {
const response = {
frontmatter: {
description: "A test file",
},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(false);

if (!(validated instanceof type.errors)) {
expect(validated.frontmatter.tags).toBeUndefined();
expect(validated.frontmatter.description).toBe("A test file");
}
});

test("accepts vault file response without description", () => {
const response = {
frontmatter: {
tags: ["tag1"],
},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(false);

if (!(validated instanceof type.errors)) {
expect(validated.frontmatter.tags).toEqual(["tag1"]);
expect(validated.frontmatter.description).toBeUndefined();
}
});

test("accepts vault file response with empty frontmatter", () => {
const response = {
frontmatter: {},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(false);

if (!(validated instanceof type.errors)) {
expect(validated.frontmatter.tags).toBeUndefined();
expect(validated.frontmatter.description).toBeUndefined();
}
});

test("accepts vault file response with empty tags array", () => {
const response = {
frontmatter: {
tags: [],
},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(false);

if (!(validated instanceof type.errors)) {
expect(validated.frontmatter.tags).toEqual([]);
}
});

test("rejects vault file response with tags as non-array", () => {
const response = {
frontmatter: {
tags: "not-an-array",
},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(true);
});

test("rejects vault file response with tags containing non-strings", () => {
const response = {
frontmatter: {
tags: [123, 456],
},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(true);
});

test("requires content field", () => {
const response = {
frontmatter: {},
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(true);
});

test("requires path field", () => {
const response = {
frontmatter: {},
content: "# Test Content",
stat: {
ctime: 1234567890,
mtime: 1234567890,
size: 100,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(true);
});

test("requires stat field with correct structure", () => {
const response = {
frontmatter: {},
content: "# Test Content",
path: "/vault/test.md",
stat: {
ctime: 1234567890,
mtime: 1234567890,
// missing size
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(true);
});

test("accepts vault file with typical Obsidian frontmatter", () => {
const response = {
frontmatter: {
tags: ["obsidian", "notes", "productivity"],
description: "My daily note from today",
},
content: "# Daily Note\n\n## Tasks\n- [ ] Task 1",
path: "/vault/Daily/2024-01-15.md",
stat: {
ctime: 1705334400000,
mtime: 1705420800000,
size: 1024,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(false);
});

test("accepts vault file from Templater plugin without tags", () => {
// Templater templates may not have tags in frontmatter
const response = {
frontmatter: {
description: "Template for new notes",
},
content: "<% tp.date.now() %>",
path: "/vault/Templates/note-template.md",
stat: {
ctime: 1705334400000,
mtime: 1705420800000,
size: 512,
},
};

const validated = ApiVaultFileResponse(response);
expect(validated instanceof type.errors).toBe(false);
});
});
2 changes: 1 addition & 1 deletion packages/shared/src/types/plugin-local-rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const ApiVaultDirectoryResponse = type({
*/
export const ApiVaultFileResponse = type({
frontmatter: {
tags: "string[]",
tags: "string[]?",
description: "string?",
},
content: "string",
Expand Down