Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Fallback content is empty string when expected to be undefined #337

Closed
alexrisch opened this issue Apr 1, 2024 · 1 comment · Fixed by #341
Closed

Bug: Fallback content is empty string when expected to be undefined #337

alexrisch opened this issue Apr 1, 2024 · 1 comment · Fixed by #341
Labels
bug Something isn't working Partner Support

Comments

@alexrisch
Copy link
Contributor

alexrisch commented Apr 1, 2024

Describe the bug

When a content type fallback is set as undefined, Decoded message's fallback is ""

Expected behavior

Should be undefined

Steps to reproduce the bug

test('register and use custom content types', async () => {
  const bob = await Client.createRandom({
    env: 'local',
    codecs: [new NumberCodec()],
  })
  const alice = await Client.createRandom({
    env: 'local',
    // codecs: [new NumberCodec()],
  })
  bob.register(new NumberCodec())
  const bobConvo = await bob.conversations.newConversation(alice.address)
  const aliceConvo = await alice.conversations.newConversation(bob.address)
  await bobConvo.send(12, { contentType: ContentTypeNumber })
  const messages = await aliceConvo.messages()
  assert(messages.length === 1, 'did not get messages')
  const message = messages[0]
  let messageContent = undefined;
  try {
    messageContent = message.content()
  } catch (err) {
    messageContent = message.fallback
    console.log('Setting fallback', typeof message.fallback, {messageContent})
  }
  assert(
    messageContent !== "",
    'did not get content properly: ' + JSON.stringify(messageContent)
  )
  return true
})
class NumberCodec implements JSContentCodec<NumberRef> {
  contentType = ContentTypeNumber
  // a completely absurd way of encoding number values
  encode(content: NumberRef): EncodedContent {
    return {
      type: ContentTypeNumber,
      parameters: {
        test: 'test',
      },
      content: new TextEncoder().encode(JSON.stringify(content)),
    }
  }
  decode(encodedContent: EncodedContent): NumberRef {
    if (encodedContent.parameters.test !== 'test') {
      throw new Error(`parameters should parse ${encodedContent.parameters}`)
    }
    const contentReceived = JSON.parse(
      new TextDecoder().decode(encodedContent.content)
    ) as NumberRef
    return contentReceived
  }
  fallback(content: NumberRef): string | undefined {
    return undefined
  }
}
@alexrisch alexrisch added the bug Something isn't working label Apr 1, 2024
@nplasterer
Copy link
Contributor

Is this also the case for non-custom content types? I thought you were seeing it for read receipts as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Partner Support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants