This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/lib/uppy-media-optimization-plugin-test.js
Martin Brennan b626373b31
FEATURE: First pass of using uppy in the composer (#13935)
Adds uppy upload functionality behind a
enable_experimental_composer_uploader site setting (default false,
and hidden).

When enabled this site setting will make the composer-editor-uppy
component be used within composer.hbs, which in turn points to
a ComposerUploadUppy mixin which overrides the relevant
functions from ComposerUpload. This uppy uploader has parity
with all the features of jQuery file uploader in the original
composer-editor, including:

progress tracking
error handling
number of files validation
pasting files
dragging and dropping files
updating upload placeholders
upload markdown resolvers
processing actions (the only one we have so far is the media optimization
worker by falco, this works)
cancelling uploads
For now all uploads still go via the /uploads.json endpoint, direct
S3 support will be added later.

Also included in this PR are some changes to the media optimization
service, to support uppy's different file data structures, and also
to make the promise tracking and resolving more robust. Currently
it uses the file name to track promises, we can switch to something
more unique later if needed.

Does not include custom upload handlers, that will come
in a later PR, it is a tricky problem to handle.

Also, this new functionality will not be used in encrypted PMs because
encrypted PM uploads rely on custom upload handlers.
2021-08-13 09:14:34 +10:00

171 lines
5.7 KiB
JavaScript

import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
import { module, test } from "qunit";
import { Promise } from "rsvp";
class FakeUppy {
constructor() {
this.preprocessors = [];
this.emitted = [];
this.files = {
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764": {
data: "old file state",
},
"uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764": {
data: "old file state 1",
},
};
}
addPreProcessor(fn) {
this.preprocessors.push(fn);
}
getFile(id) {
return this.files[id];
}
emit(event, file, data) {
this.emitted.push({ event, file, data });
}
setFileState(fileId, state) {
this.files[fileId] = state;
}
}
module("Unit | Utility | UppyMediaOptimization Plugin", function () {
test("sets the options passed in", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
id: "test-uppy",
runParallel: true,
optimizeFn: function () {
return "wow such optimized";
},
});
assert.equal(plugin.id, "test-uppy");
assert.equal(plugin.runParallel, true);
assert.equal(plugin.optimizeFn(), "wow such optimized");
});
test("installation uses the correct function", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
id: "test-uppy",
runParallel: true,
});
plugin._optimizeParallel = function () {
return "using parallel";
};
plugin._optimizeSerial = function () {
return "using serial";
};
plugin.install();
assert.equal(plugin.uppy.preprocessors[0](), "using parallel");
plugin.runParallel = false;
plugin.uppy.preprocessors = [];
plugin.install();
assert.equal(plugin.uppy.preprocessors[0](), "using serial");
});
test("sets the file state when successfully optimizing the file and emits events", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
id: "test-uppy",
runParallel: true,
optimizeFn: () => {
return Promise.resolve("new file state");
},
});
plugin.install();
const done = assert.async();
const fileId =
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileId).data, "new file state");
done();
});
});
test("handles optimizer errors gracefully by leaving old file state and calling preprocess-complete", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
id: "test-uppy",
runParallel: true,
optimizeFn: () => {
return new Promise(() => {
throw new Error("bad stuff");
});
},
});
plugin.install();
const done = assert.async();
const fileId =
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileId).data, "old file state");
done();
});
});
test("handles serial file optimization successfully", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
id: "test-uppy",
runParallel: false,
optimizeFn: () => {
return Promise.resolve("new file state");
},
});
plugin.install();
const done = assert.async();
const fileIds = [
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764",
"uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764",
];
plugin.uppy.preprocessors[0](fileIds).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.equal(plugin.uppy.emitted[2].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[3].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileIds[0]).data, "new file state");
assert.equal(plugin.uppy.getFile(fileIds[1]).data, "new file state");
done();
});
});
test("handles parallel file optimization successfully", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
id: "test-uppy",
runParallel: true,
optimizeFn: () => {
return Promise.resolve("new file state");
},
});
plugin.install();
const done = assert.async();
const fileIds = [
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764",
"uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764",
];
plugin.uppy.preprocessors[0](fileIds).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[2].event, "preprocess-complete");
assert.equal(plugin.uppy.emitted[3].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileIds[0]).data, "new file state");
assert.equal(plugin.uppy.getFile(fileIds[1]).data, "new file state");
done();
});
});
});