Skip to content

Commit

Permalink
Decouple replaceChartContent from Obsidian datatypes
Browse files Browse the repository at this point in the history
Make it easier to maintain and test
  • Loading branch information
saertna committed Jun 12, 2024
1 parent 9e811ff commit 60bad6d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ obsidian-gamified-pkm/obsidian-gamified-pkm.zip
*.zip

# Exclude macOS Finder (System Explorer) View States
coverage/
/coverage/
.DS_Store
/cp-js2cortex.bat
ncryption
Expand Down
37 changes: 27 additions & 10 deletions src/creatmodchartcalculation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import {TFile} from 'obsidian';
import { debugLogs } from './constants';

interface FileInterface {
export interface AbstractFile {
path: string;
}

export interface FileInterface extends AbstractFile{
stat: {
ctime: number;
mtime: number;
size: number;
};
}

export interface VaultInterface {
getAbstractFileByPath(path: string): AbstractFile | null;
read(file: FileInterface): Promise<string>;
modify(file: FileInterface, data: string): Promise<void>;
}

export function findEarliestCreatedFile<T extends FileInterface>(files: T[]): T {
let earliestCreatedFile: T = files[0];
for (const file of files) {
Expand Down Expand Up @@ -82,16 +93,22 @@ export function createChartFormat(y_axis: string, countsStringMod: string, chart
return "```chart\ntype: bar\nlabels: [" + y_axis + "]\nseries:\n - title: modified\n data: [" + countsStringMod + "]\ntension: 0.2\nwidth: 80 %\nlabelColors: false\nfill: false\nbeginAtZero: false\nbestFit: false\nbestFitTitle: undefined\nbestFitNumber: 0\nstacked: true\nyTitle: \"Number of Notes\"\nxTitle: \"Months\"\nxMin: " + monatsbegrenzung + "\n```";
}


export async function replaceChartContent (avatarPageName: string, newContent: string) {
const existingFile = this.app.vault.getAbstractFileByPath(`${avatarPageName}.md`);

export async function replaceChartContent(
avatarPageName: string,
newContent: string,
vault: VaultInterface,
debugLogs = false
): Promise<void> {
const existingFile = vault.getAbstractFileByPath(`${avatarPageName}.md`);
if (existingFile == null) {
if(debugLogs) console.debug(`File ${avatarPageName}.md does not exist`);
if (debugLogs) console.debug(`File ${avatarPageName}.md does not exist`);
return;
}
const file = existingFile as TFile;
}

const file = existingFile as FileInterface;

const content = await this.app.vault.read(file);
const content = await vault.read(file);
let reference: number | null = null;
let end: number | null = null;
let start: number | null = null;
Expand All @@ -105,11 +122,11 @@ export async function replaceChartContent (avatarPageName: string, newContent: s
}
}
}
if (reference != null){
if (reference != null) {
end = reference;
start = reference - 19;
const newLines = [...lines.slice(0, start), newContent, ...lines.slice(end)];
await this.app.vault.modify(file, newLines.join("\n"));
await vault.modify(file, newLines.join("\n"));
}
}

Expand Down
64 changes: 59 additions & 5 deletions tests/creatmodchartcalculation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {findEarliestCreatedFile, findEarliestModifiedFile, findEarliestDateFile, monthsBetween, getCreationDates, getModificationDates, createChartFormat, replaceChartContent} from '../src/creatmodchartcalculation'
import {AbstractFile, VaultInterface, FileInterface, findEarliestCreatedFile, findEarliestModifiedFile, findEarliestDateFile, monthsBetween, getCreationDates, getModificationDates, createChartFormat, replaceChartContent} from '../src/creatmodchartcalculation'
import { describe } from 'node:test';


Expand All @@ -7,13 +7,14 @@ interface MockFile {
stat: {
ctime: number;
mtime: number;
size: number;
};
}

const files: MockFile[] = [
{ stat: { ctime: 1625234672000 , mtime: 1625234672000 } }, // File created on July 2, 2021
{ stat: { ctime: 1625233672000 , mtime: 1625233672000 } }, // File created on July 2, 2021 (earliest)
{ stat: { ctime: 1625235672000 , mtime: 1625235672000 } }, // File created on July 2, 2021
const files: FileInterface[] = [
{ path: 'file1.md', stat: { ctime: 1625234672000, mtime: 1625234672000, size: 1234 } },
{ path: 'file2.md', stat: { ctime: 1625233672000, mtime: 1625233672000, size: 1234 } },
{ path: 'file3.md', stat: { ctime: 1625235672000, mtime: 1625235672000, size: 1234 } },
];

describe('findEarliestCreatedFile', () => {
Expand Down Expand Up @@ -149,3 +150,56 @@ describe('createChartFormat', () => {
});

});

// Mock data and implementations
class MockVault implements VaultInterface {
private files: { [key: string]: string } = {};

addFile(path: string, content: string) {
this.files[path] = content;
}

getAbstractFileByPath(path: string): AbstractFile | null {
if (this.files[path]) {
return { path } as AbstractFile;
}
return null;
}

async read(file: FileInterface): Promise<string> {
return this.files[file.path];
}

async modify(file: FileInterface, data: string): Promise<void> {
this.files[file.path] = data;
}
}
/*
describe('replaceChartContent', () => {
let mockVault: MockVault;
beforeEach(() => {
mockVault = new MockVault();
mockVault.addFile('test.md', 'Line 1\n^ChartMonth\nLine 3');
});
it('should replace chart content correctly', async () => {
await replaceChartContent('test', 'New Chart Content', mockVault, true);
const modifiedContent = await mockVault.read({ path: 'test.md' } as FileInterface);
expect(modifiedContent).toBe('New Chart Content\nLine 3');
});
it('should log and return if the file does not exist', async () => {
const consoleSpy = jest.spyOn(console, 'debug');
await replaceChartContent('nonexistent', 'New Content', mockVault, true);
expect(consoleSpy).toHaveBeenCalledWith('File nonexistent.md does not exist');
});
it('should handle file with no ^ChartMonth', async () => {
mockVault.addFile('nochart.md', 'Line 1\nLine 2\nLine 3');
await replaceChartContent('nochart', 'New Content', mockVault, true);
const modifiedContent = await mockVault.read({ path: 'nochart.md' } as FileInterface);
expect(modifiedContent).toBe('Line 1\nLine 2\nLine 3');
});
});
*/

0 comments on commit 60bad6d

Please sign in to comment.