mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-14 00:26:21 +00:00
Fix: deselect and trigger refresh for deleted documents from bulk operations with "delete originals" (#8996)
This commit is contained in:
@@ -1,326 +0,0 @@
|
||||
import {
|
||||
HttpEventType,
|
||||
HttpResponse,
|
||||
provideHttpClient,
|
||||
withInterceptorsFromDi,
|
||||
} from '@angular/common/http'
|
||||
import {
|
||||
HttpTestingController,
|
||||
provideHttpClientTesting,
|
||||
} from '@angular/common/http/testing'
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import WS from 'jest-websocket-mock'
|
||||
import { environment } from 'src/environments/environment'
|
||||
import {
|
||||
ConsumerStatusService,
|
||||
FILE_STATUS_MESSAGES,
|
||||
FileStatusPhase,
|
||||
} from './consumer-status.service'
|
||||
import { DocumentService } from './rest/document.service'
|
||||
import { SettingsService } from './settings.service'
|
||||
|
||||
describe('ConsumerStatusService', () => {
|
||||
let httpTestingController: HttpTestingController
|
||||
let consumerStatusService: ConsumerStatusService
|
||||
let documentService: DocumentService
|
||||
let settingsService: SettingsService
|
||||
|
||||
const server = new WS(
|
||||
`${environment.webSocketProtocol}//${environment.webSocketHost}${environment.webSocketBaseUrl}status/`,
|
||||
{ jsonProtocol: true }
|
||||
)
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
ConsumerStatusService,
|
||||
DocumentService,
|
||||
SettingsService,
|
||||
provideHttpClient(withInterceptorsFromDi()),
|
||||
provideHttpClientTesting(),
|
||||
],
|
||||
})
|
||||
|
||||
httpTestingController = TestBed.inject(HttpTestingController)
|
||||
settingsService = TestBed.inject(SettingsService)
|
||||
settingsService.currentUser = {
|
||||
id: 1,
|
||||
username: 'testuser',
|
||||
is_superuser: false,
|
||||
}
|
||||
consumerStatusService = TestBed.inject(ConsumerStatusService)
|
||||
documentService = TestBed.inject(DocumentService)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
httpTestingController.verify()
|
||||
})
|
||||
|
||||
it('should update status on websocket processing progress', () => {
|
||||
const task_id = '1234'
|
||||
const status = consumerStatusService.newFileUpload('file.pdf')
|
||||
expect(status.getProgress()).toEqual(0)
|
||||
|
||||
consumerStatusService.connect()
|
||||
|
||||
consumerStatusService
|
||||
.onDocumentConsumptionFinished()
|
||||
.subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.SUCCESS)
|
||||
})
|
||||
|
||||
consumerStatusService.onDocumentDetected().subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.STARTED)
|
||||
})
|
||||
|
||||
server.send({
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'WORKING',
|
||||
})
|
||||
|
||||
expect(status.getProgress()).toBeCloseTo(0.6) // (0.8 * 50/100) + .2
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
])
|
||||
|
||||
server.send({
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 100,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'SUCCESS',
|
||||
message: FILE_STATUS_MESSAGES.finished,
|
||||
})
|
||||
|
||||
expect(status.getProgress()).toEqual(1)
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
0
|
||||
)
|
||||
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
|
||||
consumerStatusService.disconnect()
|
||||
})
|
||||
|
||||
it('should update status on websocket failed progress', () => {
|
||||
const task_id = '1234'
|
||||
const status = consumerStatusService.newFileUpload('file.pdf')
|
||||
status.taskId = task_id
|
||||
consumerStatusService.connect()
|
||||
|
||||
consumerStatusService
|
||||
.onDocumentConsumptionFailed()
|
||||
.subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.FAILED)
|
||||
})
|
||||
|
||||
server.send({
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
})
|
||||
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
])
|
||||
|
||||
server.send({
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'FAILED',
|
||||
message: FILE_STATUS_MESSAGES.document_already_exists,
|
||||
})
|
||||
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
0
|
||||
)
|
||||
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should update status on upload progress', () => {
|
||||
const task_id = '1234'
|
||||
const status = consumerStatusService.newFileUpload('file.pdf')
|
||||
|
||||
documentService.uploadDocument({}).subscribe((event) => {
|
||||
if (event.type === HttpEventType.Response) {
|
||||
status.taskId = event.body['task_id']
|
||||
status.message = $localize`Upload complete, waiting...`
|
||||
} else if (event.type === HttpEventType.UploadProgress) {
|
||||
status.updateProgress(
|
||||
FileStatusPhase.UPLOADING,
|
||||
event.loaded,
|
||||
event.total
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const req = httpTestingController.expectOne(
|
||||
`${environment.apiBaseUrl}documents/post_document/`
|
||||
)
|
||||
|
||||
req.event(
|
||||
new HttpResponse({
|
||||
body: {
|
||||
task_id,
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
req.event({
|
||||
type: HttpEventType.UploadProgress,
|
||||
loaded: 100,
|
||||
total: 300,
|
||||
})
|
||||
|
||||
expect(
|
||||
consumerStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
).toEqual([status])
|
||||
expect(consumerStatusService.getConsumerStatus()).toEqual([status])
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
])
|
||||
|
||||
req.event({
|
||||
type: HttpEventType.UploadProgress,
|
||||
loaded: 300,
|
||||
total: 300,
|
||||
})
|
||||
|
||||
expect(status.getProgress()).toEqual(0.2) // 0.2 * 300/300
|
||||
})
|
||||
|
||||
it('should support dismiss completed', () => {
|
||||
consumerStatusService.connect()
|
||||
server.send({
|
||||
task_id: '1234',
|
||||
filename: 'file.pdf',
|
||||
current_progress: 100,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'SUCCESS',
|
||||
message: 'finished',
|
||||
})
|
||||
|
||||
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
consumerStatusService.dismissCompleted()
|
||||
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(0)
|
||||
consumerStatusService.disconnect()
|
||||
})
|
||||
|
||||
it('should support dismiss', () => {
|
||||
const task_id = '1234'
|
||||
const status = consumerStatusService.newFileUpload('file.pdf')
|
||||
status.taskId = task_id
|
||||
status.updateProgress(FileStatusPhase.UPLOADING, 50, 100)
|
||||
|
||||
const status2 = consumerStatusService.newFileUpload('file2.pdf')
|
||||
status2.updateProgress(FileStatusPhase.UPLOADING, 50, 100)
|
||||
|
||||
expect(
|
||||
consumerStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
).toEqual([status, status2])
|
||||
expect(consumerStatusService.getConsumerStatus()).toEqual([status, status2])
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
status2,
|
||||
])
|
||||
|
||||
consumerStatusService.dismiss(status)
|
||||
expect(consumerStatusService.getConsumerStatus()).toEqual([status2])
|
||||
|
||||
consumerStatusService.dismiss(status2)
|
||||
expect(consumerStatusService.getConsumerStatus()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should support fail', () => {
|
||||
const task_id = '1234'
|
||||
const status = consumerStatusService.newFileUpload('file.pdf')
|
||||
status.taskId = task_id
|
||||
status.updateProgress(FileStatusPhase.UPLOADING, 50, 100)
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
1
|
||||
)
|
||||
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(0)
|
||||
consumerStatusService.fail(status, 'fail')
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
0
|
||||
)
|
||||
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should notify of document created on status message without upload', () => {
|
||||
let detected = false
|
||||
consumerStatusService.onDocumentDetected().subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.STARTED)
|
||||
detected = true
|
||||
})
|
||||
|
||||
consumerStatusService.connect()
|
||||
server.send({
|
||||
task_id: '1234',
|
||||
filename: 'file.pdf',
|
||||
current_progress: 0,
|
||||
max_progress: 100,
|
||||
message: 'new_file',
|
||||
status: 'STARTED',
|
||||
})
|
||||
|
||||
consumerStatusService.disconnect()
|
||||
expect(detected).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should notify of document in progress without upload', () => {
|
||||
consumerStatusService.connect()
|
||||
server.send({
|
||||
task_id: '1234',
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
docuement_id: 12,
|
||||
status: 'WORKING',
|
||||
})
|
||||
|
||||
consumerStatusService.disconnect()
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
1
|
||||
)
|
||||
})
|
||||
|
||||
it('should not notify current user if document has different expected owner', () => {
|
||||
consumerStatusService.connect()
|
||||
server.send({
|
||||
task_id: '1234',
|
||||
filename: 'file1.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
docuement_id: 12,
|
||||
owner_id: 1,
|
||||
status: 'WORKING',
|
||||
})
|
||||
|
||||
server.send({
|
||||
task_id: '5678',
|
||||
filename: 'file2.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
docuement_id: 13,
|
||||
owner_id: 2,
|
||||
status: 'WORKING',
|
||||
})
|
||||
|
||||
consumerStatusService.disconnect()
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
1
|
||||
)
|
||||
})
|
||||
})
|
@@ -9,11 +9,11 @@ import {
|
||||
} from '@angular/common/http/testing'
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import { environment } from 'src/environments/environment'
|
||||
import {
|
||||
ConsumerStatusService,
|
||||
FileStatusPhase,
|
||||
} from './consumer-status.service'
|
||||
import { UploadDocumentsService } from './upload-documents.service'
|
||||
import {
|
||||
FileStatusPhase,
|
||||
WebsocketStatusService,
|
||||
} from './websocket-status.service'
|
||||
|
||||
const files = [
|
||||
{
|
||||
@@ -45,14 +45,14 @@ const fileList = {
|
||||
describe('UploadDocumentsService', () => {
|
||||
let httpTestingController: HttpTestingController
|
||||
let uploadDocumentsService: UploadDocumentsService
|
||||
let consumerStatusService: ConsumerStatusService
|
||||
let websocketStatusService: WebsocketStatusService
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
UploadDocumentsService,
|
||||
ConsumerStatusService,
|
||||
WebsocketStatusService,
|
||||
provideHttpClient(withInterceptorsFromDi()),
|
||||
provideHttpClientTesting(),
|
||||
],
|
||||
@@ -60,7 +60,7 @@ describe('UploadDocumentsService', () => {
|
||||
|
||||
httpTestingController = TestBed.inject(HttpTestingController)
|
||||
uploadDocumentsService = TestBed.inject(UploadDocumentsService)
|
||||
consumerStatusService = TestBed.inject(ConsumerStatusService)
|
||||
websocketStatusService = TestBed.inject(WebsocketStatusService)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -80,11 +80,11 @@ describe('UploadDocumentsService', () => {
|
||||
it('updates progress during upload and failure', () => {
|
||||
uploadDocumentsService.uploadFiles(fileList)
|
||||
|
||||
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
2
|
||||
)
|
||||
expect(
|
||||
consumerStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
websocketStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
).toHaveLength(0)
|
||||
|
||||
const req = httpTestingController.match(
|
||||
@@ -98,7 +98,7 @@ describe('UploadDocumentsService', () => {
|
||||
})
|
||||
|
||||
expect(
|
||||
consumerStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
websocketStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
).toHaveLength(1)
|
||||
})
|
||||
|
||||
@@ -110,7 +110,7 @@ describe('UploadDocumentsService', () => {
|
||||
)
|
||||
|
||||
expect(
|
||||
consumerStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
||||
websocketStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
||||
).toHaveLength(0)
|
||||
|
||||
req[0].flush(
|
||||
@@ -122,7 +122,7 @@ describe('UploadDocumentsService', () => {
|
||||
)
|
||||
|
||||
expect(
|
||||
consumerStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
||||
websocketStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
||||
).toHaveLength(1)
|
||||
|
||||
uploadDocumentsService.uploadFiles(fileList)
|
||||
@@ -140,7 +140,7 @@ describe('UploadDocumentsService', () => {
|
||||
)
|
||||
|
||||
expect(
|
||||
consumerStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
||||
websocketStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
||||
).toHaveLength(2)
|
||||
})
|
||||
|
||||
|
@@ -2,11 +2,11 @@ import { HttpEventType } from '@angular/common/http'
|
||||
import { Injectable } from '@angular/core'
|
||||
import { FileSystemFileEntry, NgxFileDropEntry } from 'ngx-file-drop'
|
||||
import { Subscription } from 'rxjs'
|
||||
import {
|
||||
ConsumerStatusService,
|
||||
FileStatusPhase,
|
||||
} from './consumer-status.service'
|
||||
import { DocumentService } from './rest/document.service'
|
||||
import {
|
||||
FileStatusPhase,
|
||||
WebsocketStatusService,
|
||||
} from './websocket-status.service'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -16,7 +16,7 @@ export class UploadDocumentsService {
|
||||
|
||||
constructor(
|
||||
private documentService: DocumentService,
|
||||
private consumerStatusService: ConsumerStatusService
|
||||
private websocketStatusService: WebsocketStatusService
|
||||
) {}
|
||||
|
||||
onNgxFileDrop(files: NgxFileDropEntry[]) {
|
||||
@@ -37,7 +37,7 @@ export class UploadDocumentsService {
|
||||
private uploadFile(file: File) {
|
||||
let formData = new FormData()
|
||||
formData.append('document', file, file.name)
|
||||
let status = this.consumerStatusService.newFileUpload(file.name)
|
||||
let status = this.websocketStatusService.newFileUpload(file.name)
|
||||
|
||||
status.message = $localize`Connecting...`
|
||||
|
||||
@@ -61,11 +61,11 @@ export class UploadDocumentsService {
|
||||
error: (error) => {
|
||||
switch (error.status) {
|
||||
case 400: {
|
||||
this.consumerStatusService.fail(status, error.error.document)
|
||||
this.websocketStatusService.fail(status, error.error.document)
|
||||
break
|
||||
}
|
||||
default: {
|
||||
this.consumerStatusService.fail(
|
||||
this.websocketStatusService.fail(
|
||||
status,
|
||||
$localize`HTTP error: ${error.status} ${error.statusText}`
|
||||
)
|
||||
|
375
src-ui/src/app/services/websocket-status.service.spec.ts
Normal file
375
src-ui/src/app/services/websocket-status.service.spec.ts
Normal file
@@ -0,0 +1,375 @@
|
||||
import {
|
||||
HttpEventType,
|
||||
HttpResponse,
|
||||
provideHttpClient,
|
||||
withInterceptorsFromDi,
|
||||
} from '@angular/common/http'
|
||||
import {
|
||||
HttpTestingController,
|
||||
provideHttpClientTesting,
|
||||
} from '@angular/common/http/testing'
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import WS from 'jest-websocket-mock'
|
||||
import { environment } from 'src/environments/environment'
|
||||
import { DocumentService } from './rest/document.service'
|
||||
import { SettingsService } from './settings.service'
|
||||
import {
|
||||
FILE_STATUS_MESSAGES,
|
||||
FileStatusPhase,
|
||||
WebsocketStatusService,
|
||||
WebsocketStatusType,
|
||||
} from './websocket-status.service'
|
||||
|
||||
describe('ConsumerStatusService', () => {
|
||||
let httpTestingController: HttpTestingController
|
||||
let websocketStatusService: WebsocketStatusService
|
||||
let documentService: DocumentService
|
||||
let settingsService: SettingsService
|
||||
|
||||
const server = new WS(
|
||||
`${environment.webSocketProtocol}//${environment.webSocketHost}${environment.webSocketBaseUrl}status/`,
|
||||
{ jsonProtocol: true }
|
||||
)
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
WebsocketStatusService,
|
||||
DocumentService,
|
||||
SettingsService,
|
||||
provideHttpClient(withInterceptorsFromDi()),
|
||||
provideHttpClientTesting(),
|
||||
],
|
||||
})
|
||||
|
||||
httpTestingController = TestBed.inject(HttpTestingController)
|
||||
settingsService = TestBed.inject(SettingsService)
|
||||
settingsService.currentUser = {
|
||||
id: 1,
|
||||
username: 'testuser',
|
||||
is_superuser: false,
|
||||
}
|
||||
websocketStatusService = TestBed.inject(WebsocketStatusService)
|
||||
documentService = TestBed.inject(DocumentService)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
httpTestingController.verify()
|
||||
})
|
||||
|
||||
it('should update status on websocket processing progress', () => {
|
||||
const task_id = '1234'
|
||||
const status = websocketStatusService.newFileUpload('file.pdf')
|
||||
expect(status.getProgress()).toEqual(0)
|
||||
|
||||
websocketStatusService.connect()
|
||||
|
||||
websocketStatusService
|
||||
.onDocumentConsumptionFinished()
|
||||
.subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.SUCCESS)
|
||||
})
|
||||
|
||||
websocketStatusService.onDocumentDetected().subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.STARTED)
|
||||
})
|
||||
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'WORKING',
|
||||
},
|
||||
})
|
||||
|
||||
expect(status.getProgress()).toBeCloseTo(0.6) // (0.8 * 50/100) + .2
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
])
|
||||
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 100,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'SUCCESS',
|
||||
message: FILE_STATUS_MESSAGES.finished,
|
||||
},
|
||||
})
|
||||
|
||||
expect(status.getProgress()).toEqual(1)
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
0
|
||||
)
|
||||
expect(websocketStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
|
||||
websocketStatusService.disconnect()
|
||||
})
|
||||
|
||||
it('should update status on websocket failed progress', () => {
|
||||
const task_id = '1234'
|
||||
const status = websocketStatusService.newFileUpload('file.pdf')
|
||||
status.taskId = task_id
|
||||
websocketStatusService.connect()
|
||||
|
||||
websocketStatusService
|
||||
.onDocumentConsumptionFailed()
|
||||
.subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.FAILED)
|
||||
})
|
||||
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
},
|
||||
})
|
||||
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
])
|
||||
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id,
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'FAILED',
|
||||
message: FILE_STATUS_MESSAGES.document_already_exists,
|
||||
},
|
||||
})
|
||||
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
0
|
||||
)
|
||||
expect(websocketStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should update status on upload progress', () => {
|
||||
const task_id = '1234'
|
||||
const status = websocketStatusService.newFileUpload('file.pdf')
|
||||
|
||||
documentService.uploadDocument({}).subscribe((event) => {
|
||||
if (event.type === HttpEventType.Response) {
|
||||
status.taskId = event.body['task_id']
|
||||
status.message = $localize`Upload complete, waiting...`
|
||||
} else if (event.type === HttpEventType.UploadProgress) {
|
||||
status.updateProgress(
|
||||
FileStatusPhase.UPLOADING,
|
||||
event.loaded,
|
||||
event.total
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const req = httpTestingController.expectOne(
|
||||
`${environment.apiBaseUrl}documents/post_document/`
|
||||
)
|
||||
|
||||
req.event(
|
||||
new HttpResponse({
|
||||
body: {
|
||||
task_id,
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
req.event({
|
||||
type: HttpEventType.UploadProgress,
|
||||
loaded: 100,
|
||||
total: 300,
|
||||
})
|
||||
|
||||
expect(
|
||||
websocketStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
).toEqual([status])
|
||||
expect(websocketStatusService.getConsumerStatus()).toEqual([status])
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
])
|
||||
|
||||
req.event({
|
||||
type: HttpEventType.UploadProgress,
|
||||
loaded: 300,
|
||||
total: 300,
|
||||
})
|
||||
|
||||
expect(status.getProgress()).toEqual(0.2) // 0.2 * 300/300
|
||||
})
|
||||
|
||||
it('should support dismiss completed', () => {
|
||||
websocketStatusService.connect()
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id: '1234',
|
||||
filename: 'file.pdf',
|
||||
current_progress: 100,
|
||||
max_progress: 100,
|
||||
document_id: 12,
|
||||
status: 'SUCCESS',
|
||||
message: 'finished',
|
||||
},
|
||||
})
|
||||
|
||||
expect(websocketStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
websocketStatusService.dismissCompleted()
|
||||
expect(websocketStatusService.getConsumerStatusCompleted()).toHaveLength(0)
|
||||
websocketStatusService.disconnect()
|
||||
})
|
||||
|
||||
it('should support dismiss', () => {
|
||||
const task_id = '1234'
|
||||
const status = websocketStatusService.newFileUpload('file.pdf')
|
||||
status.taskId = task_id
|
||||
status.updateProgress(FileStatusPhase.UPLOADING, 50, 100)
|
||||
|
||||
const status2 = websocketStatusService.newFileUpload('file2.pdf')
|
||||
status2.updateProgress(FileStatusPhase.UPLOADING, 50, 100)
|
||||
|
||||
expect(
|
||||
websocketStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
||||
).toEqual([status, status2])
|
||||
expect(websocketStatusService.getConsumerStatus()).toEqual([
|
||||
status,
|
||||
status2,
|
||||
])
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toEqual([
|
||||
status,
|
||||
status2,
|
||||
])
|
||||
|
||||
websocketStatusService.dismiss(status)
|
||||
expect(websocketStatusService.getConsumerStatus()).toEqual([status2])
|
||||
|
||||
websocketStatusService.dismiss(status2)
|
||||
expect(websocketStatusService.getConsumerStatus()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should support fail', () => {
|
||||
const task_id = '1234'
|
||||
const status = websocketStatusService.newFileUpload('file.pdf')
|
||||
status.taskId = task_id
|
||||
status.updateProgress(FileStatusPhase.UPLOADING, 50, 100)
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
1
|
||||
)
|
||||
expect(websocketStatusService.getConsumerStatusCompleted()).toHaveLength(0)
|
||||
websocketStatusService.fail(status, 'fail')
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
0
|
||||
)
|
||||
expect(websocketStatusService.getConsumerStatusCompleted()).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should notify of document created on status message without upload', () => {
|
||||
let detected = false
|
||||
websocketStatusService.onDocumentDetected().subscribe((filestatus) => {
|
||||
expect(filestatus.phase).toEqual(FileStatusPhase.STARTED)
|
||||
detected = true
|
||||
})
|
||||
|
||||
websocketStatusService.connect()
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id: '1234',
|
||||
filename: 'file.pdf',
|
||||
current_progress: 0,
|
||||
max_progress: 100,
|
||||
message: 'new_file',
|
||||
status: 'STARTED',
|
||||
},
|
||||
})
|
||||
|
||||
websocketStatusService.disconnect()
|
||||
expect(detected).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should notify of document in progress without upload', () => {
|
||||
websocketStatusService.connect()
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id: '1234',
|
||||
filename: 'file.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
docuement_id: 12,
|
||||
status: 'WORKING',
|
||||
},
|
||||
})
|
||||
|
||||
websocketStatusService.disconnect()
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
1
|
||||
)
|
||||
})
|
||||
|
||||
it('should not notify current user if document has different expected owner', () => {
|
||||
websocketStatusService.connect()
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id: '1234',
|
||||
filename: 'file1.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
docuement_id: 12,
|
||||
owner_id: 1,
|
||||
status: 'WORKING',
|
||||
},
|
||||
})
|
||||
|
||||
server.send({
|
||||
type: WebsocketStatusType.STATUS_UPDATE,
|
||||
data: {
|
||||
task_id: '5678',
|
||||
filename: 'file2.pdf',
|
||||
current_progress: 50,
|
||||
max_progress: 100,
|
||||
docuement_id: 13,
|
||||
owner_id: 2,
|
||||
status: 'WORKING',
|
||||
},
|
||||
})
|
||||
|
||||
websocketStatusService.disconnect()
|
||||
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
||||
1
|
||||
)
|
||||
})
|
||||
|
||||
it('should trigger deleted subject on document deleted', () => {
|
||||
let deleted = false
|
||||
websocketStatusService.onDocumentDeleted().subscribe(() => {
|
||||
deleted = true
|
||||
})
|
||||
|
||||
websocketStatusService.connect()
|
||||
server.send({
|
||||
type: WebsocketStatusType.DOCUMENTS_DELETED,
|
||||
data: {
|
||||
documents: [1, 2, 3],
|
||||
},
|
||||
})
|
||||
|
||||
websocketStatusService.disconnect()
|
||||
expect(deleted).toBeTruthy()
|
||||
})
|
||||
})
|
@@ -1,9 +1,15 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { Subject } from 'rxjs'
|
||||
import { environment } from 'src/environments/environment'
|
||||
import { WebsocketConsumerStatusMessage } from '../data/websocket-consumer-status-message'
|
||||
import { WebsocketDocumentsDeletedMessage } from '../data/websocket-documents-deleted-message'
|
||||
import { WebsocketProgressMessage } from '../data/websocket-progress-message'
|
||||
import { SettingsService } from './settings.service'
|
||||
|
||||
export enum WebsocketStatusType {
|
||||
STATUS_UPDATE = 'status_update',
|
||||
DOCUMENTS_DELETED = 'documents_deleted',
|
||||
}
|
||||
|
||||
// see ProgressStatusOptions in src/documents/plugins/helpers.py
|
||||
export enum FileStatusPhase {
|
||||
STARTED = 0,
|
||||
@@ -85,7 +91,7 @@ export class FileStatus {
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ConsumerStatusService {
|
||||
export class WebsocketStatusService {
|
||||
constructor(private settingsService: SettingsService) {}
|
||||
|
||||
private statusWebSocket: WebSocket
|
||||
@@ -95,6 +101,7 @@ export class ConsumerStatusService {
|
||||
private documentDetectedSubject = new Subject<FileStatus>()
|
||||
private documentConsumptionFinishedSubject = new Subject<FileStatus>()
|
||||
private documentConsumptionFailedSubject = new Subject<FileStatus>()
|
||||
private documentDeletedSubject = new Subject<boolean>()
|
||||
|
||||
private get(taskId: string, filename?: string) {
|
||||
let status =
|
||||
@@ -145,63 +152,75 @@ export class ConsumerStatusService {
|
||||
this.statusWebSocket = new WebSocket(
|
||||
`${environment.webSocketProtocol}//${environment.webSocketHost}${environment.webSocketBaseUrl}status/`
|
||||
)
|
||||
this.statusWebSocket.onmessage = (ev) => {
|
||||
let statusMessage: WebsocketConsumerStatusMessage = JSON.parse(ev['data'])
|
||||
this.statusWebSocket.onmessage = (ev: MessageEvent) => {
|
||||
const {
|
||||
type,
|
||||
data: messageData,
|
||||
}: {
|
||||
type: WebsocketStatusType
|
||||
data: WebsocketProgressMessage | WebsocketDocumentsDeletedMessage
|
||||
} = JSON.parse(ev.data)
|
||||
|
||||
// fallback if backend didn't restrict message
|
||||
if (
|
||||
statusMessage.owner_id &&
|
||||
statusMessage.owner_id !== this.settingsService.currentUser?.id &&
|
||||
!this.settingsService.currentUser?.is_superuser
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
let statusMessageGet = this.get(
|
||||
statusMessage.task_id,
|
||||
statusMessage.filename
|
||||
)
|
||||
let status = statusMessageGet.status
|
||||
let created = statusMessageGet.created
|
||||
|
||||
status.updateProgress(
|
||||
FileStatusPhase.WORKING,
|
||||
statusMessage.current_progress,
|
||||
statusMessage.max_progress
|
||||
)
|
||||
if (
|
||||
statusMessage.message &&
|
||||
statusMessage.message in FILE_STATUS_MESSAGES
|
||||
) {
|
||||
status.message = FILE_STATUS_MESSAGES[statusMessage.message]
|
||||
} else if (statusMessage.message) {
|
||||
status.message = statusMessage.message
|
||||
}
|
||||
status.documentId = statusMessage.document_id
|
||||
|
||||
if (statusMessage.status in FileStatusPhase) {
|
||||
status.phase = FileStatusPhase[statusMessage.status]
|
||||
}
|
||||
|
||||
switch (status.phase) {
|
||||
case FileStatusPhase.STARTED:
|
||||
if (created) this.documentDetectedSubject.next(status)
|
||||
switch (type) {
|
||||
case WebsocketStatusType.DOCUMENTS_DELETED:
|
||||
this.documentDeletedSubject.next(true)
|
||||
break
|
||||
|
||||
case FileStatusPhase.SUCCESS:
|
||||
this.documentConsumptionFinishedSubject.next(status)
|
||||
break
|
||||
|
||||
case FileStatusPhase.FAILED:
|
||||
this.documentConsumptionFailedSubject.next(status)
|
||||
break
|
||||
|
||||
default:
|
||||
case WebsocketStatusType.STATUS_UPDATE:
|
||||
this.handleProgressUpdate(messageData as WebsocketProgressMessage)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleProgressUpdate(messageData: WebsocketProgressMessage) {
|
||||
// fallback if backend didn't restrict message
|
||||
if (
|
||||
messageData.owner_id &&
|
||||
messageData.owner_id !== this.settingsService.currentUser?.id &&
|
||||
!this.settingsService.currentUser?.is_superuser
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
let statusMessageGet = this.get(messageData.task_id, messageData.filename)
|
||||
let status = statusMessageGet.status
|
||||
let created = statusMessageGet.created
|
||||
|
||||
status.updateProgress(
|
||||
FileStatusPhase.WORKING,
|
||||
messageData.current_progress,
|
||||
messageData.max_progress
|
||||
)
|
||||
if (messageData.message && messageData.message in FILE_STATUS_MESSAGES) {
|
||||
status.message = FILE_STATUS_MESSAGES[messageData.message]
|
||||
} else if (messageData.message) {
|
||||
status.message = messageData.message
|
||||
}
|
||||
status.documentId = messageData.document_id
|
||||
|
||||
if (messageData.status in FileStatusPhase) {
|
||||
status.phase = FileStatusPhase[messageData.status]
|
||||
}
|
||||
|
||||
switch (status.phase) {
|
||||
case FileStatusPhase.STARTED:
|
||||
if (created) this.documentDetectedSubject.next(status)
|
||||
break
|
||||
|
||||
case FileStatusPhase.SUCCESS:
|
||||
this.documentConsumptionFinishedSubject.next(status)
|
||||
break
|
||||
|
||||
case FileStatusPhase.FAILED:
|
||||
this.documentConsumptionFailedSubject.next(status)
|
||||
break
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fail(status: FileStatus, message: string) {
|
||||
status.message = message
|
||||
status.phase = FileStatusPhase.FAILED
|
||||
@@ -250,4 +269,8 @@ export class ConsumerStatusService {
|
||||
onDocumentDetected() {
|
||||
return this.documentDetectedSubject
|
||||
}
|
||||
|
||||
onDocumentDeleted() {
|
||||
return this.documentDeletedSubject
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user