Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 52 additions & 15 deletions __tests__/files/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('File data change', () => {
})
})

describe('Altering attributes updates mtime', () => {
describe('Altering attributes does NOT updates mtime', () => {
test('mtime is updated on existing attribute', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma',
Expand All @@ -202,12 +202,12 @@ describe('Altering attributes updates mtime', () => {
expect(file.attributes.test).toBe(true)
file.attributes.test = false

// Check that mtime has been updated
expect(file.mtime?.getDate()).toBe(new Date().getDate())
// Check that mtime has NOT been updated
expect(file.mtime?.getDate()).toBe(1)
expect(file.attributes.test).toBe(false)
})

test('mtime is updated on new attribute', () => {
test('mtime is NOT updated on new attribute', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma',
mime: 'image/jpeg',
Expand All @@ -217,12 +217,12 @@ describe('Altering attributes updates mtime', () => {
expect(file.attributes.test).toBeFalsy()
file.attributes.test = true

// Check that mtime has been updated
expect(file.mtime?.getDate()).toBe(new Date().getDate())
// Check that mtime has NOT been updated
expect(file.mtime?.getDate()).toBe(1)
expect(file.attributes.test).toBe(true)
})

test('mtime is updated on deleted attribute', () => {
test('mtime is NOT updated on deleted attribute', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma',
mime: 'image/jpeg',
Expand All @@ -235,8 +235,8 @@ describe('Altering attributes updates mtime', () => {
expect(file.attributes.test).toBe(true)
delete file.attributes.test

// Check that mtime has been updated
expect(file.mtime?.getDate()).toBe(new Date().getDate())
// Check that mtime has NOT been updated
expect(file.mtime?.getDate()).toBe(1)
expect(file.attributes.test).toBeUndefined()
})

Expand All @@ -245,15 +245,52 @@ describe('Altering attributes updates mtime', () => {
source: 'https://cloud.domain.com/remote.php/dav/files/emma',
mime: 'image/jpeg',
owner: 'emma',
attributes: {
test: true,
},
permissions: Permission.READ,
})
expect(file.attributes.test).toBe(true)
delete file.attributes.test

expect(file.permissions).toBe(Permission.READ)
file.permissions = Permission.ALL

// Check that mtime has been updated
expect(file.mtime).toBeUndefined()
expect(file.attributes.test).toBeUndefined()
expect(file.permissions).toBe(Permission.ALL)
})

})

describe('Altering top-level properties updates mtime', () => {
test('mtime is updated on permissions change', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma',
mime: 'image/jpeg',
owner: 'emma',
mtime: new Date(Date.UTC(1990, 0, 1, 0, 0, 0)),
permissions: Permission.READ,
})

expect(file.permissions).toBe(Permission.READ)
file.permissions = Permission.ALL

// Check that mtime has been updated
expect(file.mtime?.getDate()).toBe(new Date().getDate())
expect(file.permissions).toBe(Permission.ALL)
})

test('mtime is updated on size change', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma',
mime: 'image/jpeg',
owner: 'emma',
mtime: new Date(Date.UTC(1990, 0, 1, 0, 0, 0)),
size: 100,
})

expect(file.size).toBe(100)
file.size = 200

// Check that mtime has been updated
expect(file.mtime?.getDate()).toBe(new Date().getDate())
expect(file.size).toBe(200)
})

})
31 changes: 31 additions & 0 deletions __tests__/files/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ describe('FileId attribute', () => {
})
})

describe('Mtime attribute', () => {
test('Mtime definition', () => {
const mtime = new Date()
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
mtime,
})
expect(file.mtime?.toISOString()).toBe(mtime.toISOString())
})

test('Mtime update', async () => {
const mtime = new Date()
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
mtime,
})

expect(file.mtime?.toISOString()).toBe(mtime.toISOString())

// Update mtime
file.mtime = new Date()

// Mtime is updated
expect(file.mtime?.toISOString()).not.toBe(mtime.toISOString())
})
})

describe('Size attribute', () => {
test('Size definition', () => {
const file = new File({
Expand Down
19 changes: 10 additions & 9 deletions lib/files/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@ export abstract class Node {
if (this.readonlyAttributes.includes(prop)) {
return false
}
// Edit modification time
this.updateMtime()

// Apply original changes
return Reflect.set(target, prop, value)
},
deleteProperty: (target: Attribute, prop: string): boolean => {
if (this.readonlyAttributes.includes(prop)) {
return false
}
// Edit modification time
this.updateMtime()

// Apply original changes
return Reflect.deleteProperty(target, prop)
},
Expand All @@ -89,9 +87,6 @@ export abstract class Node {
// Update attributes, this sanitizes the attributes to only contain valid attributes
this.update(data.attributes ?? {})

// Reset the mtime if changed while updating the attributes
this._data.mtime = data.mtime

if (davService) {
this._knownDavService = davService
}
Expand Down Expand Up @@ -175,13 +170,18 @@ export abstract class Node {

/**
* Get the file modification time
* There is no setter as the modification time is not meant to be changed manually.
* It will be automatically updated when the attributes are changed.
*/
get mtime(): Date|undefined {
return this._data.mtime
}

/**
* Set the file modification time
*/
set mtime(mtime: Date|undefined) {
this._data.mtime = mtime
}

/**
* Get the file creation time
* There is no setter as the creation time is not meant to be changed
Expand Down Expand Up @@ -351,6 +351,7 @@ export abstract class Node {

/**
* Update the attributes of the node
* Warning, updating attributes will NOT automatically update the mtime.
*
* @param attributes The new attributes to update on the Node attributes
*/
Expand Down